Initial commit
This commit is contained in:
0
invoice_ninja/endpoints/__init__.py
Normal file
0
invoice_ninja/endpoints/__init__.py
Normal file
30
invoice_ninja/endpoints/base_endpoint.py
Normal file
30
invoice_ninja/endpoints/base_endpoint.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from invoice_ninja import InvoiceNinja
|
||||
|
||||
class BaseEndpoint(InvoiceNinja):
|
||||
def bulk(self, action: str):
|
||||
pass
|
||||
|
||||
def archive(self):
|
||||
pass
|
||||
|
||||
def delete(self):
|
||||
pass
|
||||
|
||||
def restore(self):
|
||||
pass
|
||||
|
||||
def all(self):
|
||||
pass
|
||||
|
||||
def get(self):
|
||||
pass
|
||||
|
||||
def update(self):
|
||||
pass
|
||||
|
||||
def create(self):
|
||||
pass
|
||||
|
||||
def download(self):
|
||||
pass
|
||||
|
||||
92
invoice_ninja/endpoints/clients.py
Normal file
92
invoice_ninja/endpoints/clients.py
Normal file
@@ -0,0 +1,92 @@
|
||||
from invoice_ninja.endpoints.base_endpoint import BaseEndpoint
|
||||
from invoice_ninja.types.client import Client
|
||||
|
||||
import requests
|
||||
|
||||
class Clients(BaseEndpoint):
|
||||
uri = '/api/v1/clients'
|
||||
|
||||
def __init__(self, base_url: str = str(), api_token: str = str()):
|
||||
super().__init__(base_url, api_token)
|
||||
self.url = super()._get_url_for('clients')
|
||||
|
||||
def __build_sort_params(self, sort: dict):
|
||||
sort_params = {'sort': str()}
|
||||
is_first_entry = True
|
||||
for option in sort.keys():
|
||||
if is_first_entry:
|
||||
sort_params['sort'] += '{}|{}'.format(option, sort[option])
|
||||
is_first_entry = False
|
||||
|
||||
else:
|
||||
sort_params['sort'] += ' {}|{}'.format(option, sort[option])
|
||||
|
||||
return sort_params
|
||||
|
||||
def __client_from_dict(self, client: dict):
|
||||
return Client(client_id=client['id'],
|
||||
name=client['name'],
|
||||
address=client['address1'],
|
||||
city=client['city'],
|
||||
state=client['state'],
|
||||
postal_code=client['postal_code'],
|
||||
phone=client['phone'],
|
||||
email=client['contacts'][0]['email'],
|
||||
pets=client['custom_value1'])
|
||||
|
||||
def __clients_from_response(self, response: requests.Response):
|
||||
clients = list()
|
||||
for client_dict in response.json()['data']:
|
||||
clients.append(self.__client_from_dict(client_dict))
|
||||
|
||||
return clients
|
||||
|
||||
def show_client(self, client_id: str = None):
|
||||
"""
|
||||
Get client based on client id.
|
||||
"""
|
||||
|
||||
if client_id:
|
||||
response = requests.get(url=self.url,
|
||||
headers=super()._get_headers())
|
||||
|
||||
if response.ok:
|
||||
return self.__client_from_dict(response.json()['data'])
|
||||
|
||||
return None
|
||||
|
||||
def list_clients(self, include: str = 'activities',
|
||||
sort: dict = dict(), status: str = 'active',
|
||||
name: str = None):
|
||||
"""
|
||||
Get list of clients.
|
||||
"""
|
||||
|
||||
request_params = dict()
|
||||
|
||||
# Add sort parameters to request
|
||||
if len(sort) > 0:
|
||||
request_params.update(self.__build_sort_params(sort))
|
||||
|
||||
# Add include parameters to request
|
||||
request_params.update({'include': include})
|
||||
|
||||
# Add status parameters to request
|
||||
request_params.update({'status': status})
|
||||
|
||||
# Add name parameters to request
|
||||
if name:
|
||||
request_params.update({'name': name})
|
||||
|
||||
# Check is request should be sent with parameters
|
||||
if len(request_params) > 0:
|
||||
response = requests.get(url=self.url,
|
||||
params=request_params,
|
||||
headers=super()._get_headers())
|
||||
else:
|
||||
response = requests.get(url=self.url,
|
||||
headers=super()._get_headers())
|
||||
|
||||
if response.ok:
|
||||
return self.__clients_from_response(response)
|
||||
|
||||
Reference in New Issue
Block a user