Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates Python Workers examples to use new Python SDK API #18596

Draft
wants to merge 1 commit into
base: production
Choose a base branch
from

Conversation

dom96
Copy link
Contributor

@dom96 dom96 commented Dec 5, 2024

The new Python SDK is now available in the latest version of Wrangler. It provides Python native implementations of classes like Response.

This PR modifies most of our Python examples to use the new APIs in the SDK.

Copy link

cloudflare-workers-and-pages bot commented Dec 5, 2024

Deploying cloudflare-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: a938004
Status: ✅  Deploy successful!
Preview URL: https://c3c58b58.cloudflare-docs-7ou.pages.dev
Branch Preview URL: https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev

View logs

Copy link

github-actions bot commented Dec 5, 2024

Files with changes (up to 15)

Original Link Updated Link
https://developers.cloudflare.com/workers/examples/alter-headers/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/alter-headers/
https://developers.cloudflare.com/workers/examples/conditional-response/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/conditional-response/
https://developers.cloudflare.com/workers/languages/python/examples/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/languages/python/examples/
https://developers.cloudflare.com/workers/examples/geolocation-app-weather/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/geolocation-app-weather/
https://developers.cloudflare.com/workers/languages/python/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/languages/python/
https://developers.cloudflare.com/workers/examples/security-headers/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/security-headers/
https://developers.cloudflare.com/workers/examples/protect-against-timing-attacks/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/protect-against-timing-attacks/
https://developers.cloudflare.com/workers/examples/aggregate-requests/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/aggregate-requests/
https://developers.cloudflare.com/workers/examples/103-early-hints/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/103-early-hints/
https://developers.cloudflare.com/workers/examples/fetch-json/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/fetch-json/
https://developers.cloudflare.com/workers/examples/modify-response/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/modify-response/
https://developers.cloudflare.com/workers/examples/redirect/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/redirect/
https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/accessing-the-cloudflare-object/
https://developers.cloudflare.com/workers/examples/bulk-redirects/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/bulk-redirects/
https://developers.cloudflare.com/workers/examples/auth-with-headers/ https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev/workers/examples/auth-with-headers/


if location:
return Response.redirect(location, 301)

# If request not in map, return the original request
return fetch(request)
Copy link
Contributor Author

@dom96 dom96 Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that confused me while I was going through these examples: this line ends up creating an infinite loop of requests back to the worker. What's the intention behind this? Is there something I missed?

I changed it here to return a 404 as it seems to make most sense for this example, but there are other examples that use the same pattern. I left those largely alone.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@dom96 dom96 Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. These examples should really call this pattern out. Getting an infinite loop on examples like this is really not a great dev experience when playing with these examples locally.

Maybe something for another PR

@dom96 dom96 force-pushed the dominik/update-python-for-sdk branch from 7df4a10 to e662fd3 Compare December 9, 2024 17:41
@dom96
Copy link
Contributor Author

dom96 commented Dec 9, 2024

  • The validator version that's currently in prod doesn't support these changes yet, so we need to wait for it to be updated before merging this.

@dom96 dom96 force-pushed the dominik/update-python-for-sdk branch from e662fd3 to a938004 Compare December 9, 2024 17:43
@@ -61,19 +61,21 @@ export default {
</TabItem> <TabItem label="Python" icon="seti:python">

```py
from js import Response, fetch, Headers, JSON, Promise
from cloudflare.workers import Response, fetch
import asyncio
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be deployed even when packages not enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah

Copy link
Contributor

@irvinebroque irvinebroque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the actual SDK documented? Showing what modules and methods are available on it?

@dom96 dom96 marked this pull request as draft January 13, 2025 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product:d1 D1: https://developers.cloudflare.com/d1/ product:workers Related to Workers product size/m
Projects
None yet
Development

Successfully merging this pull request may close these issues.