From 86f095d13b88d7b643a6c2df63a2919385400601 Mon Sep 17 00:00:00 2001 From: Dariusz Aniszewski Date: Sun, 14 Jun 2015 21:50:50 +0200 Subject: [PATCH] downloading PDF invoice --- python_ifirma/core.py | 36 ++++++++++++++++++++++++++++++++---- python_ifirma/tests.py | 14 ++++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/python_ifirma/core.py b/python_ifirma/core.py index 5699009..a6b61ea 100644 --- a/python_ifirma/core.py +++ b/python_ifirma/core.py @@ -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 \ No newline at end of file diff --git a/python_ifirma/tests.py b/python_ifirma/tests.py index 13d0507..b768266 100644 --- a/python_ifirma/tests.py +++ b/python_ifirma/tests.py @@ -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) \ No newline at end of file + 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)) \ No newline at end of file