From 8a7333a4bd6609b4a67f068387a56aa07fab376a Mon Sep 17 00:00:00 2001 From: Ciro Date: Thu, 2 Jun 2022 17:27:15 -0300 Subject: [PATCH] added more info about async and decoded to utf8 string results from request --- src/socketify/socketify.py | 10 +++++----- tests/examples/async.py | 10 ++++++---- tests/examples/http_request_cache.py | 4 +++- tests/examples/router_and_basics.py | 8 +++++--- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/socketify/socketify.py b/src/socketify/socketify.py index e5904a7..56357b3 100644 --- a/src/socketify/socketify.py +++ b/src/socketify/socketify.py @@ -270,14 +270,14 @@ class AppRequest: buffer_address = ffi.addressof(buffer, 0)[0] if buffer_address == ffi.NULL: return None - return ffi.unpack(buffer_address, length) + return ffi.unpack(buffer_address, length).decode("utf-8") def get_method(self): buffer = ffi.new("char**") length = lib.uws_req_get_method(self.req, buffer) buffer_address = ffi.addressof(buffer, 0)[0] if buffer_address == ffi.NULL: return None - return ffi.unpack(buffer_address, length) + return ffi.unpack(buffer_address, length).decode("utf-8") def get_header(self, lower_case_header): buffer = ffi.new("char**") data = lower_case_header.encode("utf-8") @@ -285,7 +285,7 @@ class AppRequest: buffer_address = ffi.addressof(buffer, 0)[0] if buffer_address == ffi.NULL: return None - return ffi.unpack(buffer_address, length) + return ffi.unpack(buffer_address, length).decode("utf-8") def get_query(self, key): buffer = ffi.new("char**") data = key.encode("utf-8") @@ -293,14 +293,14 @@ class AppRequest: buffer_address = ffi.addressof(buffer, 0)[0] if buffer_address == ffi.NULL: return None - return ffi.unpack(buffer_address, length) + return ffi.unpack(buffer_address, length).decode("utf-8") def get_parameter(self, index): buffer = ffi.new("char**") length = lib.uws_req_get_parameter(self.req, ffi.cast("unsigned short", index), buffer) buffer_address = ffi.addressof(buffer, 0)[0] if buffer_address == ffi.NULL: return None - return ffi.unpack(buffer_address, length) + return ffi.unpack(buffer_address, length).decode("utf-8") def set_yield(self, has_yield): lib.uws_req_set_field(self.req, 1 if has_yield else 0) def get_yield(self): diff --git a/tests/examples/async.py b/tests/examples/async.py index 60fc5c7..06e7ef0 100644 --- a/tests/examples/async.py +++ b/tests/examples/async.py @@ -16,11 +16,13 @@ def home(res, req): #abort handler is grabed here, so responses only will be send if res.aborted == False res.run_async(delayed_hello(delay, res)) -async def json(res, _): - #req maybe will not be available in direct attached async functions - #but if you dont care about req info you can do it +async def json(res, req): + #request object only lives during the life time of this call + #get parameters, query, headers anything you need here before first await :) + user_agent = req.get_header("user-agent") + #req maybe will not be available in direct attached async functions after await await asyncio.sleep(2) #do something async - res.end({ "message": "I'm delayed!"}) + res.end({ "message": "I'm delayed!", "user-agent": user_agent}) def not_found(res, req): res.write_status(404).end("Not Found") diff --git a/tests/examples/http_request_cache.py b/tests/examples/http_request_cache.py index f79dc6a..ad22b51 100644 --- a/tests/examples/http_request_cache.py +++ b/tests/examples/http_request_cache.py @@ -71,7 +71,9 @@ def list_pokemon(res, req): res.run_async(find_pokemon(number, res)) - +### +# Here i decided to use an sync first and async only if needs, but you can use async directly see ./async.py +### app = App() app.get("/", list_original_pokemons) app.get("/:number", list_pokemon) diff --git a/tests/examples/router_and_basics.py b/tests/examples/router_and_basics.py index 4f2881f..cb1381d 100644 --- a/tests/examples/router_and_basics.py +++ b/tests/examples/router_and_basics.py @@ -38,11 +38,13 @@ def json(res, req): #if you pass an object will auto write an header with application/json res.end({ "message": "I'm an application/json!"}) -async def sleepy_json(res, _): - #req maybe will not be available in direct attached async functions +async def sleepy_json(res, req): + #get parameters, query, headers anything you need here before first await :) + user_agent = req.get_header("user-agent") + #req maybe will not be available in direct attached async functions after await #but if you dont care about req info you can do it await asyncio.sleep(2) #do something async - res.end({ "message": "I'm delayed!"}) + res.end({ "message": "I'm delayed!", "user-agent": user_agent}) def custom_header(res, req): res.write_header("Content-Type", "application/octet-stream")