API Refactors

This commit is contained in:
Kuba Winnicki 2024-11-17 09:58:55 +01:00
parent b91ddb6685
commit 4f2e8e70c5

View File

@ -185,22 +185,6 @@ class iFirmaAPI():
self.__invoice_key_value = Helpers.unhex_key_value(_invoice_key_value)
self.__user_key_value = Helpers.unhex_key_value(_user_key_value)
@staticmethod
def __execute_post_request(headers, request_content, url):
response = requests.post(url, data=request_content, headers=headers)
response_dict = json.loads(response.content.decode("utf-8"))
if "response" not in response_dict:
raise PythonIfirmaExceptionFactory.throw_exception_by_code(-1)
real_response_content = response_dict["response"]
response_code = real_response_content.get("Kod", -1)
if response_code != 0:
# print(response_code)
print(real_response_content)
raise PythonIfirmaExceptionFactory.throw_exception_by_code(response_code)
return response_dict
def __get_auth_header(self, request_hash_text, key_value=""):
key_value = key_value or self.__invoice_key_value
return "IAPIS user={}, hmac-sha1={}".format(
@ -223,6 +207,24 @@ class iFirmaAPI():
response = requests.get(url, headers=headers, params=params)
return json.loads(response.content.decode('utf-8'))
@staticmethod
def __post(headers, request_content, url):
"""POST request to IFirma API"""
response = requests.post(url, data=request_content, headers=headers)
response_dict = json.loads(response.content.decode("utf-8"))
if "response" not in response_dict:
raise PythonIfirmaExceptionFactory.throw_exception_by_code(-1)
real_response_content = response_dict["response"]
response_code = real_response_content.get("Kod", -1)
if response_code != 0:
# print(response_code)
print(real_response_content)
raise PythonIfirmaExceptionFactory.throw_exception_by_code(response_code)
return response_dict
def find_partner(self, keyword):
return self.__get(f"kontrahenci/{quote(keyword)}.json",
self.__invoice_key_name)
@ -241,11 +243,10 @@ class iFirmaAPI():
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication": self.__get_auth_header(request_hash_text,
key_value=__user_key_value)
"Authentication": self.__get_auth_header(request_hash_text)
}
response_dict = self.__execute_post_request(headers, request_content, url)
response_dict = self.__post(headers, request_content, url)
if response_dict["response"].get("Identyfikator"):
invoice_id = response_dict["response"]["Identyfikator"]
@ -292,7 +293,7 @@ class iFirmaAPI():
"Authentication": self.__get_auth_header(request_hash_text)
}
response_dict = self.__execute_post_request(headers, request_content, url)
response_dict = self.__post(headers, request_content, url)
if response_dict["response"].get("Identyfikator"):
invoice_id = response_dict["response"]["Identyfikator"]
@ -308,7 +309,6 @@ class iFirmaAPI():
return self.__get("faktury.json", self.__invoice_key_name,
params=params)
def get_invoice_list(self, limit=0):
url = "https://www.ifirma.pl/iapi/fakturaeksportuslugue/list.json"
request_hash_text = "{}{}{}".format(