Changed renamed source directory to pyinvoiceninja

This commit is contained in:
2024-09-30 12:54:44 -04:00
parent cc34db9ed1
commit deacda0cfa
21 changed files with 1343 additions and 297 deletions

View File

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