moved http client logic to http_client library
This commit is contained in:
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)
|
||||
|
||||
Reference in New Issue
Block a user