mirror of
				https://github.com/Eledio/superfaktura-client.git
				synced 2025-10-31 16:11:20 +01:00 
			
		
		
		
	api: remove data_format and simplify usages of get request
This commit is contained in:
		| @@ -31,6 +31,7 @@ Usage: | ||||
|     bank.post(data) | ||||
| """ | ||||
|  | ||||
| import json | ||||
| from dataclasses import dataclass, asdict | ||||
| from typing import Optional | ||||
|  | ||||
| @@ -96,7 +97,8 @@ class BankAccount(SuperFakturaAPI): | ||||
|     def list(self) -> dict: | ||||
|         """Retrieves a list of bank accounts.""" | ||||
|         url = "bank_accounts/index" | ||||
|         return self.get(url) | ||||
|         bank_accounts = self.get(url) | ||||
|         return json.loads(bank_accounts) | ||||
|  | ||||
|     def default(self) -> Optional[BankAccountModel]: | ||||
|         """Retrieves the default bank account.""" | ||||
|   | ||||
| @@ -104,12 +104,14 @@ class ClientContact(SuperFakturaAPI): | ||||
|     def list(self) -> dict: | ||||
|         """Lists all exists client contacts.""" | ||||
|         url = "clients/index.json" | ||||
|         return self.get(endpoint=url) | ||||
|         clients = self.get(endpoint=url) | ||||
|         return json.loads(clients) | ||||
|  | ||||
|     def get_client(self, client_id: int) -> ClientContactModel: | ||||
|         """Gets a client contact by ID.""" | ||||
|         url = f"clients/view/{client_id}" | ||||
|         data = self.get(endpoint=url) | ||||
|         clients = self.get(endpoint=url) | ||||
|         data = json.loads(clients) | ||||
|         if "Client" not in data: | ||||
|             raise ClientException("Client not found") | ||||
|         data = data["Client"] | ||||
| @@ -122,4 +124,4 @@ if __name__ == "__main__": | ||||
|  | ||||
|     pprint(resp) | ||||
|  | ||||
|     pprint(client.get_client(40019)) | ||||
|     pprint(client.get_client(40011)) | ||||
|   | ||||
| @@ -1,32 +0,0 @@ | ||||
| """ | ||||
| Data Format Enumeration. | ||||
|  | ||||
| This module provides an enumeration of data formats that can be used in the SuperFaktura API. | ||||
|  | ||||
| Classes: | ||||
|     - DataFormat: Enumeration of data formats. | ||||
|  | ||||
| Usage: | ||||
|     from superfaktura.enumerations.data_format import DataFormat | ||||
|     data_format = DataFormat.JSON | ||||
| """ | ||||
|  | ||||
| import enum | ||||
|  | ||||
|  | ||||
| class DataFormat(enum.Enum): | ||||
|     """ | ||||
|     Data Format Enumeration. | ||||
|  | ||||
|     This enumeration represents the different data formats that can be used in the SuperFaktura API. | ||||
|  | ||||
|     Values: | ||||
|         - JSON: JSON format | ||||
|         - PDF: PDF format | ||||
|  | ||||
|     Usage: | ||||
|         data_format = DataFormat.JSON | ||||
|     """ | ||||
|  | ||||
|     JSON = enum.auto() | ||||
|     PDF = enum.auto() | ||||
| @@ -57,7 +57,6 @@ from typing import Optional, List | ||||
| import json | ||||
|  | ||||
| from superfaktura.client_contacts import ClientContactModel | ||||
| from superfaktura.enumerations.data_format import DataFormat | ||||
| from superfaktura.enumerations.language import Language | ||||
| from superfaktura.superfaktura_api import SuperFakturaAPI | ||||
| from superfaktura.utils.data_types import Date, DateEncoder | ||||
| @@ -255,7 +254,7 @@ class Invoice(SuperFakturaAPI): | ||||
|         invoice_model: InvoiceModel, | ||||
|         items: List[InvoiceItem], | ||||
|         contact: ClientContactModel, | ||||
|         invoice_settings: Optional[InvoiceSettings], | ||||
|         invoice_settings: Optional[InvoiceSettings] = None, | ||||
|     ) -> InvoiceRespModel: | ||||
|         """ | ||||
|         Adds a new invoice. | ||||
| @@ -277,7 +276,7 @@ class Invoice(SuperFakturaAPI): | ||||
|             "Invoice": invoice_model.as_dict(), | ||||
|             "InvoiceItem": [item.as_dict() for item in items], | ||||
|             "Client": contact.as_dict(), | ||||
|             "InvoiceSetting": invoice_settings.as_dict(), | ||||
|             "InvoiceSetting": invoice_settings.as_dict() if invoice_settings else {}, | ||||
|         } | ||||
|         url = "invoices/create" | ||||
|         resp = self.post(endpoint=url, data=json.dumps(data, cls=DateEncoder)) | ||||
| @@ -304,5 +303,5 @@ class Invoice(SuperFakturaAPI): | ||||
|             bytes: A bytes containing the PDF data. | ||||
|         """ | ||||
|         url = f"{language}/invoices/pdf/{invoice.invoice_id}/token:{invoice.invoice_token}" | ||||
|         document = self.get(url, data_format=DataFormat.PDF)["pdf"] | ||||
|         document = self.get(url) | ||||
|         return document | ||||
|   | ||||
| @@ -32,8 +32,6 @@ from typing import Dict | ||||
| import requests | ||||
| from dotenv import load_dotenv  # type: ignore | ||||
|  | ||||
| from superfaktura.enumerations.data_format import DataFormat | ||||
|  | ||||
|  | ||||
| class SuperFakturaAPIException(Exception): | ||||
|     """Exception for errors when working with the SuperFaktura API.""" | ||||
| @@ -64,9 +62,7 @@ class SuperFakturaAPI: | ||||
|             f"{_api_company_id}" | ||||
|         } | ||||
|  | ||||
|     def get( | ||||
|         self, endpoint: str, data_format: DataFormat = DataFormat.JSON, timeout: int = 5 | ||||
|     ) -> Dict: | ||||
|     def get(self, endpoint: str, timeout: int = 5) -> bytes: | ||||
|         """ | ||||
|         Retrieves data from the SuperFaktura API. | ||||
|  | ||||
| @@ -78,7 +74,7 @@ class SuperFakturaAPI: | ||||
|             timeout (int, optional): The timeout for the API request in seconds. Defaults to 5. | ||||
|  | ||||
|         Returns: | ||||
|             Dict: The retrieved data in JSON format. | ||||
|             bytes: The retrieved data in bytes. | ||||
|  | ||||
|         Raises: | ||||
|             SuperFakturaAPIException: If the API request fails or returns an error. | ||||
| @@ -94,14 +90,9 @@ class SuperFakturaAPI: | ||||
|         url = f"{self._api_url}/{endpoint}" | ||||
|         req = requests.get(url=url, headers=self._auth_header, timeout=timeout) | ||||
|         if req.status_code == 200: | ||||
|             if data_format == DataFormat.JSON: | ||||
|                 return req.json() | ||||
|             elif data_format == DataFormat.PDF: | ||||
|                 return {"pdf": req.content}  # returns a dict with the PDF content | ||||
|             else: | ||||
|                 raise SuperFakturaAPIException("Invalid data format") | ||||
|             return req.content | ||||
|         raise SuperFakturaAPIException( | ||||
|             f"Get status code: {req.status_code}; {req.json()}" | ||||
|             f"Get status code: {req.status_code}; {req.content!r}" | ||||
|         ) | ||||
|  | ||||
|     def post(self, endpoint: str, data: str, timeout: int = 5) -> Dict: | ||||
|   | ||||
| @@ -12,6 +12,8 @@ Usage: | ||||
|     print(countries) | ||||
| """ | ||||
|  | ||||
| import json | ||||
|  | ||||
| from superfaktura.superfaktura_api import SuperFakturaAPI | ||||
|  | ||||
|  | ||||
| @@ -30,4 +32,5 @@ def country_list(): | ||||
|     """ | ||||
|     api = SuperFakturaAPI() | ||||
|     url = "countries" | ||||
|     return api.get(url) | ||||
|     countries = api.get(url) | ||||
|     return json.loads(countries) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user