From b919ef0569607e95ee269fafb7485599de8810a5 Mon Sep 17 00:00:00 2001 From: Miguel Ferrer Date: Mon, 16 Sep 2019 11:55:22 -0500 Subject: [PATCH] Adds support to API token Bump ver. 1.1.0 --- README.md | 18 ++++++++++++++++-- pipedrive/client.py | 24 ++++++++++++++++++++---- setup.py | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4dc1887..91928ac 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ pip install pipedrive-python-lib ## Usage +### Using this library with OAuth 2.0 + #### Client instantiation ``` from pipedrive.client import Client @@ -16,8 +18,6 @@ from pipedrive.client import Client client = Client('CLIENT_ID', 'CLIENT_SECRET') ``` -### OAuth 2.0 - #### Get authorization url ``` url = client.authorization_url('REDIRECT_URL') @@ -38,6 +38,20 @@ client.set_access_token('ACCESS_TOKEN') token = client.refresh_token('REFRESH_TOKEN') ``` +### Using this library with API Token + +#### Client instantiation +``` +from pipedrive.client import Client + +client = Client(domain='https://companydomain.pipedrive.com/') +``` + +#### Set API token in the library +``` +client.set_api_token('API_TOKEN') +``` + ### Activities API docs: https://developers.pipedrive.com/docs/api/v1/#!/Activities diff --git a/pipedrive/client.py b/pipedrive/client.py index a93d979..a219105 100644 --- a/pipedrive/client.py +++ b/pipedrive/client.py @@ -20,10 +20,11 @@ class Client: BASE_URL = 'https://api-proxy.pipedrive.com/' OAUTH_BASE_URL = 'https://oauth.pipedrive.com/oauth/' - def __init__(self, client_id, client_secret): + def __init__(self, client_id=None, client_secret=None, domain=None): self.client_id = client_id self.client_secret = client_secret self.access_token = None + self.api_token = None self.activities = Activities(self) self.deals = Deals(self) self.filters = Filters(self) @@ -36,6 +37,11 @@ def __init__(self, client_id, client_secret): self.users = Users(self) self.webhooks = Webhooks(self) + if domain: + if not domain.endswith('/'): + domain += '/' + self.BASE_URL = domain + 'v1/' + def authorization_url(self, redirect_uri, state=None): params = { 'client_id': self.client_id, @@ -65,6 +71,9 @@ def refresh_token(self, refresh_token): def set_access_token(self, access_token): self.access_token = access_token + def set_api_token(self, api_token): + self.api_token = api_token + def _get(self, url, **kwargs): return self._request('get', url, **kwargs) @@ -77,11 +86,18 @@ def _put(self, url, **kwargs): def _delete(self, url, **kwargs): return self._request('delete', url, **kwargs) - def _request(self, method, url, headers=None, **kwargs): - _headers = {'Authorization': 'Bearer {}'.format(self.access_token)} + def _request(self, method, url, headers=None, params=None, **kwargs): + _headers = {} + _params = {} + if self.access_token: + _headers['Authorization'] = 'Bearer {}'.format(self.access_token) + if self.api_token: + _params['api_token'] = self.api_token if headers: _headers.update(headers) - return self._parse(requests.request(method, url, headers=_headers, **kwargs)) + if params: + _params.update(params) + return self._parse(requests.request(method, url, headers=_headers, params=_params, **kwargs)) def _parse(self, response): status_code = response.status_code diff --git a/setup.py b/setup.py index 174e5e5..9925400 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ def read(fname): setup(name='pipedrive-python-lib', - version='1.0.0', + version='1.1.0', description='API wrapper for Pipedrive written in Python', long_description=read('README.md'), long_description_content_type="text/markdown",