exceptions handling & renamed Invoice to NewInvoiceParameters

This commit is contained in:
Dariusz Aniszewski 2015-06-13 23:52:47 +02:00
parent 0d13244808
commit c97fdf5bdd
3 changed files with 50 additions and 9 deletions

View File

@ -3,8 +3,8 @@ import json
import datetime
from time import strftime
from python_ifirma.exceptions import PythonIfirmaExceptionFactory
from python_ifirma.helpers import Helpers
import requests
@ -67,7 +67,7 @@ class Position:
}
class Invoice:
class NewInvoiceParams:
def __init__(self, client, positions):
self.client = client
self.positions = positions
@ -115,6 +115,20 @@ 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"), '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:
raise PythonIfirmaExceptionFactory.throw_exception_by_code(response_code)
return response_dict
def __create_invoice_and_return_id(self, invoice, url):
request_content = json.dumps(invoice.get_request_data(), separators=(',', ':'))
request_hash_text = "{}{}{}{}".format(
@ -131,8 +145,9 @@ class iFirmaAPI():
Helpers.get_hmac_of_text(self.__invoice_key_value, request_hash_text)
)
}
response = requests.post(url, data=request_content, headers=headers)
response_dict = json.loads(response.content.decode("utf-8"), 'utf-8')
response_dict = self.__execute_post_request(headers, request_content, url)
if response_dict["response"].get("Identyfikator"):
invoice_id = response_dict["response"]["Identyfikator"]
return invoice_id

View File

@ -0,0 +1,25 @@
class PythonIfirmaExceptionFactory:
@staticmethod
def throw_exception_by_code(code):
if code == 201:
raise BadRequestParameters()
elif code == 400:
raise BadRequestStructureException()
raise UnknownException()
class PythonIfirmaException(BaseException):
code = None
class UnknownException(PythonIfirmaException):
code = -1
class BadRequestParameters(PythonIfirmaException):
code = 201
class BadRequestStructureException(PythonIfirmaException):
code = 400

View File

@ -1,6 +1,7 @@
from unittest.case import TestCase
from python_ifirma.core import Client, iFirmaAPI, Invoice, Position, VAT, Address
from python_ifirma.core import Client, iFirmaAPI, NewInvoiceParams, Position, VAT, Address
from python_ifirma import exceptions
from python_ifirma.helpers import Helpers
@ -92,7 +93,6 @@ class TestClient(TestCase):
class TestCreateInvoice(TestCase):
def setUp(self):
print("SERUP")
self.ifirma_client = iFirmaAPI(TEST_IFIRMA_USER, TEST_IFIRMA_INVOICE_KEY, TEST_IFIRMA_USER_KEY)
self.client = Client(
@ -105,11 +105,12 @@ class TestCreateInvoice(TestCase):
self.position = Position(VAT.VAT_23, 1, 1000, "nazwa", "szt")
def test_generate_invoice(self):
invoice = Invoice(self.client, [self.position])
invoice = NewInvoiceParams(self.client, [self.position])
self.assertIsNotNone(self.ifirma_client.generate_invoice(invoice))
def test_generate_invoice_with_position_with_bad_vat(self):
bad_position = Position(0.22, 1, 1000, "nazwa", "szt")
invoice = Invoice(self.client, [bad_position])
self.assertIsNone(self.ifirma_client.generate_invoice(invoice))
with self.assertRaises(exceptions.BadRequestParameters):
invoice = NewInvoiceParams(self.client, [bad_position])
self.ifirma_client.generate_invoice(invoice)