@@ -14,9 +14,10 @@ In addition to those examples, consider the following ones that illustrate Pytho
1414## Parse an incoming request URL
1515
1616``` python
17- from workers import Response
17+ from workers import handler, Response
1818from urllib.parse import urlparse, parse_qs
1919
20+ @handler
2021async def on_fetch (request , env ):
2122 # Parse the incoming request URL
2223 url = urlparse(request.url)
@@ -38,8 +39,9 @@ async def on_fetch(request, env):
3839## Parse JSON from the incoming request
3940
4041``` python
41- from workers import Response
42+ from workers import handle, Response
4243
44+ @handler
4345async def on_fetch (request ):
4446 name = (await request.json())[" name" ]
4547 return Response(f " Hello, { name} " )
@@ -50,10 +52,11 @@ async def on_fetch(request):
5052``` python
5153# To use the JavaScript console APIs
5254from js import console
53- from workers import Response
55+ from workers import handler, Response
5456# To use the native Python logging
5557import logging
5658
59+ @handler
5760async def on_fetch (request ):
5861 # Use the console APIs from JavaScript
5962 # https://developer.mozilla.org/en-US/docs/Web/API/console
@@ -78,14 +81,11 @@ async def on_fetch(request):
7881
7982``` python
8083from js import Object
81- from pyodide.ffi import to_js as _to_js
84+ import json
8285
83- from workers import Response
84-
85- # to_js converts between Python dictionaries and JavaScript Objects
86- def to_js (obj ):
87- return _to_js(obj, dict_converter = Object.fromEntries)
86+ from workers import handler, Response
8887
88+ @handler
8989async def on_fetch (request , env ):
9090 # Bindings are available on the 'env' parameter
9191 # https://developers.cloudflare.com/queues/
@@ -94,7 +94,7 @@ async def on_fetch(request, env):
9494 # We can also pass plain text strings
9595 await env.QUEUE .send(" hello" , contentType = " text" )
9696 # Send a JSON payload
97- await env.QUEUE .send(to_js ({" hello" : " world" }))
97+ await env.QUEUE .send(json.dumps ({" hello" : " world" }))
9898
9999 # Return a response
100100 return Response.json({" write" : " success" })
@@ -103,8 +103,9 @@ async def on_fetch(request, env):
103103## Query a D1 Database
104104
105105``` python
106- from workers import Response
106+ from workers import handler, Response
107107
108+ @handler
108109async def on_fetch (request , env ):
109110 results = await env.DB .prepare(" PRAGMA table_list" ).all()
110111 # Return a JSON response
0 commit comments