75 lines
2.0 KiB
Python
75 lines
2.0 KiB
Python
import requests
|
|
|
|
class InvoiceNinja(object):
|
|
API_V1 = 'api/v1'
|
|
ENDPOINT_URLS = {
|
|
'clients': 'clients',
|
|
'products': 'products',
|
|
'invoices': 'invoices',
|
|
'recurring invoices': 'recurring_invoices',
|
|
'payments': 'payments',
|
|
'quotes': 'quotes',
|
|
'credits': 'credits',
|
|
'reports': 'reports',
|
|
'activities': 'activities',
|
|
'charts': 'charts',
|
|
'companies': 'companies',
|
|
'documents': 'documents',
|
|
'emails': 'emails',
|
|
'expense': 'expenses',
|
|
'export': 'export',
|
|
'import': 'import_json',
|
|
'ping': 'ping',
|
|
'health check': 'health_check',
|
|
'users': 'users'
|
|
}
|
|
|
|
def __init__(self,
|
|
endpoint_url: str = 'https://invoicing.co',
|
|
api_token: str = str()):
|
|
self.endpoint_url = '{}/{}'.format(endpoint_url, self.API_V1)
|
|
self.api_token = api_token
|
|
self.headers = dict()
|
|
|
|
def _get_url_for(self, endpoint: str = 'ping'):
|
|
'''
|
|
Get complete URL for an endpoint.
|
|
|
|
Endpoint URLs are appended to the Invoice Ninja base URL
|
|
and returned.
|
|
'''
|
|
if endpoint in self.ENDPOINT_URLS:
|
|
return '{}/{}'.format(self.endpoint_url,
|
|
self.ENDPOINT_URLS[endpoint])
|
|
|
|
else:
|
|
raise KeyError('Endpoint URL not found')
|
|
|
|
#def _get_headers(self, headers: dict = dict()):
|
|
def build_headers(self):
|
|
'''
|
|
Build Invoice Ninja API headers for request.
|
|
|
|
A header dictionary with the API token is returned by default.
|
|
'''
|
|
|
|
headers = {
|
|
'X-API-TOKEN': self.api_token,
|
|
'X-Requested-With': 'XMLHttpRequest'}
|
|
|
|
return self.headers.update(headers)
|
|
|
|
def ping(self):
|
|
'''
|
|
Ping Invoice Ninja instance.
|
|
'''
|
|
server_response = requests.get(url=self._get_url_for(),
|
|
headers=self.build_headers())
|
|
|
|
if server_response.ok:
|
|
return True
|
|
|
|
else:
|
|
return False
|
|
|