[docs]classHttpRequestSerializer(BaseHttpMessageSerializer):""" Serialize an HTTP request object into string representations for different message parts: - summary: the first line of the request message (e.g. "GET / HTTP/1.1") - headers: the headers of the request message (e.g. "Host: localhost:4080\nConnection: keep-alive\n...") - body: the body of the request message (e.g. b'{"foo": "bar"}') The main goal of this serializer is to prepare a request message for storage. """wrapped:HttpRequest@cached_propertydefquery_string(self)->str:returnurlencode(self.wrapped.query)ifself.wrapped.queryelse""@cached_propertydefsummary(self)->str:query=f"?{self.query_string}"ifself.query_stringelse""returnf"{self.wrapped.method}{self.wrapped.path}{query} HTTP/1.1"
[docs]classHttpResponseSerializer(BaseHttpMessageSerializer):""" Serialize an HTTP response object into string representations for different message parts: - summary: the first line of the response message (e.g. "HTTP/1.1 200 OK") - headers: the headers of the response message (e.g. "Content-Type: text/plain\nContent-Length: 13\n...") - body: the body of the response message (e.g. b'Hello, world!') The main goal of this serializer is to prepare a response message for storage. """wrapped:HttpResponse@cached_propertydefsummary(self)->str:reason=codes.get_reason_phrase(self.wrapped.status)returnf"HTTP/1.1 {self.wrapped.status}{reason}"
[docs]classHttpErrorSerializer(BaseHttpMessageSerializer):""" Serialize an HTTP error object into string representations for different message parts: - summary: the error message - headers: empty - body: stack trace (xxx this may change, maybe too much info and too much internal) The main goal of this serializer is to prepare an error message for storage. """wrapped:HttpError@cached_propertydefsummary(self)->str:returnself.wrapped.message
[docs]defget_serializer_for(message:BaseMessage)->MessageSerializer:ifisinstance(message,HttpRequest):returnHttpRequestSerializer(message)ifisinstance(message,HttpResponse):returnHttpResponseSerializer(message)ifisinstance(message,HttpError):returnHttpErrorSerializer(message)raiseValueError(f"No serializer available for message type: {type(message)}")