From f77de49fe64620622b5412e32da89a17f171a538 Mon Sep 17 00:00:00 2001 From: Rohan Shah Date: Wed, 2 May 2018 15:32:53 -0700 Subject: [PATCH 1/2] add api versioning --- plaid/client.py | 10 ++++++++-- plaid/requester.py | 26 ++++++++++++++++++++------ tests/integration/util.py | 3 ++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/plaid/client.py b/plaid/client.py index 644526518..911a37706 100644 --- a/plaid/client.py +++ b/plaid/client.py @@ -33,7 +33,8 @@ def __init__(self, public_key, environment, suppress_warnings=False, - timeout=DEFAULT_TIMEOUT): + timeout=DEFAULT_TIMEOUT, + api_version=None): ''' Initialize a client with credentials. @@ -52,6 +53,7 @@ def __init__(self, self.environment = environment self.suppress_warnings = suppress_warnings self.timeout = timeout + self.api_version = api_version if self.environment == 'development' and not self.suppress_warnings: warnings.warn(''' @@ -95,8 +97,12 @@ def post_public_key(self, path, data): return self._post(path, post_data) def _post(self, path, data): + headers = {} + if self.api_version is not None: + headers = {'Plaid-Version': self.api_version} return post_request( urljoin('https://' + self.environment + '.plaid.com', path), data=data, - timeout=self.timeout + timeout=self.timeout, + headers=headers, ) diff --git a/plaid/requester.py b/plaid/requester.py index 0b0ec1e69..bf091867b 100644 --- a/plaid/requester.py +++ b/plaid/requester.py @@ -17,15 +17,19 @@ JSONDecodeError = ValueError -def _requests_http_request(url, method, data, timeout=DEFAULT_TIMEOUT): +def _requests_http_request( + url, + method, + data, + headers, + timeout=DEFAULT_TIMEOUT): normalized_method = method.lower() + headers.update({'User-Agent': 'Plaid Python v{}'.format(__version__)}) if normalized_method in ALLOWED_METHODS: return getattr(requests, normalized_method)( url, json=data, - headers={ - 'User-Agent': 'Plaid Python v{}'.format(__version__), - }, + headers=headers, timeout=timeout, ) else: @@ -34,8 +38,18 @@ def _requests_http_request(url, method, data, timeout=DEFAULT_TIMEOUT): ) -def http_request(url, method=None, data=None, timeout=DEFAULT_TIMEOUT): - response = _requests_http_request(url, method, data or {}, timeout) +def http_request( + url, + method=None, + data=None, + timeout=DEFAULT_TIMEOUT, + headers=None): + response = _requests_http_request( + url, + method, + data or {}, + headers or {}, + timeout) try: response_body = json.loads(response.text) except JSONDecodeError: diff --git a/tests/integration/util.py b/tests/integration/util.py index 346cdb557..810081507 100644 --- a/tests/integration/util.py +++ b/tests/integration/util.py @@ -10,7 +10,8 @@ def create_client(): return Client(os.environ['CLIENT_ID'], os.environ['SECRET'], os.environ['PUBLIC_KEY'], - 'sandbox') + 'sandbox', + api_version="2017-03-08") CREDENTIALS = { 'username': 'user_good', From aafd3e6ae11700fb3e5c86ae96e09826e187064a Mon Sep 17 00:00:00 2001 From: Rohan Shah Date: Wed, 2 May 2018 16:27:40 -0700 Subject: [PATCH 2/2] make arguments consistent between http_request and _requests_http_request --- plaid/requester.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plaid/requester.py b/plaid/requester.py index bf091867b..5cc081ee8 100644 --- a/plaid/requester.py +++ b/plaid/requester.py @@ -42,8 +42,8 @@ def http_request( url, method=None, data=None, - timeout=DEFAULT_TIMEOUT, - headers=None): + headers=None, + timeout=DEFAULT_TIMEOUT): response = _requests_http_request( url, method,