Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for Projects #55

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
36 changes: 35 additions & 1 deletion pipedrive/client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import time
from urllib.parse import urlencode

import requests

from pipedrive import exceptions
from pipedrive.activities import Activities
from pipedrive.deals import Deals
from pipedrive.projects import Projects
from pipedrive.filters import Filters
from pipedrive.leads import Leads
from pipedrive.items import Items
Expand All @@ -31,6 +33,7 @@ def __init__(self, client_id=None, client_secret=None, domain=None):
self.api_token = None
self.activities = Activities(self)
self.deals = Deals(self)
self.projects = Projects(self)
self.filters = Filters(self)
self.leads = Leads(self)
self.items = Items(self)
Expand All @@ -44,11 +47,15 @@ def __init__(self, client_id=None, client_secret=None, domain=None):
self.stages = Stages(self)
self.users = Users(self)
self.webhooks = Webhooks(self)
self.requests_session = requests.Session()

if domain:
if not domain.endswith("/"):
domain += "/"
self.BASE_URL = domain + "v1/"
else:
self.BASE_URL = self.BASE_URL + "v1/"


def authorization_url(self, redirect_uri, state=None):
params = {
Expand Down Expand Up @@ -116,7 +123,34 @@ def _request(self, method, url, headers=None, params=None, **kwargs):
_headers.update(headers)
if params:
_params.update(params)
return self._parse(requests.request(method, url, headers=_headers, params=_params, **kwargs))

number_of_retries = kwargs.get('number_of_retries', 5)
intervaltime = kwargs.get('intervaltime', 2500)

# remove number of retries and intervaltime from kwargs, otherwise the requests call will fail.
if 'number_of_retries' in kwargs:
del kwargs['number_of_retries']
if 'intervaltime' in kwargs:
del kwargs['intervaltime']

if number_of_retries:
while number_of_retries > 0:
try:
response = self._parse(self.requests_session.request(method, url, headers=_headers, params=_params, **kwargs))
# No except, response is ok, return it.
return response
except (exceptions.BadRequestError, exceptions.UnauthorizedError, exceptions.NotFoundError,
exceptions.UnsupportedMediaTypeError, exceptions.UnprocessableEntityError,
exceptions.NotImplementedError) as e:
# Do not retry, just return the response.
raise e
except (exceptions.ForbiddenError, exceptions.InternalServerError, exceptions.ServiceUnavailableError,
exceptions.UnknownError, exceptions.TooManyRequestsError):
# Retry! There is hope.
number_of_retries -= 1
time.sleep(intervaltime / 1000.0)
else:
return self._parse(self.requests_session.request(method, url, headers=_headers, params=_params, **kwargs))

def _parse(self, response):
status_code = response.status_code
Expand Down
77 changes: 77 additions & 0 deletions pipedrive/projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Projects(object):
def __init__(self, client):
self._client = client

def get_project(self, project_id, **kwargs):
url = "projects/{}".format(project_id)
return self._client._get(self._client.BASE_URL + url, **kwargs)

def get_all_projects(self, params=None, **kwargs):
url = "projects"
return self._client._get(self._client.BASE_URL + url, params=params, **kwargs)

def get_all_projects_with_filter(self, filter_id, params=None, **kwargs):
url = "projects?filter_id={}".format(filter_id)
return self._client._get(self._client.BASE_URL + url, params=params, **kwargs)

def create_project(self, data, **kwargs):
url = "projects"
return self._client._post(self._client.BASE_URL + url, json=data, **kwargs)

def update_project(self, project_id, data, **kwargs):
url = "projects/{}".format(project_id)
return self._client._put(self._client.BASE_URL + url, json=data, **kwargs)

def delete_project(self, project_id, **kwargs):
url = "projects/{}".format(project_id)
return self._client._delete(self._client.BASE_URL + url, **kwargs)

def duplicate_project(self, project_id, **kwargs):
url = "projects/{}/duplicate".format(project_id)
return self._client._post(self._client.BASE_URL + url, **kwargs)

def get_project_details(self, project_id, **kwargs):
url = "projects/{}".format(project_id)
return self._client._get(self._client.BASE_URL + url, **kwargs)

def search_projects(self, params=None, **kwargs):
url = "projects/search"
return self._client._get(self._client.BASE_URL + url, params=params, **kwargs)

def get_project_plan(self, project_id, **kwargs):
url = "projects/{}/plan".format(project_id)
return self._client._get(self._client.BASE_URL + url, **kwargs)

def get_project_groups(self, project_id, **kwargs):
url = "projects/{}/groups".format(project_id)
return self._client._get(self._client.BASE_URL + url, **kwargs)

def get_project_tasks(self, project_id, **kwargs):
url = "projects/{}/tasks".format(project_id)
return self._client._get(self._client.BASE_URL + url, **kwargs)

def get_project_activities(self, project_id, **kwargs):
url = "projects/{}/activities".format(project_id)
return self._client._get(self._client.BASE_URL + url, **kwargs)

def get_project_fields(self, params=None, **kwargs):
url = "projectFields"
return self._client._get(self._client.BASE_URL + url, params=params, **kwargs)

def get_all_projects_boards(self, params=None, **kwargs):
url = "projects/boards"
return self._client._get(self._client.BASE_URL + url, params=params, **kwargs)

def get_board(self, board_id, **kwargs):
url = "projects/boards/{}".format(board_id)
return self._client._get(self._client.BASE_URL + url, **kwargs)

def get_project_phases(self, board_id, **kwargs):
url = "projects/phases?board_id={}".format(board_id)
return self._client._get(self._client.BASE_URL + url, **kwargs)

def get_phase(self, phase_id, **kwargs):
url = "projects/phases/{}".format(phase_id)
return self._client._get(self._client.BASE_URL + url, **kwargs)


2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pipedrive-python-lib"
version = "1.2.3"
version = "1.2.5"
description = "API wrapper for Pipedrive written in Python"
authors = ["Miguel Ferrer <[email protected]>"]
license = "MIT"
Expand Down