Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 02cc6cdf0e | |||
| 560d3377e3 | |||
| e61396dd6a | |||
| e14328c68b |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
__pycache__
|
||||||
@ -1,74 +1,24 @@
|
|||||||
import requests
|
from invoice_ninja.endpoints.clients import Clients
|
||||||
|
|
||||||
class InvoiceNinja(object):
|
class InvoiceNinja(object):
|
||||||
API_V1 = 'api/v1'
|
def __init__(self, endpoint_url: str = 'https://invoicing.co',
|
||||||
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()):
|
api_token: str = str()):
|
||||||
self.endpoint_url = '{}/{}'.format(endpoint_url, self.API_V1)
|
self._endpoint_url = endpoint_url
|
||||||
self.api_token = api_token
|
self._api_token = api_token
|
||||||
self.headers = dict()
|
|
||||||
|
|
||||||
def _get_url_for(self, endpoint: str = 'ping'):
|
@property
|
||||||
'''
|
def endpoint_url(self):
|
||||||
Get complete URL for an endpoint.
|
return self._endpoint_url
|
||||||
|
|
||||||
Endpoint URLs are appended to the Invoice Ninja base URL
|
@endpoint_url.setter
|
||||||
and returned.
|
def endpoint_url(self, endpoint_url: str):
|
||||||
'''
|
self._endpoint_url = endpoint_url
|
||||||
if endpoint in self.ENDPOINT_URLS:
|
|
||||||
return '{}/{}'.format(self.endpoint_url,
|
|
||||||
self.ENDPOINT_URLS[endpoint])
|
|
||||||
|
|
||||||
else:
|
@property
|
||||||
raise KeyError('Endpoint URL not found')
|
def api_token(self):
|
||||||
|
return self._api_token
|
||||||
|
|
||||||
#def _get_headers(self, headers: dict = dict()):
|
@api_token.setter
|
||||||
def build_headers(self):
|
def api_token(self, api_token: str):
|
||||||
'''
|
self._api_token = api_token
|
||||||
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
|
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
from invoice_ninja import InvoiceNinja
|
class BaseEndpoint(object):
|
||||||
|
|
||||||
class BaseEndpoint(InvoiceNinja):
|
|
||||||
def bulk(self, action: str):
|
def bulk(self, action: str):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@ -1,15 +1,11 @@
|
|||||||
from invoice_ninja.endpoints.base_endpoint import BaseEndpoint
|
from invoice_ninja.endpoints.base_endpoint import BaseEndpoint
|
||||||
from invoice_ninja.types.client import Client
|
from invoice_ninja.models.client import Client
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
class Clients(BaseEndpoint):
|
class Clients(BaseEndpoint):
|
||||||
uri = '/api/v1/clients'
|
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):
|
def __build_sort_params(self, sort: dict):
|
||||||
sort_params = {'sort': str()}
|
sort_params = {'sort': str()}
|
||||||
is_first_entry = True
|
is_first_entry = True
|
||||||
|
|||||||
10
invoice_ninja/endpoints/ping.py
Normal file
10
invoice_ninja/endpoints/ping.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from invoice_ninja.endpoints.base_endpoint import BaseEndpoint
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
class Ping(BaseEndpoint):
|
||||||
|
uri = '/api/v1/ping'
|
||||||
|
|
||||||
|
def ping(self):
|
||||||
|
pass
|
||||||
|
|
||||||
57
invoice_ninja/http_client.py
Normal file
57
invoice_ninja/http_client.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
class HTTPClient(object):
|
||||||
|
"""HTTP client for Invoice Ninja REST API."""
|
||||||
|
|
||||||
|
def __init__(self, endpoint_url: str = 'https://invoicing.co',
|
||||||
|
api_token: str = str()):
|
||||||
|
self._endpoint_url = 'https://invoicing.co'
|
||||||
|
self._api_token = str()
|
||||||
|
self._headers = dict()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def endpoint_url(self):
|
||||||
|
return self._endpoint_url
|
||||||
|
|
||||||
|
@endpoint_url.setter
|
||||||
|
def endpoint_url(self, endpoint_url: str):
|
||||||
|
self._endpoint_url = endpoint_url
|
||||||
|
|
||||||
|
@property
|
||||||
|
def api_token(self):
|
||||||
|
return self._api_token
|
||||||
|
|
||||||
|
@api_token.setter
|
||||||
|
def api_token(self, api_token: str):
|
||||||
|
self._api_token = api_token
|
||||||
|
|
||||||
|
def add_headers(self, headers: dict):
|
||||||
|
"""Add HTTP headers to request."""
|
||||||
|
|
||||||
|
self._headers.update(headers)
|
||||||
|
|
||||||
|
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': _api_token,
|
||||||
|
'X-Requested-With': 'XMLHttpRequest'}
|
||||||
|
|
||||||
|
return self._headers.update(headers)
|
||||||
|
|
||||||
|
def send(self, uri: str,
|
||||||
|
payload: dict,
|
||||||
|
method: str = 'get'):
|
||||||
|
"""Send request to Invoice Ninja REST API."""
|
||||||
|
|
||||||
|
url = '{}/{}'.format(endpoint_url, uri)
|
||||||
|
|
||||||
|
if method == 'get':
|
||||||
|
return requests.get(url, params=payload)
|
||||||
|
|
||||||
|
elif method == 'post':
|
||||||
|
return requests.post(url, params=payload)
|
||||||
|
|
||||||
57
invoice_ninja/http_client/http_client.py
Normal file
57
invoice_ninja/http_client/http_client.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
class HTTPClient(object):
|
||||||
|
"""HTTP client for Invoice Ninja REST API."""
|
||||||
|
|
||||||
|
def __init__(self, endpoint_url: str = 'https://invoicing.co',
|
||||||
|
api_token: str = str()):
|
||||||
|
self_endpoint_url = 'https://invoicing.co'
|
||||||
|
self._api_token = str()
|
||||||
|
self._headers = dict()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def endpoint_url(self):
|
||||||
|
return self._endpoint_url
|
||||||
|
|
||||||
|
@endpoint_url.setter
|
||||||
|
def endpoint_url(self, endpoint_url: str):
|
||||||
|
self._endpoint_url = endpoint_url
|
||||||
|
|
||||||
|
@property
|
||||||
|
def api_token(self):
|
||||||
|
return self._api_token
|
||||||
|
|
||||||
|
@api_token.setter
|
||||||
|
def api_token(self, api_token: str):
|
||||||
|
self._api_token = api_token
|
||||||
|
|
||||||
|
def add_headers(self, headers: dict):
|
||||||
|
"""Add HTTP headers to request."""
|
||||||
|
|
||||||
|
self._headers.update(headers)
|
||||||
|
|
||||||
|
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': _api_token,
|
||||||
|
'X-Requested-With': 'XMLHttpRequest'}
|
||||||
|
|
||||||
|
return self._headers.update(headers)
|
||||||
|
|
||||||
|
def send(self, uri: str,
|
||||||
|
payload: dict,
|
||||||
|
method: str = 'get'):
|
||||||
|
"""Send request to Invoice Ninja REST API."""
|
||||||
|
|
||||||
|
url = '{}/{}'.format(endpoint_url, uri)
|
||||||
|
|
||||||
|
if method == 'get':
|
||||||
|
return requests.get(url, params=payload)
|
||||||
|
|
||||||
|
elif method == 'post':
|
||||||
|
return requests.post(url, params=payload)
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user