Better default application of user-agent in requests lib
@@ -33,10 +33,10 @@ def patch_requests_source_address(new_source_address):
def patch_requests_user_agent(user_agent):
import requests
old_request = requests.Session.request
- def new_request(*args, **kwargs):
- headers = kwargs.get("headers", {})
- headers = headers if headers else {}
+ def new_request(self, *args, **kwargs):
+ headers = kwargs.get("headers", getattr(self, "headers", {}))
+ if "User-Agent" not in headers:
headers["User-Agent"] = user_agent
kwargs["headers"] = headers
- return old_request(*args, **kwargs)
+ return old_request(self, *args, **kwargs)
requests.Session.request = new_request
