mirror of
https://github.com/Eledio/superfaktura-client.git
synced 2025-11-01 00:18:25 +01:00
superfaktura_api: allow get request response body in pdf, not only in json
This commit is contained in:
@@ -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,10 +93,14 @@ class SuperFakturaAPI:
|
||||
url = f"{self._api_url}/{endpoint}"
|
||||
req = requests.get(url=url, headers=self._auth_header, timeout=timeout)
|
||||
if req.status_code == 200:
|
||||
return req.json()
|
||||
raise SuperFakturaAPIException(
|
||||
f"Get status code: {req.status_code}; {req.json()}"
|
||||
)
|
||||
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()}"
|
||||
)
|
||||
|
||||
def post(self, endpoint: str, data: str, timeout: int = 5) -> Dict:
|
||||
"""
|
||||
@@ -130,6 +137,7 @@ class SuperFakturaAPI:
|
||||
)
|
||||
if req.status_code == 200:
|
||||
return req.json()
|
||||
raise SuperFakturaAPIException(
|
||||
f"Post status code: {req.status_code}; {req.json()}"
|
||||
)
|
||||
else:
|
||||
raise SuperFakturaAPIException(
|
||||
f"Post status code: {req.status_code}; {req.json()}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user