From b5734b13487a883cbfb4f5686a09576d753f8e81 Mon Sep 17 00:00:00 2001 From: Alexis Jacob Date: Tue, 1 Oct 2019 18:56:36 +0200 Subject: [PATCH 1/2] app_id is now optional --- README.md | 3 ++- deepomatic/api/client.py | 4 ++-- deepomatic/api/http_helper.py | 27 +++++++++++++++++---------- demo.py | 3 +-- tests/test_client.py | 1 - 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b235ff0..d86143d 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,9 @@ Does not make any call to the server. from deepomatic.api.client import Client # If you don't have your credentials, contact us at support@deepomatic.com -client = Client(app_id, api_key, user_agent_prefix='my-app/1.0.0') +client = Client(api_key=api_key, user_agent_prefix='my-app/1.0.0') ``` +> app_id is now optional but if you don't put it, you have to ensure that the api_key is a named parameter to the function ### Client methods diff --git a/deepomatic/api/client.py b/deepomatic/api/client.py index d4d181a..4079e1f 100644 --- a/deepomatic/api/client.py +++ b/deepomatic/api/client.py @@ -36,9 +36,9 @@ def __init__(self, *args, **kwargs): """ Constructs a Client to send requests to the Deepomatic API. - :param app_id: App ID for authentication. Defaults to `None`. + :param app_id: App ID for authentication. Defaults to `None`. If `None`, try to retrieve it from the `DEEPOMATIC_APP_ID` environment variable. - If it fails raise a `DeepomaticException`. + Important: this parameter is deprecated and will be removed in a near future. :type app_id: string :param api_key: API key for authentication. Defaults to `None`. If `None` try to retrieve it from the `DEEPOMATIC_API_KEY` environment variable. diff --git a/deepomatic/api/http_helper.py b/deepomatic/api/http_helper.py index 7508d0e..a816baf 100644 --- a/deepomatic/api/http_helper.py +++ b/deepomatic/api/http_helper.py @@ -75,9 +75,9 @@ def __init__(self, app_id=None, api_key=None, verify_ssl=None, app_id = os.getenv('DEEPOMATIC_APP_ID') if api_key is None: api_key = os.getenv('DEEPOMATIC_API_KEY') - if app_id is None or api_key is None: - raise DeepomaticException("Please specify 'app_id' and 'api_key' either by passing those values to the client" - " or by defining the DEEPOMATIC_APP_ID and DEEPOMATIC_API_KEY environment variables.") + if api_key is None: + raise DeepomaticException("Please specify 'api_key' either by passing those values to the client" + " or by defining the DEEPOMATIC_API_KEY environment variables.") if not isinstance(version, string_types): version = 'v%g' % version @@ -111,7 +111,7 @@ def __init__(self, app_id=None, api_key=None, verify_ssl=None, self.user_agent = ' '.join(user_agent_list).format(**user_agent_params) self.api_key = str(api_key) - self.app_id = str(app_id) + self.app_id = str(app_id) if app_id else None self.verify = verify self.host = host self.resource_prefix = host + version @@ -119,18 +119,25 @@ def __init__(self, app_id=None, api_key=None, verify_ssl=None, # This is only used in mixins, this should not stay here self.check_query_parameters = check_query_parameters - headers = { - 'User-Agent': self.user_agent, - 'X-APP-ID': self.app_id, - 'X-API-KEY': self.api_key, - } self.session = requests.Session() - self.session.headers.update(headers) + self.session.headers.update(self.default_headers()) # Use pool_maxsize to cache connections for the same host adapter = requests.adapters.HTTPAdapter(pool_maxsize=pool_maxsize) self.session.mount('http://', adapter) self.session.mount('https://', adapter) + def default_headers(self): + """ + Return the default headers, can be overridden + """ + headers = { + 'User-Agent': self.user_agent, + 'X-API-KEY': self.api_key, + } + if self.app_id: + headers['X-APP-ID'] = self.app_id, + return headers + def setup_headers(self, headers=None, content_type=None): """ Build additional headers diff --git a/demo.py b/demo.py index 7633cfe..5dc2829 100644 --- a/demo.py +++ b/demo.py @@ -45,9 +45,8 @@ def demo(client=None): # In both ways `user_agent_prefix` parameter is optional but recommended to identify your app to the API # Here we actually use a mix of those two methods to illustrate: if client is None: - app_id = os.getenv('DEEPOMATIC_APP_ID') api_key = os.getenv('DEEPOMATIC_API_KEY') - client = Client(app_id, api_key, user_agent_prefix='{}-demo/{}'.format(__title__, __version__)) + client = Client(api_key=api_key, user_agent_prefix='{}-demo/{}'.format(__title__, __version__)) ################### # Public networks # diff --git a/tests/test_client.py b/tests/test_client.py index 5b883d7..1ff137d 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -174,7 +174,6 @@ def test_headers(self, client): '{}-tests/{} {}-python-client/{}'.format(__title__, __version__, __title__, __version__)) assert 'platform/' in session_headers['User-Agent'] assert 'python/' in session_headers['User-Agent'] - assert session_headers['X-APP-ID'] assert session_headers['X-API-KEY'] headers = http_helper.setup_headers(headers={'Hello': 'World'}, From d973d0c282df0f90be6043f7a6a63a4f1a96cb92 Mon Sep 17 00:00:00 2001 From: Alexis Jacob Date: Thu, 3 Oct 2019 13:40:17 +0200 Subject: [PATCH 2/2] Update http_helper.py --- deepomatic/api/http_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deepomatic/api/http_helper.py b/deepomatic/api/http_helper.py index a816baf..0a14d3d 100644 --- a/deepomatic/api/http_helper.py +++ b/deepomatic/api/http_helper.py @@ -135,7 +135,7 @@ def default_headers(self): 'X-API-KEY': self.api_key, } if self.app_id: - headers['X-APP-ID'] = self.app_id, + headers['X-APP-ID'] = self.app_id return headers def setup_headers(self, headers=None, content_type=None):