Compare commits

..

1 Commits

Author SHA1 Message Date
b91ddb6685 Refactoring ifirma core 2024-11-15 14:32:02 +01:00

View File

@ -10,6 +10,9 @@ from python_ifirma.helpers import Helpers
import requests
from urllib.parse import urljoin, quote
class VAT:
VAT_0 = 0.00
VAT_5 = 0.05
@ -165,6 +168,8 @@ class NewInvoiceParams:
class iFirmaAPI():
__base_url = "https://www.ifirma.pl/iapi/"
__username = None
__invoice_key_name = 'faktura'
__invoice_key_value = None
@ -203,10 +208,30 @@ class iFirmaAPI():
Helpers.get_hmac_of_text(key_value, request_hash_text)
)
def __get(self, url_path, key_name, key_value="", params={}):
"""Send GET request to IFirma API"""
url = urljoin(self.__base_url, url_path)
request_hash_text = f"{url}{self.__username}{key_name}"
headers = {
# Content-type not needed in GET requests
# "Content-type": "application/json; charset=UTF-8",
"Accept": "application/json",
"Authentication": self.__get_auth_header(request_hash_text,
key_value=key_value)
}
response = requests.get(url, headers=headers, params=params)
return json.loads(response.content.decode('utf-8'))
def find_partner(self, keyword):
return self.__get(f"kontrahenci/{quote(keyword)}.json",
self.__invoice_key_name)
def __create_invoice_and_return_id(self, invoice, url):
# from pprint import pprint
# pprint(invoice.get_request_data())
request_content = json.dumps(invoice.get_request_data(), separators=(',', ':'))
request_content = json.dumps(invoice.get_request_data(),
separators=(',', ':'))
request_hash_text = "{}{}{}{}".format(
url,
self.__username,
@ -216,7 +241,8 @@ class iFirmaAPI():
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication": self.__get_auth_header(request_hash_text)
"Authentication": self.__get_auth_header(request_hash_text,
key_value=__user_key_value)
}
response_dict = self.__execute_post_request(headers, request_content, url)
@ -244,43 +270,14 @@ class iFirmaAPI():
self.__post_cost(cost_json, url)
def get_accounting_month(self):
url = 'https://www.ifirma.pl/iapi/abonent/miesiacksiegowy.json'
request_hash_text = "{}{}{}".format(
url,
self.__username,
self.__user_key_name,
)
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication":
self.__get_auth_header(request_hash_text, self.__user_key_value)
}
response = requests.get(url, headers=headers)
resp = json.loads(response.content.decode("utf-8"))
response = self.__get("abonent/miesiacksiegowy.json",
self.__user_key_name,
key_value=self.__user_key_value)
month = resp['response']["MiesiacKsiegowy"]
year = resp['response']['RokKsiegowy']
month = response['response']["MiesiacKsiegowy"]
year = response['response']['RokKsiegowy']
return year, month
def list_invoices(self, date_from="", date_to=""):
url = f"https://www.ifirma.pl/iapi/faktury.json?dataOd={date_from}&dataDo={date_to}"
request_hash_text = "{}{}{}".format(
url.split('?')[0],
self.__username,
self.__invoice_key_name,
)
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication":
self.__get_auth_header(request_hash_text)
}
response = requests.get(url, headers=headers)
resp = json.loads(response.content.decode("utf-8"))
return resp
def __post_cost(self, cost_json, url):
request_content = json.dumps(cost_json, separators=(',', ':'))
request_hash_text = "{}{}{}{}".format(
@ -303,6 +300,15 @@ class iFirmaAPI():
else:
return None
def list_invoices(self, date_from="", date_to=""):
params = {"dataOd": date_from}
if date_to:
params.update(dataDo=date_to)
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(