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
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ pip install pipedrive-python-lib

## Usage

### Using this library with OAuth 2.0

#### Client instantiation
```
from pipedrive.client import Client

client = Client('CLIENT_ID', 'CLIENT_SECRET')
```

### OAuth 2.0

#### Get authorization url
```
url = client.authorization_url('REDIRECT_URL')
Expand All @@ -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
Expand Down
24 changes: 20 additions & 4 deletions pipedrive/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down