Refactoring ifirma core

This commit is contained in:
Kuba Winnicki 2024-11-14 18:48:55 +01:00
parent 8483d1432c
commit 00111e84d3

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import six
import json
import datetime
@ -50,17 +51,17 @@ class Client:
"Ulica": self.address.street,
"Miejscowosc": self.address.city,
"Kraj": self.address.country,
#"Email": self.email,
#"Telefon": self.phone_number,
# "Email": self.email,
# "Telefon": self.phone_number,
"OsobaFizyczna": False,
}
if self.export == "eu":
d.update({"PrefiksUE": self.tax_id[:2]})
if self.export == "eu" or self.export == "yes":
d.update({"NIP": self.tax_id[2:]})
#if self.export == "eu":
#d.update({"PrefiksUE": self.tax_id[:2]})
#elif self.export == "yes":
# if self.export == "eu":
# d.update({"PrefiksUE": self.tax_id[:2]})
# elif self.export == "yes":
d.update({"Kraj": self.tax_id[:2]})
else:
d.update({"NIP": self.tax_id})
@ -147,7 +148,7 @@ class NewInvoiceParams:
d.update({
"DataObowiazkuPodatkowego": self.__get_issue_date(),
"NazwaUslugi": "services",
"Waluta": self.currency, #"PLN",
"Waluta": self.currency,
"Jezyk": "en",
"KursWalutyWidoczny": False,
"KursWalutyZDniaPoprzedzajacegoDzienWystawieniaFaktury": 1.00,
@ -195,7 +196,7 @@ class iFirmaAPI():
return response_dict
def __create_authentication_header_value(self, request_hash_text, key_value=""):
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(
self.__username,
@ -215,7 +216,7 @@ class iFirmaAPI():
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication": self.__create_authentication_header_value(request_hash_text)
"Authentication": self.__get_auth_header(request_hash_text)
}
response_dict = self.__execute_post_request(headers, request_content, url)
@ -253,7 +254,7 @@ class iFirmaAPI():
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication":
self.__create_authentication_header_value(request_hash_text, self.__user_key_value)
self.__get_auth_header(request_hash_text, self.__user_key_value)
}
resp = requests.get(url, headers=headers)
content = resp.content
@ -270,7 +271,7 @@ class iFirmaAPI():
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication": self.__create_authentication_header_value(request_hash_text)
"Authentication": self.__get_auth_header(request_hash_text)
}
response_dict = self.__execute_post_request(headers, request_content, url)
@ -291,7 +292,7 @@ class iFirmaAPI():
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication": self.__create_authentication_header_value(request_hash_text)
"Authentication": self.__get_auth_header(request_hash_text)
}
params = {}
if limit:
@ -301,7 +302,7 @@ class iFirmaAPI():
return rj
def get_invoice(self, invoice_id):
url = "https://www.ifirma.pl/iapi/fakturaeksportuslug/{}.json".format(invoice_id)
url = f"https://www.ifirma.pl/iapi/fakturaeksportuslug/{invoice_id}.json"
request_hash_text = "{}{}{}".format(
url,
self.__username,
@ -310,7 +311,7 @@ class iFirmaAPI():
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication": self.__create_authentication_header_value(request_hash_text)
"Authentication": self.__get_auth_header(request_hash_text)
}
resp = requests.get(url, headers=headers)
rj = json.loads(resp.content.decode('utf-8'))
@ -320,11 +321,11 @@ class iFirmaAPI():
return rj
def get_invoice_pdf(self, invoice_id):
url = "https://www.ifirma.pl/iapi/fakturakraj/{}.pdf".format(invoice_id)
url = f"https://www.ifirma.pl/iapi/fakturakraj/{invoice_id}.pdf"
return self.__download_pdf(url)
def __get_invoice_number(self, invoice_id):
url = "https://www.ifirma.pl/iapi/fakturakraj/{}.json".format(invoice_id)
url = f"https://www.ifirma.pl/iapi/fakturakraj/{invoice_id}.json"
request_hash_text = "{}{}{}".format(
url,
self.__username,
@ -333,7 +334,7 @@ class iFirmaAPI():
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication": self.__create_authentication_header_value(request_hash_text)
"Authentication": self.__get_auth_header(request_hash_text)
}
resp = requests.get(url, headers=headers)
rj = json.loads(resp.content.decode('utf-8'))
@ -351,7 +352,7 @@ class iFirmaAPI():
headers = {
"Accept": "application/pdf",
"Content-type": "application/pdf; charset=UTF-8",
"Authentication": self.__create_authentication_header_value(request_hash_text)
"Authentication": self.__get_auth_header(request_hash_text)
}
resp = requests.get(url, headers=headers)