65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
import requests
|
|
|
|
from pyinvoiceninja.models import Client
|
|
from pyinvoiceninja.models import Document
|
|
|
|
class DocumentsAPI(object):
|
|
"""
|
|
A client for interacting with the Invoice Ninja documents API.
|
|
|
|
Attributes
|
|
----------
|
|
client : InvoiceNinjaClient
|
|
An instance of the InvoiceNinjaClient class.
|
|
documents_url : str
|
|
The URL for accessing the clients endpoint.
|
|
"""
|
|
|
|
def __init__(self, client):
|
|
"""
|
|
Constructs all the necessary attributes for the DocumentsAPI object.
|
|
|
|
Parameters
|
|
----------
|
|
client : InvoiceNinjaClient
|
|
An instance of the InvoiceNinjaClient class.
|
|
"""
|
|
self.client = client
|
|
self.documents_url = f'{self.client.base_url}/documents'
|
|
|
|
def __get_documents_from_response(self, response: requests.Response):
|
|
"""
|
|
Parses the response from the API and converts it to a list of Client objects.
|
|
|
|
Parameters
|
|
----------
|
|
response : requests.Response
|
|
The response object from the API request.
|
|
|
|
Returns
|
|
-------
|
|
list of Client
|
|
A list of Client objects parsed from the response.
|
|
"""
|
|
documents = list()
|
|
for document_dict in response.json()['data']:
|
|
documents.append(Document.from_json(document_dict))
|
|
return documents
|
|
|
|
def get_documents(self):
|
|
"""
|
|
Retrieves a list of documents from the Invoice Ninja API.
|
|
|
|
Returns
|
|
-------
|
|
list of Documents
|
|
A list of Document objects if the request is successful.
|
|
"""
|
|
self.client._log_debug(f'Fetching documents from {self.documents_url}')
|
|
response = requests.get(self.documents_url, headers=self.client.headers)
|
|
self.client._log_debug(f'Server response: {response.status_code}, {response.text}')
|
|
|
|
if response.ok:
|
|
return self.__get_documents_from_response(response)
|
|
|