Skip to content

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

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

Merged
merged 1 commit into from
Mar 24, 2025

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: 81e9232
Status: ✅  Deploy successful!
Preview URL: https://622ec03c.cloudflare-docs-7ou.pages.dev
Branch Preview URL: https://dominik-update-python-for-sd.cloudflare-docs-7ou.pages.dev

View logs

@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
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
@Oxyjun Oxyjun removed their assignment Feb 11, 2025
@dom96 dom96 force-pushed the dominik/update-python-for-sdk branch from a938004 to 81e9232 Compare March 14, 2025 12:42
@dom96 dom96 marked this pull request as ready for review March 14, 2025 12:42
Comment on lines -88 to +89
# Clone the response so that it's no longer immutable
new_response = Response.new(response.body, response)
# Grab the response headers so they can be modified
new_headers = response.headers
Copy link
Contributor

Choose a reason for hiding this comment

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

What is response.headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


return new_response
return Response(response.body, headers=new_headers)
Copy link
Contributor

Choose a reason for hiding this comment

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

What would happen if we just returned the original response? Since it looks like we modified its headers in place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we don't modify them in-place, we get a new instance of the headers when we access headers.

@dom96
Copy link
Contributor Author

dom96 commented Mar 14, 2025

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

Nowhere as of right now, but we do have some code to generate markdown from the Python modules that we can get into the docs eventually.

Comment on lines 87 to 89
encoder = TextEncoder.new()
a = encoder.encode(auth_token)
b = encoder.encode(secret)
Copy link
Contributor

Choose a reason for hiding this comment

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

Using TextEncoder here is pretty weird I think. I like the following slightly better:

    a = to_js(auth_token.encode())
    b = to_js(auth_token.encode())

Copy link
Contributor

Choose a reason for hiding this comment

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

Not really relevant to the current PR though I understand.

@@ -232,23 +232,23 @@ async def on_fetch(request):
blocked_headers = ["Public-Key-Pins", "X-Powered-By" ,"X-AspNet-Version"]

res = await fetch(request)
new_headers = Headers.new(res.headers)
new_headers = res.headers
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, calling this new_headers but seemingly (?) modifying the original ones in place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, headers returns a new instance of headers as a dict

@@ -99,7 +101,7 @@ async def on_fetch(request, env):
## Query a D1 Database

```python
from js import Response
from workers import Response
Copy link
Contributor

Choose a reason for hiding this comment

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

So does Response.json() just work? Maybe we should replace the example above that does it manually?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you mean that the above example uses to_js in the Response.json? I guess I missed that, fixed it.

Copy link
Contributor

@hoodmane hoodmane left a comment

Choose a reason for hiding this comment

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

Looks good to me aside from a bunch of minor comments. I'm most concerned about the semantics around mutation, but it's probably not that important.

@dom96 dom96 force-pushed the dominik/update-python-for-sdk branch from 81e9232 to c5315f8 Compare March 14, 2025 14:18
Copy link
Contributor

github-actions bot commented Mar 14, 2025

Preview URL: https://4ec4f337.preview.developers.cloudflare.com
Preview Branch URL: https://dominik-update-python-for-sdk.preview.developers.cloudflare.com

Files with changes (up to 15)

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

@dom96 dom96 force-pushed the dominik/update-python-for-sdk branch from c5315f8 to 4ec4f33 Compare March 14, 2025 14:53
@dom96 dom96 merged commit 904820f into production Mar 24, 2025
9 checks passed
@dom96 dom96 deleted the dominik/update-python-for-sdk branch March 24, 2025 17:07
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.