You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
httpx didn't maintain compatibility with requests, which is fine most of the time.
However sometimes when a third party library assumes requests-style interfaces (e.g. googleapis/google-resumable-media-python#440), it's nice to be able to mask httpx.Responses to look like requests.Responses.
To do so, I made a wrapper class that looks something like this:
importhttpxclassRequestsLikeHTTPXStreamingResponse:
""" Wrapper that converts a httpx.Response to look like a streamable requests.Response. SEE: https://www.python-httpx.org/compatibility/#streaming-responses """def__init__(self, response: httpx.Response):
self.response=responsedef__getattr__(self, name: str):
returngetattr(self.response, name)
defiter_content(self, chunk_size: int=1, decode_unicode: bool=False):
ifdecode_unicode:
returnself.response.iter_text(chunk_size)
returnself.response.iter_bytes(chunk_size)
@propertydefraw(self):
returnself.responsedef__enter__(self):
returnselfdef__exit__(self, *_) ->None:
self.response.close()
Do you think httpx can officially support adapters/wrappers between the two libraries?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
httpx
didn't maintain compatibility withrequests
, which is fine most of the time.However sometimes when a third party library assumes
requests
-style interfaces (e.g. googleapis/google-resumable-media-python#440), it's nice to be able to maskhttpx.Response
s to look likerequests.Response
s.To do so, I made a wrapper class that looks something like this:
Do you think
httpx
can officially support adapters/wrappers between the two libraries?Beta Was this translation helpful? Give feedback.
All reactions