@@ -32,17 +32,32 @@ class HTTPResponse:
3232 # Response with 'Content-Length' header
3333 @server.route(path, method)
3434 def route_func(request):
35+
3536 response = HTTPResponse(request)
3637 response.send("Some content", content_type="text/plain")
37- # or
38- @server.route(path, method)
39- def route_func(request):
38+
39+ # or
40+
41+ response = HTTPResponse(request)
42+ with response:
43+ response.send(body='Some content', content_type="text/plain")
44+
45+ # or
46+
4047 with HTTPResponse(request) as response:
4148 response.send("Some content", content_type="text/plain")
4249
4350 # Response with 'Transfer-Encoding: chunked' header
4451 @server.route(path, method)
4552 def route_func(request):
53+
54+ response = HTTPResponse(request, content_type="text/plain", chunked=True)
55+ with response:
56+ response.send_body_chunk("Some content")
57+ response.send_body_chunk("Some more content")
58+
59+ # or
60+
4661 with HTTPResponse(request, content_type="text/plain", chunked=True) as response:
4762 response.send_body_chunk("Some content")
4863 response.send_body_chunk("Some more content")
@@ -68,7 +83,8 @@ def __init__( # pylint: disable=too-many-arguments
6883 """
6984 Creates an HTTP response.
7085
71- Sets `status`, ``headers`` and `http_version`.
86+ Sets `status`, ``headers`` and `http_version`
87+ and optionally default ``content_type``.
7288
7389 To send the response, call `send` or `send_file`.
7490 For chunked response use
@@ -122,7 +138,8 @@ def send(
122138 """
123139 Sends response with `body` over the given socket.
124140 Implicitly calls ``_send_headers`` before sending the body.
125- Should be called only once per response.
141+
142+ Should be called **only once** per response.
126143 """
127144 encoded_response_message_body = body .encode ("utf-8" )
128145
@@ -139,6 +156,9 @@ def send_file(
139156 ) -> None :
140157 """
141158 Send response with content of ``filename`` located in ``root_path`` over the given socket.
159+ Implicitly calls ``_send_headers`` before sending the file content.
160+
161+ Should be called **only once** per response.
142162 """
143163 if not root_path .endswith ("/" ):
144164 root_path += "/"
@@ -162,7 +182,7 @@ def send_chunk_body(self, chunk: str = "") -> None:
162182 """
163183 Send chunk of data to the given socket.
164184
165- Should be used only inside
185+ Should be used ** only** inside
166186 ``with HTTPResponse(request, chunked=True) as response:`` context manager.
167187
168188 :param str chunk: String data to be sent.
0 commit comments