Skip to content

Commit

Permalink
change Response.write, change "end_response" to "more_body"
Browse files Browse the repository at this point in the history
Make method Response.write more like the asgi protocol
  • Loading branch information
livioribeiro committed Dec 18, 2023
1 parent 0352bcb commit ee78eb3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ on:
push:
branches:
- main
paths:
- .github/workflows/test.yml
- pyproject.toml
- 'src/**'
- 'tests/**'

jobs:
test:
Expand Down
4 changes: 2 additions & 2 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def app(scope, receive, send):
response.content_type = "text/plain"
response.content_length = len(greeting)
await response.start()
await response.write(greeting, end_response=True)
await response.write(greeting)

# # shortcut
## shortcut
# await respond_text(response, greeting)
17 changes: 13 additions & 4 deletions examples/responses/response_streaming.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from asgikit.requests import Request
from asgikit.responses import stream_writer
from asgikit.responses import respond_stream, stream_writer

from . import fibonacci

Expand All @@ -16,6 +16,15 @@ async def app(scope, receive, send):
limit = int(request.query.get("limit", "10"))

response.content_type = "text/plain"
async with stream_writer(response) as write:
async for data in fibonacci_stream(limit):
await write(data)

await respond_stream(response, fibonacci_stream(limit))

## alternative with stream_writer
# async with stream_writer(response) as write:
# async for data in fibonacci_stream(limit):
# await write(data)

## alternative with response.write
# async for data in fibonacci_stream(limit):
# await response.write(data, more_body=True)
# await response.end()
1 change: 1 addition & 0 deletions examples/responses/response_streaming_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ async def app(scope, receive, send):
limit = int(request.query.get("limit", "10"))

response.content_type = "application/json"

await respond_stream(response, fibonacci_stream(limit))
16 changes: 8 additions & 8 deletions src/asgikit/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async def start(self, status=HTTPStatus.OK):
}
)

async def write(self, data: bytes | str, *, end_response=False):
async def write(self, data: bytes | str, *, more_body=False):
encoded_data = data if isinstance(data, bytes) else data.encode(self.encoding)

if not self.is_started:
Expand All @@ -213,21 +213,21 @@ async def write(self, data: bytes | str, *, end_response=False):
{
"type": "http.response.body",
"body": encoded_data,
"more_body": not end_response,
"more_body": more_body,
}
)

if end_response:
if not more_body:
self.__set_finished()

async def end(self):
if not self.is_started:
raise RuntimeError("response was not started")

if self.is_finished:
raise RuntimeError("response has already ended")
raise RuntimeError("response is already finished")

await self.write(b"", end_response=True)
await self.write(b"", more_body=False)


async def respond_text(
Expand All @@ -240,7 +240,7 @@ async def respond_text(
response.content_length = len(data)

await response.start(status)
await response.write(data, end_response=True)
await response.write(data, more_body=False)


async def respond_status(response: Response, status: HTTPStatus):
Expand Down Expand Up @@ -271,7 +271,7 @@ async def respond_json(response: Response, content: Any, status=HTTPStatus.OK):
response.content_length = len(data)

await response.start(status)
await response.write(data, end_response=True)
await response.write(data, more_body=False)


async def __listen_for_disconnect(receive):
Expand All @@ -290,7 +290,7 @@ async def stream_writer(response: Response):
async def write(data: bytes | str):
if client_disconect.done():
raise ClientDisconnectError()
await response.write(data, end_response=False)
await response.write(data, more_body=True)

try:
yield write
Expand Down

0 comments on commit ee78eb3

Please sign in to comment.