-
Notifications
You must be signed in to change notification settings - Fork 64
Description
I currently have a wrapper library around requests that lets me easily log the raw requests/responses which is very helpful for troubleshooting.
The relevant bits of logic look something like this:
def requests_PreparedRequest_str ( self: requests.PreparedRequest ) -> str:
headers = '\r\n'.join ( f'{k}: {v}' for k, v in self.headers.items() )
body = t2s ( self.body or '', 'ascii', 'backslashreplace' )
return f'{self.method} {self.url}\r\n{headers}\r\n\r\n{body}'
def requests_Response_str ( self: requests.Response ) -> str:
d = { 10: 'HTTP/1.0', 11: 'HTTP/1.1' }
version = d.get ( self.raw.version ) or self.raw.version
headers = '\r\n'.join ( f'{k}: {v}' for k, v in self.headers.items() )
return f'{version or "MOCK"} {self.status_code} {self.reason}\r\n{headers}\r\n\r\n{self.text}'
setattr ( requests.PreparedRequest, '__str__', requests_PreparedRequest_str )
setattr ( requests.models.Response, '__str__', requests_Response_str )
log = logging.debug
req = session.prepare_request ( requests.Request ( method, url, **kwargs ) )
if log:
for line in map ( lambda line: line.rstrip ( '\r\n' ), str ( req ).split ( '\n' ) ):
log ( f'C>{line}' )
r = session.send ( req )
if log:
for line in map ( lambda line: line.rstrip ( '\r\n' ), str ( r ).split ( '\n' ) ):
log ( f'S>{line}' )
There doesn't appear to be a straight-forward way to implement this sort of thing in asks. I'd be happy to work on a PR, but I would need some guidance from you guys on how to do it.
It looks like I could subclass RequestProcessor._send() and RequestProcessor._catch_response(), but those functions are so deep that I don't think I could do so without disrupting all uses of asks within the process.
Specifically one of the things I'm able to do is to log requests/responses with different log levels depending on whether the call was successful or not. For example, sometimes I'll log any 4xx/5xx response as an ERROR, but 2xx/3xx as DEBUG. However, in some scenarios I have to process the body of a response to determine whether a request was successful or not, then choose the log level.
Thanks!