Skip to content

Commit

Permalink
remove handler, add the neccesary comments and remove the extra part
Browse files Browse the repository at this point in the history
  • Loading branch information
IronJam11 committed Jan 30, 2025
1 parent 99b532b commit d2af5e5
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions channels/generic/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@
from ..exceptions import StopConsumer

logger = logging.getLogger("channels.consumer")
if not logger.hasHandlers():
handler = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)


class AsyncHttpConsumer(AsyncConsumer):
"""
Async HTTP consumer. Provides basic primitives for building asynchronous
HTTP endpoints.
Note that the ASGI spec requires that the protocol server only starts
sending the response to the client after ``self.send_body`` has been
called the first time.
"""

def __init__(self, *args, **kwargs):
Expand All @@ -43,6 +40,10 @@ async def send_headers(self, *, status=200, headers=None):
async def send_body(self, body, *, more_body=False):
"""
Sends a response body to the client. The method expects a bytestring.
Set ``more_body=True`` if you want to send more body content later.
The default behavior closes the response, and further messages on
the channel will be ignored.
"""
assert isinstance(body, bytes), "Body is not bytes"
await self.send(
Expand All @@ -51,14 +52,18 @@ async def send_body(self, body, *, more_body=False):

async def send_response(self, status, body, **kwargs):
"""
Sends a response to the client.
Sends a response to the client. This is a thin wrapper over
``self.send_headers`` and ``self.send_body``, and everything said
above applies here as well. This method may only be called once.
"""
await self.send_headers(status=status, **kwargs)
await self.send_body(body)

async def handle(self, body):
"""
Receives the request body as a bytestring.
Receives the request body as a bytestring. Response may be composed
using the ``self.send*`` methods; the return value of this method is
thrown away.
"""
raise NotImplementedError(
"Subclasses of AsyncHttpConsumer must provide a handle() method."
Expand Down Expand Up @@ -94,10 +99,6 @@ async def http_disconnect(self, message):
"""
Let the user do their cleanup and close the consumer.
"""
try:
await self.disconnect()
await aclose_old_connections()
except Exception as e:
logger.error(f"Error during disconnect: {str(e)}")
finally:
raise StopConsumer()
await self.disconnect()
await aclose_old_connections()
raise StopConsumer()

0 comments on commit d2af5e5

Please sign in to comment.