Set user agent on all requests
This commit is contained in:
Родитель
654f6c214a
Коммит
cfdc46cd0b
|
@ -29,6 +29,7 @@ class ADOClient:
|
|||
|
||||
:param str tenant: The ADO tenant to connect to
|
||||
:param Tuple[str,str] credentials: The credentials to use for the API connection
|
||||
:param Optional[str] user_agent: The user agent to set
|
||||
:param Optional[Dict[str,str]] extra_headers: Any extra headers which should be sent with the API requests
|
||||
:param Optional[logging.Logger] log: The logger to use for logging (a new one will be used if one is not supplied)
|
||||
"""
|
||||
|
@ -53,6 +54,7 @@ class ADOClient:
|
|||
*,
|
||||
tenant: str,
|
||||
credentials: Tuple[str, str],
|
||||
user_agent: Optional[str] = None,
|
||||
extra_headers: Optional[Dict[str, str]] = None,
|
||||
log: Optional[logging.Logger] = None,
|
||||
) -> None:
|
||||
|
@ -64,7 +66,11 @@ class ADOClient:
|
|||
self.log = log.getChild("ado")
|
||||
|
||||
self.http_client = ADOHTTPClient(
|
||||
tenant=tenant, credentials=credentials, log=self.log, extra_headers=extra_headers,
|
||||
tenant=tenant,
|
||||
credentials=credentials,
|
||||
user_agent=user_agent if user_agent is not None else tenant,
|
||||
log=self.log,
|
||||
extra_headers=extra_headers,
|
||||
)
|
||||
|
||||
self.builds = ADOBuildClient(self.http_client, self.log)
|
||||
|
|
|
@ -49,6 +49,7 @@ class ADOHTTPClient:
|
|||
|
||||
:param str tenant: The name of the ADO tenant to connect to
|
||||
:param Dict[str,str] extra_headers: Any extra headers which should be added to each request
|
||||
:param str user_agent: The user agent to set
|
||||
:param Tuple[str,str] credentials: The credentials which should be used for authentication
|
||||
:param logging.Logger log: The logger to use for logging
|
||||
"""
|
||||
|
@ -57,12 +58,14 @@ class ADOHTTPClient:
|
|||
tenant: str
|
||||
extra_headers: Dict[str, str]
|
||||
credentials: Tuple[str, str]
|
||||
_session: requests.Session
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
tenant: str,
|
||||
credentials: Tuple[str, str],
|
||||
user_agent: str,
|
||||
log: logging.Logger,
|
||||
extra_headers: Optional[Dict[str, str]] = None,
|
||||
) -> None:
|
||||
|
@ -73,6 +76,9 @@ class ADOHTTPClient:
|
|||
self.tenant = tenant
|
||||
self.credentials = credentials
|
||||
|
||||
self._session = requests.Session()
|
||||
self._session.headers.update({"User-Agent": f"simple_ado/{user_agent}"})
|
||||
|
||||
if extra_headers is None:
|
||||
self.extra_headers = {}
|
||||
else:
|
||||
|
@ -147,7 +153,7 @@ class ADOHTTPClient:
|
|||
:returns: The raw response object from the API
|
||||
"""
|
||||
headers = self.construct_headers(additional_headers=additional_headers)
|
||||
return requests.get(request_url, auth=self.credentials, headers=headers, stream=stream)
|
||||
return self._session.get(request_url, auth=self.credentials, headers=headers, stream=stream)
|
||||
|
||||
@retry(
|
||||
retry=retry_if_exception(_is_connection_failure),
|
||||
|
@ -185,7 +191,7 @@ class ADOHTTPClient:
|
|||
additional_headers["Content-Type"] = "application/json-patch+json"
|
||||
|
||||
headers = self.construct_headers(additional_headers=additional_headers)
|
||||
return requests.post(
|
||||
return self._session.post(
|
||||
request_url, auth=self.credentials, headers=headers, json=json_data, stream=stream
|
||||
)
|
||||
|
||||
|
@ -223,7 +229,9 @@ class ADOHTTPClient:
|
|||
additional_headers["Content-Type"] = "application/json-patch+json"
|
||||
|
||||
headers = self.construct_headers(additional_headers=additional_headers)
|
||||
return requests.patch(request_url, auth=self.credentials, headers=headers, json=json_data)
|
||||
return self._session.patch(
|
||||
request_url, auth=self.credentials, headers=headers, json=json_data
|
||||
)
|
||||
|
||||
@retry(
|
||||
retry=retry_if_exception(_is_connection_failure),
|
||||
|
@ -246,7 +254,9 @@ class ADOHTTPClient:
|
|||
:returns: The raw response object from the API
|
||||
"""
|
||||
headers = self.construct_headers(additional_headers=additional_headers)
|
||||
return requests.put(request_url, auth=self.credentials, headers=headers, json=json_data)
|
||||
return self._session.put(
|
||||
request_url, auth=self.credentials, headers=headers, json=json_data
|
||||
)
|
||||
|
||||
@retry(
|
||||
retry=retry_if_exception(_is_connection_failure),
|
||||
|
@ -264,7 +274,7 @@ class ADOHTTPClient:
|
|||
:returns: The raw response object from the API
|
||||
"""
|
||||
headers = self.construct_headers(additional_headers=additional_headers)
|
||||
return requests.delete(request_url, auth=self.credentials, headers=headers)
|
||||
return self._session.delete(request_url, auth=self.credentials, headers=headers)
|
||||
|
||||
@retry(
|
||||
retry=retry_if_exception(_is_connection_failure),
|
||||
|
@ -299,8 +309,7 @@ class ADOHTTPClient:
|
|||
with open(file_path, "rb") as file_handle:
|
||||
prepped.body = file_handle.read(file_size)
|
||||
|
||||
session = requests.Session()
|
||||
response: requests.Response = session.send(prepped)
|
||||
response: requests.Response = self._session.send(prepped)
|
||||
return response
|
||||
|
||||
def validate_response(self, response: requests.models.Response) -> None:
|
||||
|
|
Загрузка…
Ссылка в новой задаче