downloading PDF invoice

This commit is contained in:
Dariusz Aniszewski 2015-06-14 21:50:50 +02:00
parent 59d68be586
commit 86f095d13b
2 changed files with 42 additions and 8 deletions

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import six
import json
import datetime
from time import strftime
@ -129,6 +130,12 @@ class iFirmaAPI():
return response_dict
def __create_authentication_header_value(self, request_hash_text):
return "IAPIS user={}, hmac-sha1={}".format(
self.__username,
Helpers.get_hmac_of_text(self.__invoice_key_value, request_hash_text)
)
def __create_invoice_and_return_id(self, invoice, url):
request_content = json.dumps(invoice.get_request_data(), separators=(',', ':'))
request_hash_text = "{}{}{}{}".format(
@ -140,10 +147,7 @@ class iFirmaAPI():
headers = {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authentication": "IAPIS user={}, hmac-sha1={}".format(
self.__username,
Helpers.get_hmac_of_text(self.__invoice_key_value, request_hash_text)
)
"Authentication": self.__create_authentication_header_value(request_hash_text)
}
response_dict = self.__execute_post_request(headers, request_content, url)
@ -157,3 +161,27 @@ class iFirmaAPI():
def generate_invoice(self, invoice):
url = "https://www.ifirma.pl/iapi/fakturakraj.json"
return self.__create_invoice_and_return_id(invoice, url)
def get_invoice_pdf(self, invoice_id):
url = "https://www.ifirma.pl/iapi/fakturakraj/{}.pdf".format(invoice_id)
return self.__download_pdf(url)
def __download_pdf(self, url):
request_hash_text = "{}{}{}".format(
url,
self.__username,
self.__invoice_key_value,
)
headers = {
"Accept": "application/pdf",
"Content-type": "application/pdf; charset=UTF-8",
"Authentication": self.__create_authentication_header_value(request_hash_text)
}
resp = requests.get(url, headers=headers)
content = resp.content
file_obj = six.BytesIO()
file_obj.write(content)
file_obj.seek(0)
return file_obj

View File

@ -5,9 +5,9 @@ from python_ifirma import exceptions
from python_ifirma.helpers import Helpers
TEST_IFIRMA_USER = "$DEMO254271"
TEST_IFIRMA_INVOICE_KEY = "7B10B300C2C029E2"
TEST_IFIRMA_USER_KEY = "12EFF84EFE80A28A"
TEST_IFIRMA_USER = "$DEMO254343"
TEST_IFIRMA_INVOICE_KEY = "C501C88284462384"
TEST_IFIRMA_USER_KEY = "B83E825D4D28BD11"
class TestHelpers(TestCase):
@ -113,4 +113,10 @@ class TestCreateInvoice(TestCase):
with self.assertRaises(exceptions.BadRequestParameters):
invoice = NewInvoiceParams(self.client, [bad_position])
self.ifirma_client.generate_invoice(invoice)
self.ifirma_client.generate_invoice(invoice)
def test_download_invoice(self):
invoice = NewInvoiceParams(self.client, [self.position])
invoice_id = self.ifirma_client.generate_invoice(invoice)
self.assertIsNotNone(self.ifirma_client.get_invoice_pdf(invoice_id))