From 5adae987df3f52e3b4b62fa9a8f60d202a8b3ffa Mon Sep 17 00:00:00 2001 From: Lubomir Sokolovsky Date: Thu, 1 Feb 2024 16:00:51 +0100 Subject: [PATCH] Refactored client --- .../azure/devops/azure_devops_client.py | 24 +++++++++++++++++++ azure-devops/azure/devops/client.py | 9 +++---- .../azure/devops/client_configuration.py | 17 ------------- azure-devops/azure/devops/connection.py | 13 ++++------ azure-devops/azure/devops/exceptions.py | 15 ++++-------- 5 files changed, 36 insertions(+), 42 deletions(-) create mode 100644 azure-devops/azure/devops/azure_devops_client.py delete mode 100644 azure-devops/azure/devops/client_configuration.py diff --git a/azure-devops/azure/devops/azure_devops_client.py b/azure-devops/azure/devops/azure_devops_client.py new file mode 100644 index 00000000..2d0816f2 --- /dev/null +++ b/azure-devops/azure/devops/azure_devops_client.py @@ -0,0 +1,24 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- + +from azure.core import PipelineClient +from azure.core.pipeline.policies import UserAgentPolicy, HeadersPolicy +from .version import VERSION + + +class AzureDevOpsClient(PipelineClient): + def __init__(self, base_url=None, credentials=None, user_agent=None): + if not base_url: + raise ValueError('base_url is required.') + if not credentials: + raise ValueError('credentials is required.') + base_url = base_url.rstrip('/') + user_agent_policy = UserAgentPolicy('azure-devops/{}'.format(VERSION)) + if user_agent is not None: + user_agent_policy.add_user_agent(user_agent) + headers_policy = HeadersPolicy() + headers_policy.add_header('Authentication', credentials) + policies = [user_agent_policy, headers_policy] + super(AzureDevOpsClient, self).__init__(base_url=base_url, policies=policies) diff --git a/azure-devops/azure/devops/client.py b/azure-devops/azure/devops/client.py index dbdd9ad8..96089a9d 100644 --- a/azure-devops/azure/devops/client.py +++ b/azure-devops/azure/devops/client.py @@ -11,10 +11,9 @@ import uuid from azure.core.rest import HttpRequest, HttpResponse -from azure.core.rest.exceptions import DeserializationError, SerializationError -from azure.core.rest.service_client import ServiceClient +from azure.core.exceptions import DeserializationError, SerializationError from .exceptions import AzureDevOpsAuthenticationError, AzureDevOpsClientRequestError, AzureDevOpsServiceError -from .client_configuration import ClientConfiguration +from .azure_devops_client import AzureDevOpsClient from . import _models from ._file_cache import OPTIONS_CACHE as OPTIONS_FILE_CACHE @@ -29,9 +28,7 @@ class Client(object): """ def __init__(self, base_url=None, creds=None): - self.config = ClientConfiguration(base_url) - self.config.credentials = creds - self._client = ServiceClient(creds, config=self.config) + self._client = AzureDevOpsClient(base_url=base_url, credentials=creds) _base_client_models = {k: v for k, v in _models.__dict__.items() if isinstance(v, type)} self._base_deserialize = Deserializer(_base_client_models) self._base_serialize = Serializer(_base_client_models) diff --git a/azure-devops/azure/devops/client_configuration.py b/azure-devops/azure/devops/client_configuration.py deleted file mode 100644 index 6bdc47e3..00000000 --- a/azure-devops/azure/devops/client_configuration.py +++ /dev/null @@ -1,17 +0,0 @@ -# -------------------------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for license information. -# -------------------------------------------------------------------------------------------- - -from azure.core.rest import Configuration -from .version import VERSION - - -class ClientConfiguration(Configuration): - def __init__(self, base_url=None): - if not base_url: - raise ValueError('base_url is required.') - base_url = base_url.rstrip('/') - super(ClientConfiguration, self).__init__(base_url) - self.add_user_agent('azure-devops/{}'.format(VERSION)) - self.additional_headers = {} diff --git a/azure-devops/azure/devops/connection.py b/azure-devops/azure/devops/connection.py index 985c8732..9d38a36c 100644 --- a/azure-devops/azure/devops/connection.py +++ b/azure-devops/azure/devops/connection.py @@ -5,9 +5,8 @@ import logging -from azure.core.rest.service_client import ServiceClient from ._file_cache import RESOURCE_CACHE as RESOURCE_FILE_CACHE -from .client_configuration import ClientConfiguration +from .azure_devops_client import AzureDevOpsClient from .exceptions import AzureDevOpsClientRequestError from .released.client_factory import ClientFactory from .v7_2.location.location_client import LocationClient @@ -21,16 +20,12 @@ class Connection(object): """Connection. """ - def __init__(self, base_url=None, creds=None, user_agent=None): - self._config = ClientConfiguration(base_url) - self._config.credentials = creds + def __init__(self, base_url=None, credentials=None, user_agent=None): self._addition_user_agent = user_agent - if user_agent is not None: - self._config.add_user_agent(user_agent) - self._client = ServiceClient(creds, self._config) + self._client = AzureDevOpsClient(base_url=base_url, credentials=credentials, user_agent=user_agent) self._client_cache = {} self.base_url = base_url - self._creds = creds + self._creds = credentials self._resource_areas = None self.clients = ClientFactory(self) self.clients_v7_2 = ClientFactoryV7_2(self) diff --git a/azure-devops/azure/devops/exceptions.py b/azure-devops/azure/devops/exceptions.py index 6589b97c..b8ce202f 100644 --- a/azure-devops/azure/devops/exceptions.py +++ b/azure-devops/azure/devops/exceptions.py @@ -3,22 +3,17 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- -from azure.core.rest.exceptions import ( - ClientException, - ClientRequestError, - AuthenticationError, +from azure.core.exceptions import ( + ClientAuthenticationError, + ServiceResponseError ) -class AzureDevOpsClientError(ClientException): +class AzureDevOpsAuthenticationError(ClientAuthenticationError): pass -class AzureDevOpsAuthenticationError(AuthenticationError): - pass - - -class AzureDevOpsClientRequestError(ClientRequestError): +class AzureDevOpsClientRequestError(ServiceResponseError): pass