superfaktura_api: allow get request response body in pdf, not only in json

This commit is contained in:
Richard Kubíček
2025-01-25 19:17:15 +01:00
parent 4edf8fd84c
commit 5a85c2eb00

View File

@@ -32,11 +32,14 @@ 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."""
class SuperFakturaAPIMissingCredentialsException(Exception):
"""Exception for missing login credentials."""
@@ -62,7 +65,7 @@ class SuperFakturaAPI:
f"{_api_company_id}"
}
def get(self, endpoint: str, timeout: int = 5) -> Dict:
def get(self, endpoint: str, data_format: DataFormat = DataFormat.JSON, timeout: int = 5) -> Dict:
"""
Retrieves data from the SuperFaktura API.
@@ -90,7 +93,11 @@ 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(
f"Get status code: {req.status_code}; {req.json()}"
)
@@ -130,6 +137,7 @@ class SuperFakturaAPI:
)
if req.status_code == 200:
return req.json()
else:
raise SuperFakturaAPIException(
f"Post status code: {req.status_code}; {req.json()}"
)