From 5672df1fb8bfc5229c24c50390e66091f3409490 Mon Sep 17 00:00:00 2001 From: riley-jenkins Date: Fri, 28 Jan 2022 12:22:56 -0700 Subject: [PATCH] Supporting a configurable request timeout configuration --- pydomo/Transport.py | 6 ++++-- pydomo/__init__.py | 42 ++++++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pydomo/Transport.py b/pydomo/Transport.py index 7aa06b2..8418070 100644 --- a/pydomo/Transport.py +++ b/pydomo/Transport.py @@ -13,12 +13,13 @@ class DomoAPITransport: serialization and deserialization of objects. """ - def __init__(self, client_id, client_secret, api_host, use_https, logger): + def __init__(self, client_id, client_secret, api_host, use_https, logger, request_timeout): self.apiHost = self._build_apihost(api_host, use_https) self.clientId = client_id self.clientSecret = client_secret self.logger = logger self._renew_access_token() + self.request_timeout = request_timeout @staticmethod def _build_apihost(host, use_https): @@ -68,7 +69,8 @@ def request(self, url, method, headers, params=None, body=None): self.logger.debug('{} {} {}'.format(method, url, body)) request_args = {'method': method, 'url': url, 'headers': headers, 'params': params, 'data': body, 'stream': True} - + if self.request_timeout: + request_args['timeout'] = self.request_timeout response = requests.request(**request_args) if response.status_code == requests.codes.UNAUTHORIZED: self._renew_access_token() diff --git a/pydomo/__init__.py b/pydomo/__init__.py index 7f87a3b..ca82423 100644 --- a/pydomo/__init__.py +++ b/pydomo/__init__.py @@ -80,11 +80,13 @@ def __init__(self, client_id, client_secret, api_host='api.domo.com', **kwargs): else: self.logger = parent_logger + timeout = kwargs.get('request_timeout', None) + if kwargs.get('log_level'): self.logger.setLevel(kwargs['log_level']) self.logger.debug("\n" + DOMO + "\n") - self.transport = DomoAPITransport(client_id, client_secret, api_host, kwargs.get('use_https', True), self.logger) + self.transport = DomoAPITransport(client_id, client_secret, api_host, kwargs.get('use_https', True), self.logger, request_timeout = timeout) self.datasets = DataSetClient(self.transport, self.logger) self.groups = GroupClient(self.transport, self.logger) self.pages = PageClient(self.transport, self.logger) @@ -136,7 +138,7 @@ def ds_list(self, df_output = True, per_page=50, offset=0, limit=0): - `per_page`: results per page. Default 50 (int) - `offset`: offset if you need to paginate results. Default 0 (int) - `limit`: max ouput to return. If 0 then return all results on page. Default 0 (int) - + :Returns: list or pandas dataframe depending on parameters @@ -147,7 +149,7 @@ def ds_list(self, df_output = True, per_page=50, offset=0, limit=0): else: out = DataFrame(list(l)) return out - + def ds_query(self, dataset_id, query, return_data=True): """ Evaluate query and return dataset in a dataframe @@ -160,7 +162,7 @@ def ds_query(self, dataset_id, query, return_data=True): - `dataset_id`: id of a dataset (str) - `query`: query object (dict) - `return_data`: should the result be a dataframe. Default True (Boolean) - + :Returns: dict or pandas dataframe depending on parameters """ @@ -179,10 +181,10 @@ def ds_get(self, dataset_id): :Parameters: - `dataset_id`: id of a dataset (str) - + :Returns: pandas dataframe - """ + """ csv_download = self.datasets.data_export(dataset_id, include_csv_header=True) content = StringIO(csv_download) @@ -236,14 +238,14 @@ def pdp_create(self, dataset_id, pdp_request): , "not": false } ], "users": [ 27 ],"groups": [ ]} :Parameters: - - `dataset_id`: id of the dataset PDP will be applied to (String) Required + - `dataset_id`: id of the dataset PDP will be applied to (String) Required Policy Object: - `name`: Name of the Policy (String) Required - - `filters[].column`: Name of the column to filter on (String) Required + - `filters[].column`: Name of the column to filter on (String) Required - `filters[].not`: Determines if NOT is applied to the filter operation (Boolean) Required - - `filters[].operator`: Matching operator (EQUALS) (String) Required - - `filters[].values[]`: Values to filter on (String) Required - - `type`: Type of policy (user or system) (String) Required + - `filters[].operator`: Matching operator (EQUALS) (String) Required + - `filters[].values[]`: Values to filter on (String) Required + - `type`: Type of policy (user or system) (String) Required - `users`: List of user IDs the policy applies to (array) Required - `groups`: List of group IDs the policy applies to (array) Required """ @@ -256,7 +258,7 @@ def pdp_delete(self, dataset_id, policy_id): >>> domo.pdp_delete('4405ff58-1957-45f0-82bd-914d989a3ea3', 35) :Parameters: - - `dataset_id`: id of the dataset PDP will be applied to (String) Required + - `dataset_id`: id of the dataset PDP will be applied to (String) Required - `policy_id`: id of the policy to delete (String) Required """ return self.datasets.delete_pdp(dataset_id, policy_id) @@ -271,7 +273,7 @@ def pdp_list(self, dataset_id, df_output = True): :Parameters: - `dataset_id`: id of dataset with PDP policies (str) Required - `df_output`: should the result be a dataframe. Default True (Boolean) - + :Returns: list or pandas dataframe depending on parameters @@ -301,15 +303,15 @@ def pdp_update(self, dataset_id, policy_id, policy_update): , "not": false } ], "users": [ 27 ],"groups": [ ]} :Parameters: - - `dataset_id`: id of the dataset PDP will be applied to (String) Required - - `policy_id`: id of the PDP pollicy that will be updated (String) Required + - `dataset_id`: id of the dataset PDP will be applied to (String) Required + - `policy_id`: id of the PDP pollicy that will be updated (String) Required Policy Object: - `name`: Name of the Policy (String) Required - - `filters[].column`: Name of the column to filter on (String) Required + - `filters[].column`: Name of the column to filter on (String) Required - `filters[].not`: Determines if NOT is applied to the filter operation (Boolean) Required - - `filters[].operator`: Matching operator (EQUALS) (String) Required - - `filters[].values[]`: Values to filter on (String) Required - - `type`: Type of policy (user or system) (String) Required + - `filters[].operator`: Matching operator (EQUALS) (String) Required + - `filters[].values[]`: Values to filter on (String) Required + - `type`: Type of policy (user or system) (String) Required - `users`: List of user IDs the policy applies to (array) Required - `groups`: List of group IDs the policy applies to (array) Required """ @@ -604,7 +606,7 @@ def accounts_list(self): - `per_page`: results per page. Default 50 (int) - `offset`: offset if you need to paginate results. Default 0 (int) - `limit`: max ouput to return. If 0 then return all results on page. Default 0 (int) - + :returns: - A list of dicts (with nesting possible)