From e61396dd6a03503bd4523a744bf3b7392072bec7 Mon Sep 17 00:00:00 2001 From: awkawb Date: Sun, 17 Dec 2023 14:21:41 -0500 Subject: [PATCH] initial client work --- invoice_ninja/__init__.py | 52 +----------------- invoice_ninja/endpoints/base_endpoint.py | 4 +- invoice_ninja/endpoints/clients.py | 2 +- invoice_ninja/invoice_ninja.py | 69 ++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 invoice_ninja/invoice_ninja.py diff --git a/invoice_ninja/__init__.py b/invoice_ninja/__init__.py index 8058aa0..e24c1be 100644 --- a/invoice_ninja/__init__.py +++ b/invoice_ninja/__init__.py @@ -1,51 +1 @@ -import requests - -class InvoiceNinja(object): - API_V1 = 'api/v1' - - 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 set_url(url: str): - self.endpoint_url = url - - def get_url(): - return self.endpoint_url - - def set_token(token: str): - self.api_token = token - - def get_token(): - return self.api_token - - #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 - +from invoice_ninja.invoice_ninja import InvoiceNinja diff --git a/invoice_ninja/endpoints/base_endpoint.py b/invoice_ninja/endpoints/base_endpoint.py index 2415307..1600d44 100644 --- a/invoice_ninja/endpoints/base_endpoint.py +++ b/invoice_ninja/endpoints/base_endpoint.py @@ -1,6 +1,4 @@ -from invoice_ninja import InvoiceNinja - -class BaseEndpoint(InvoiceNinja): +class BaseEndpoint(object): def bulk(self, action: str): pass diff --git a/invoice_ninja/endpoints/clients.py b/invoice_ninja/endpoints/clients.py index b7d5397..7d71fff 100644 --- a/invoice_ninja/endpoints/clients.py +++ b/invoice_ninja/endpoints/clients.py @@ -1,5 +1,5 @@ from invoice_ninja.endpoints.base_endpoint import BaseEndpoint -from invoice_ninja.types.client import Client +from invoice_ninja.models.client import Client import requests diff --git a/invoice_ninja/invoice_ninja.py b/invoice_ninja/invoice_ninja.py new file mode 100644 index 0000000..761941e --- /dev/null +++ b/invoice_ninja/invoice_ninja.py @@ -0,0 +1,69 @@ +import requests + +class InvoiceNinja(object): + API_V1 = 'api/v1' + + 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 set_url(self, url: str): + self.endpoint_url = url + + def get_url(self): + return self.endpoint_url + + def set_token(self, token: str): + self.api_token = token + + def get_token(self): + return self.api_token + + def get_headers(self): + """Get HTTP headers for request.""" + + return self.headers + + 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': self.api_token, + 'X-Requested-With': 'XMLHttpRequest'} + + return self.headers.update(headers) + + def send(self, uri: str, payload: dict, method: str = 'get'): + url = '{}/{}'.format(self.endpoint_url, uri) + + if method == 'get': + return requests.get(url, params=payload) + + if method == 'post': + return requests.post(url, params=payload) + + + 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 +