diff --git a/deepomatic/api/client.py b/deepomatic/api/client.py index e099eb8..b3bc902 100644 --- a/deepomatic/api/client.py +++ b/deepomatic/api/client.py @@ -28,16 +28,43 @@ from deepomatic.api.resources.task import Task from deepomatic.api.resources.account import Account -############################################################################### - -API_VERSION = 0.7 - -############################################################################### class Client(object): - def __init__(self, app_id=None, api_key=None, verify_ssl=None, check_query_parameters=True, host=None, version=API_VERSION, user_agent_suffix='', pool_maxsize=20): - self.http_helper = HTTPHelper(app_id, api_key, verify_ssl, host, version, check_query_parameters, user_agent_suffix, pool_maxsize) + 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`. + If `None`, try to retrieve it from the `DEEPOMATIC_APP_ID` environment variable. + If it fails raise a `DeepomaticException`. + :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. + If it fails raise a `DeepomaticException`. + :type api_key: string + :param verify_ssl (optional): whether to ask `requests` to verify the TLS/SSL certificates. + Defaults to `None`. + If `None` try to get it from the `DEEPOMATIC_API_VERIFY_TLS` environment variable (`0`: False, `1`: True). + If not found it is set to True. + :type verify_ssl: bool + :param host (optional): API root URL. + :type host: string + :param version (optional): API version. + :type version: string + :param user_agent_prefix (optional): Prefix the HTTP User-Agent. + It is recommended to declare your client via this parameter. Example: 'my-app/1.0.0'. + :type user_agent_prefix: string + :param user_agent_suffix (optional): Suffix the HTTP User-Agent. + :type user_agent_suffix: string + :param pool_maxsize (optional): Set `requests.adapters.HTTPAdapter.pool_maxsize` for concurrent calls. + Defaults to 20. + :type pool_maxsize: int + + :return: :class:`Client` object + :rtype: deepomatic.api.client.Client + """ + self.http_helper = HTTPHelper(*args, **kwargs) # /accounts diff --git a/deepomatic/api/http_helper.py b/deepomatic/api/http_helper.py index c5eb62c..69a7925 100644 --- a/deepomatic/api/http_helper.py +++ b/deepomatic/api/http_helper.py @@ -31,21 +31,22 @@ from six import string_types from deepomatic.api.exceptions import DeepomaticException, BadStatus -from deepomatic.api.version import __version__ +from deepomatic.api.version import __title__, __version__ API_HOST = 'https://api.deepomatic.com' +API_VERSION = 0.7 ############################################################################### class HTTPHelper(object): - def __init__(self, app_id, api_key, verify, host, version, check_query_parameters, user_agent_suffix='', pool_maxsize=20): + def __init__(self, app_id=None, api_key=None, verify_ssl=None, host=None, version=API_VERSION, check_query_parameters=True, user_agent_prefix='', user_agent_suffix='', pool_maxsize=20): """ Init the HTTP helper with API key and secret """ if host is None: host = os.getenv('DEEPOMATIC_API_URL', API_HOST) - if verify is None: + if verify_ssl is None: verify = os.getenv('DEEPOMATIC_API_VERIFY_TLS', '1') == '0' if app_id is None: app_id = os.getenv('DEEPOMATIC_APP_ID') @@ -65,14 +66,21 @@ def __init__(self, app_id, api_key, verify, host, version, check_query_parameter python_version = "{0}.{1}.{2}".format(sys.version_info.major, sys.version_info.minor, sys.version_info.micro) user_agent_params = { + 'package_title': __title__, 'package_version': __version__, 'requests_version': requests.__version__, 'python_version': python_version, 'platform': platform.platform() } - self.user_agent = 'deepomatic-api/{package_version} requests/{requests_version} python/{python_version} platform/{platform}\ + if user_agent_prefix: + self.user_agent = user_agent_prefix + ' ' + else: + self.user_agent = '' + + self.user_agent += '{package_title}/{package_version} requests/{requests_version} python/{python_version} platform/{platform}\ '.format(**user_agent_params) + if user_agent_suffix: self.user_agent += ' ' + user_agent_suffix @@ -81,6 +89,8 @@ def __init__(self, app_id, api_key, verify, host, version, check_query_parameter 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 = {