moved http client logic to http_client library

This commit is contained in:
Andrew Bryant 2023-12-18 19:14:15 -05:00
parent e61396dd6a
commit 560d3377e3
7 changed files with 271 additions and 75 deletions

122
diff.txt Normal file
View File

@ -0,0 +1,122 @@
diff --git a/invoice_ninja/__init__.py b/invoice_ninja/__init__.py
index e24c1be..ae1c40c 100644
--- a/invoice_ninja/__init__.py
+++ b/invoice_ninja/__init__.py
@@ -1 +1,24 @@
-from invoice_ninja.invoice_ninja import InvoiceNinja
+from invoice_ninja.endpoints.clients import Clients
+
+class InvoiceNinja(object):
+ def __init__(self, endpoint_url: str = 'https://invoicing.co',
+ api_token: str = str()):
+ self._endpoint_url = endpoint_url
+ self._api_token = api_token
+
+ @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
+
diff --git a/invoice_ninja/endpoints/clients.py b/invoice_ninja/endpoints/clients.py
index 7d71fff..cacde09 100644
--- a/invoice_ninja/endpoints/clients.py
+++ b/invoice_ninja/endpoints/clients.py
@@ -5,11 +5,7 @@ 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
diff --git a/invoice_ninja/invoice_ninja.py b/invoice_ninja/invoice_ninja.py
deleted file mode 100644
index 761941e..0000000
--- a/invoice_ninja/invoice_ninja.py
+++ /dev/null
@@ -1,69 +0,0 @@
-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
-

View File

@ -1 +1,24 @@
from invoice_ninja.invoice_ninja import InvoiceNinja from invoice_ninja.endpoints.clients import Clients
class InvoiceNinja(object):
def __init__(self, endpoint_url: str = 'https://invoicing.co',
api_token: str = str()):
self._endpoint_url = endpoint_url
self._api_token = api_token
@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

View File

@ -5,11 +5,7 @@ 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

View 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

View 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)

View 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)

View File

@ -1,69 +0,0 @@
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