-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Conversation
Deploying cloudflare-docs with
|
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 |
7df4a10
to
e662fd3
Compare
|
e662fd3
to
a938004
Compare
There was a problem hiding this 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?
a938004
to
81e9232
Compare
# 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is response.headers
?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
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. |
encoder = TextEncoder.new() | ||
a = encoder.encode(auth_token) | ||
b = encoder.encode(secret) |
There was a problem hiding this comment.
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())
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
81e9232
to
c5315f8
Compare
c5315f8
to
4ec4f33
Compare
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.