From: Tom Christie Date: Tue, 30 Apr 2019 10:40:37 +0000 (+0100) Subject: Clean up response interface X-Git-Tag: 0.3.0~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d908d024f72cc4df5d163b55d801792f5a16529;p=thirdparty%2Fhttpx.git Clean up response interface --- diff --git a/httpcore/models.py b/httpcore/models.py index 97e18dc5..2e01dab5 100644 --- a/httpcore/models.py +++ b/httpcore/models.py @@ -324,20 +324,6 @@ class Response: self.is_closed = False self.is_streamed = False - decoders = [] # type: typing.List[Decoder] - value = self.headers.get("content-encoding", "identity") - for part in value.split(","): - part = part.strip().lower() - decoder_cls = SUPPORTED_DECODERS[part] - decoders.append(decoder_cls()) - - if len(decoders) == 0: - self.decoder = IdentityDecoder() # type: Decoder - elif len(decoders) == 1: - self.decoder = decoders[0] - else: - self.decoder = MultiDecoder(decoders) - if isinstance(body, bytes): self.is_closed = True self.body = self.decoder.decode(body) + self.decoder.flush() @@ -350,8 +336,36 @@ class Response: @property def url(self) -> typing.Optional[URL]: + """ + Returns the URL for which the request was made. + + Requires that `request` was provided when instantiating the response. + """ return None if self.request is None else self.request.url + @property + def decoder(self) -> Decoder: + """ + Returns a decoder instance which can be used to decode the raw byte + content, depending on the Content-Encoding used in the response. + """ + if not hasattr(self, "_decoder"): + decoders = [] # type: typing.List[Decoder] + value = self.headers.get("content-encoding", "identity") + for part in value.split(","): + part = part.strip().lower() + decoder_cls = SUPPORTED_DECODERS[part] + decoders.append(decoder_cls()) + + if len(decoders) == 1: + self._decoder = decoders[0] + elif len(decoders) > 1: + self._decoder = MultiDecoder(decoders) + else: + self._decoder = IdentityDecoder() + + return self._decoder + async def read(self) -> bytes: """ Read and return the response content.