diff --git a/src/socketify/socketify.py b/src/socketify/socketify.py index f4eefe9..caa0eeb 100644 --- a/src/socketify/socketify.py +++ b/src/socketify/socketify.py @@ -604,6 +604,9 @@ class AppResponse: return (bool(result.ok), bool(result.has_responded)) except: return (False, False) + + def cork_end(self, message, end_connection=False): + self.cork(lambda res: res.end(message, end_connection)) def end(self, message, end_connection=False): try: diff --git a/tests/examples/async.py b/tests/examples/async.py index e752f0d..65f5b91 100644 --- a/tests/examples/async.py +++ b/tests/examples/async.py @@ -5,7 +5,7 @@ app = App() async def delayed_hello(delay, res): await asyncio.sleep(delay) #do something async - res.cork(lambda res: res.end("Hello with delay!")) + res.cork_end("Hello with delay!") def home(res, req): #request object only lives during the life time of this call @@ -22,7 +22,8 @@ async def json(res, req): 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.cork(lambda res: res.end({ "message": "I'm delayed!", "user-agent": user_agent})) + + res.cork_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 94c29cc..91cf6ca 100644 --- a/tests/examples/http_request_cache.py +++ b/tests/examples/http_request_cache.py @@ -45,7 +45,7 @@ def list_original_pokemons(res, req): #get asynchronous from Model async def get_originals(): value = await cache.run_once("original_pokemons", 5, get_original_pokemons) - res.cork(lambda res: res.end(value)) + res.cork_end(value) res.run_async(get_originals()) @@ -70,7 +70,7 @@ def list_pokemon(res, req): #sync with redis lock to run only once #if more than 1 worker/request try to do this request, only one will call the Model and the others will get from cache value = await cache.run_once(cache_key, 5, get_pokemon, number) - res.cork(lambda res: res.end(value)) + res.cork_end(value) res.run_async(find_pokemon(number, res)) diff --git a/tests/examples/router_and_basics.py b/tests/examples/router_and_basics.py index 0da5107..eee1129 100644 --- a/tests/examples/router_and_basics.py +++ b/tests/examples/router_and_basics.py @@ -45,7 +45,10 @@ def user(res, req): async def delayed_hello(delay, res): await asyncio.sleep(delay) #do something async - res.cork(lambda res: res.end("Hello sorry for the delay!")) + res.cork_end("Hello sorry for the delay!") + # cork_end is a less verbose way of writing + # res.cork(lambda res: res.end("Hello sorry for the delay!")) + def delayed(res, req): #request object only lives during the life time of this call @@ -66,7 +69,7 @@ async def sleepy_json(res, req): #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.cork(lambda res: res.end({ "message": "I'm delayed!", "user-agent": user_agent})) + res.cork_end({ "message": "I'm delayed!", "user-agent": user_agent}) def custom_header(res, req): res.write_header("Content-Type", "application/octet-stream") diff --git a/tests/examples/upload_or_post.py b/tests/examples/upload_or_post.py index 57f8d38..5177424 100644 --- a/tests/examples/upload_or_post.py +++ b/tests/examples/upload_or_post.py @@ -10,7 +10,7 @@ def upload(res, req): def on_data(res, chunk, is_end): print(f"Got chunk of data with length {len(chunk)}, is_end: {is_end}") if (is_end): - res.cork(lambda res: res.end("Thanks for the data!")) + res.cork_end("Thanks for the data!") res.on_data(on_data) @@ -24,7 +24,7 @@ async def upload_chunks(res, req): print(f"Got chunk of data with length {len(chunk)}") #We respond when we are done - res.cork(lambda res: res.end("Thanks for the data!")) + res.cork_end("Thanks for the data!") async def upload_json(res, req): print(f"Posted to {req.get_url()}") @@ -35,7 +35,7 @@ async def upload_json(res, req): print(f"First person is named: {people[0]['name']}") #We respond when we are done - res.cork(lambda res: res.end("Thanks for the data!")) + res.cork_end("Thanks for the data!") async def upload_text(res, req): print(f"Posted to {req.get_url()}") @@ -45,7 +45,7 @@ async def upload_text(res, req): print(f"Your text is ${text}") #We respond when we are done - res.cork(lambda res: res.end("Thanks for the data!")) + res.cork_end("Thanks for the data!") async def upload_urlencoded(res, req): print(f"Posted to {req.get_url()}") @@ -55,7 +55,7 @@ async def upload_urlencoded(res, req): print(f"Your form is ${form}") #We respond when we are done - res.cork(lambda res: res.end("Thanks for the data!")) + res.cork_end("Thanks for the data!") async def upload_multiple(res, req): @@ -72,7 +72,7 @@ async def upload_multiple(res, req): print(f"Your data is ${data}") #We respond when we are done - res.cork(lambda res: res.end("Thanks for the data!")) + res.cork_end("Thanks for the data!") app = App()