Skip to content

Commit

Permalink
docstring and PR feedback updates
Browse files Browse the repository at this point in the history
  • Loading branch information
schaabs committed Aug 6, 2018
1 parent e9771ee commit 12dffec
Showing 1 changed file with 57 additions and 10 deletions.
67 changes: 57 additions & 10 deletions azure-keyvault/azure/keyvault/key_vault_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def __init__(

class KeyVaultClient(MultiApiClientMixin):
"""The key vault client performs cryptographic key operations and vault operations against the Key Vault service.
Implementation depends on the API version:
* 2016-10-01: :class:`v2016_10_01.KeyVaultClient<azure.keyvault.v2016_10_01.KeyVaultClient>`
* 7.0: :class:`v7_0.KeyVaultClient<azure.mgmt.keyvault.v7_0.KeyVaultClient>`
:ivar config: Configuration for client.
:vartype config: KeyVaultClientConfiguration
Expand Down Expand Up @@ -76,6 +80,7 @@ class KeyVaultClient(MultiApiClientMixin):
def __init__(self, credentials, api_version=None, profile=KnownProfiles.default):
self.config = KeyVaultClientConfiguration(credentials)
self._client_impls = {}
self._entered = False
super(KeyVaultClient, self).__init__(
api_version=api_version,
profile=profile
Expand All @@ -93,54 +98,97 @@ def __init__(self, credentials, api_version=None, profile=KnownProfiles.default)

@property
def models(self):
"""Module depends on the API version:
* 2016-10-01: :mod:`v2016_10_01.models<azure.keyvault.v2016_10_01.models>`
* 7.0: :mod:`v7_0.models<azure.keyvault.v7_0.models>`
"""
api_version = self._get_api_version(None)

if api_version == v7_0_VERSION:
from azure.keyvault.v7_0 import models as implModels
elif api_version == v2016_10_01_VERSION:
from azure.keyvault.v2016_10_01 import models as implModels
else:
raise ValueError('unrecognized api_version')
raise NotImplementedError("APIVersion {} is not available".format(api_version))
return implModels

def _get_client_impl(self):
"""
Get the versioned client implementation corresponding to the current profile.
:return: The versioned client implementation.
"""
api_version = self._get_api_version(None)
if api_version not in self._client_impls:
self._client_impls[api_version] = self._create_client_impl(api_version)
self._create_client_impl(api_version)
return self._client_impls[api_version]

def _create_client_impl(self, api_version):
"""
Creates the client implementation corresponding to the specifeid api_version.
:param api_version:
:return:
"""
if api_version == v7_0_VERSION:
from azure.keyvault.v7_0 import KeyVaultClient as ImplClient
elif api_version == v2016_10_01_VERSION:
from azure.keyvault.v2016_10_01 import KeyVaultClient as ImplClient
else:
raise ValueError('unrecognized api_version')
raise NotImplementedError("APIVersion {} is not available".format(api_version))

impl = ImplClient(credentials=self._credentials)
if hasattr(impl, '__enter__'):
impl.__enter__()
impl.config = self.config

# if __enter__ has previously been called and the impl client has __enter__ defined we need to call it
if self._entered and hasattr(impl, '__enter__'):
impl.__enter__()

self._client_impls[api_version] = impl
return impl

def __enter__(self, *args, **kwargs):
"""
Calls __enter__ on all client implementations which support it
:param args: positional arguments to relay to client implementations of __enter__
:param kwargs: keyword arguments to relay to client implementations of __enter__
:return: returns the current KeyVaultClient instance
"""
for _, impl in self._client_impls.items():
if hasattr(impl, '__enter__'):
impl.__enter__(*args, **kwargs)
# mark the current KeyVaultClient as _entered so that client implementations instantiated
# subsequently will also have __enter__ called on them as appropriate
self._entered = True
return self

def __exit__(self, *args, **kwargs):
"""
Calls __exit__ on all client implementations which support it
:param args: positional arguments to relay to client implementations of __enter__
:param kwargs: keyword arguments to relay to client implementations of __enter__
:return: returns the current KeyVaultClient instance
"""
for _, impl in self._client_impls.items():
if hasattr(impl, '__exit__'):
impl.__exit__(*args, **kwargs)
return self

def __getattr__(self, name, *args, **kwargs):
def __getattr__(self, name):
"""
In the case that the attribute is not defined on the custom KeyVaultClient. Attempt to get
the attribute from the versioned client implementation corresponding to the current profile.
:param name: Name of the attribute retrieve from the current versioned client implementation
:return: The value of the specified attribute on the current client implementation.
"""
impl = self._get_client_impl()
return getattr(impl, name)

def __setattr__(self, name, attr):
"""
Sets the specified attribute either on the custom KeyVaultClient or the current underlying implementation.
:param name: Name of the attribute to set
:param attr: Value of the attribute to set
:return: None
"""
if self._init_complete and not hasattr(self, name):
impl = self._get_client_impl()
setattr(impl, name, attr)
Expand Down Expand Up @@ -179,7 +227,6 @@ def get_pending_certificate_signing_request(self, vault_base_url, certificate_na

# Construct headers
header_parameters = {}
header_parameters['Content-Type'] = 'application/json; charset=utf-8'
header_parameters['Accept'] = 'application/pkcs10'
if self.config.generate_client_request_id:
header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
Expand All @@ -189,16 +236,16 @@ def get_pending_certificate_signing_request(self, vault_base_url, certificate_na
header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')

# Construct and send request
request = self._client.get(url, query_parameters)
response = self._client.send(request, header_parameters, **operation_config)
request = self._client.get(url, query_parameters, header_parameters)
response = self._client.send(request, stream=False, **operation_config)

if response.status_code not in [200]:
raise self.models.KeyVaultErrorException(self._deserialize, response)

deserialized = None

if response.status_code == 200:
deserialized = response.content
deserialized = response.body() if hasattr(response, 'body') else response.content

if raw:
client_raw_response = ClientRawResponse(deserialized, response)
Expand Down

0 comments on commit 12dffec

Please sign in to comment.