Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions deepomatic/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 17 additions & 10 deletions deepomatic/api/http_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/those values/it/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in #69

" or by defining the DEEPOMATIC_API_KEY environment variables.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/variables/variable/

Copy link
Contributor

@maingoh maingoh Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in #69


if not isinstance(version, string_types):
version = 'v%g' % version
Expand Down Expand Up @@ -111,26 +111,33 @@ 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

# 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
Expand Down
3 changes: 1 addition & 2 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand Down
1 change: 0 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand Down