def download_invoice(invoice_id, file_handle): http = Config.get_http_module() resp = Request().download(invoice_id).execute(http) resp.raise_for_status() file_handle.write_bytes(resp.content)
def create_invoice(invoice): from ifirma.request import InvoiceResponse http = Config.get_http_module() data = _serialize(invoice) resp = Request().submit(data).execute(http) resp.raise_for_status() return InvoiceResponse(resp.json())
def email_invoice(invoice_id, email_addr, message): serializer = Config.get_serializer_module() http = Config.get_http_module() data = serializer.make_email(email_addr, message) resp = Request().email(invoice_id, data).execute(http) resp.raise_for_status() return resp.json()
def test_list_invoices_url(): from datetime import date, timedelta df = date(1970, 1, 1) dt = df + timedelta(days=30) req = Request().list(params=dict(date_from=df, date_to=dt, page=7)) res = req.execute(FakeHttpModule) assert req.data is None assert (GET, "https://www.ifirma.pl/iapi/faktury.json?typ=prz_faktura_kraj" "&dataOd=1970-01-01&dataDo=1970-01-31&strona=7&iloscNaStronie=20" ) == get_url(res)
def test_sign_get_invoices_list_request(): from datetime import date, timedelta df = date(1970, 1, 1) dt = df + timedelta(days=30) expected_header = 'IAPIS [email protected], hmac-sha1=081dec7098dbed6aab839e76501a964aa5b9386a' res1 = Request(**args).list( params=dict(date_from=df, date_to=dt, page=7)).execute(FakeHttpModule) res2 = Request(**args).list( params=dict(date_from=dt, date_to=df, page=0)).execute(FakeHttpModule) # signature does not depends on params assert_auth_header(expected_header, res1) assert_auth_header(expected_header, res2)
def test_sign_send_email_request(): res = Request(**args).email( invoice_id=0, data=SEND_EMAIL_WITH_INVOICE_REQUEST).execute(FakeHttpModule) expected = 'IAPIS [email protected], hmac-sha1=491bd26d7f487fb822007899183cf1a29de02f7a' assert_auth_header(expected, res)
def test_sign_create_vat_payer_invoice_with_new_customer_request(): payload = open( f'{SAMPLE_DATA_DIR}/create_vat_payer_invoice_with_new_customer.request.json' ).read() res = Request(**args).submit(payload).execute(FakeHttpModule) expected = 'IAPIS [email protected], hmac-sha1=418cea6c93f4deadbeb5a5e9b25f63145db6a9c0' assert_auth_header(expected, res)
def test_sign_create_vat_payer_invoice_with_known_customer_request(): payload = open( f'{SAMPLE_DATA_DIR}/create_vat_payer_invoice_with_known_customer.request.json' ).read() res = Request(**args).submit(payload).execute(FakeHttpModule) expected = 'IAPIS [email protected], hmac-sha1=33443d8492027e0fb549f4d4aeed48cc5891cf2f' assert_auth_header(expected, res)
def test_sign_create_nonvat_payer_invoice_with_known_customer_request(): payload = open( f'{SAMPLE_DATA_DIR}/create_nonvat_payer_invoice_with_known_customer.request.json' ).read() res = Request(**args).submit(payload).execute(FakeHttpModule) expected = 'IAPIS [email protected], hmac-sha1=ec0b955b8e63733ecb6ebd461d0783391c09bce1' assert_auth_header(expected, res)
def test_sign_create_nonvat_payer_invoice_with_new_customer_request(): payload = open( f'{SAMPLE_DATA_DIR}/create_nonvat_payer_invoice_with_new_customer.request.json' ).read() res = Request(**args).submit(payload).execute(FakeHttpModule) expected = 'IAPIS [email protected], hmac-sha1=c2d3a2c69988792b2941336a3059dc035253407d' assert_auth_header(expected, res)
def test_request_mixed_passing_params_and_config(): os.environ['IFIRMA_API_USERNAME'] = '******' req = Request(api_key='api_key') # it's important to tear down del os.environ['IFIRMA_API_USERNAME'] assert req.api_user == 'user' assert req.api_key == 'api_key'
def test_request_from_config(): os.environ['IFIRMA_API_KEY'] = 'api_key' os.environ['IFIRMA_API_USERNAME'] = '******' req = Request() # it's important to tear down del os.environ['IFIRMA_API_USERNAME'] del os.environ['IFIRMA_API_KEY'] assert req.api_user == 'user' assert req.api_key == 'api_key'
import os, sys from datetime import date, timedelta import requests from pprint import pprint script_path = os.path.realpath(__file__) module_path = os.path.dirname(os.path.dirname(script_path)) sys.path.append(module_path) print("Module path: " + module_path) from ifirma.request import Request if __name__ == "__main__": date_from = date(2022, 1, 1) params = dict(date_from=date_from, date_to=date_from + timedelta(days=30)) req = Request().list(params) resp = req.execute(requests) resp.raise_for_status() invoices_json = resp.json()['response'] pprint(invoices_json) if invoices_json['Kod'] == 0 and invoices_json['Wynik']: invoice_id = invoices_json['Wynik'][0]['FakturaId'] print(f"\nDetails of the invoice id: {invoice_id}\n") req = Request().get(invoice_id) resp = req.execute(requests) resp.raise_for_status()
def test_get_invoice_details_url(): res = Request().get(invoice_id=1337).execute(FakeHttpModule) assert (GET, "https://www.ifirma.pl/iapi/fakturakraj/1337.json") == get_url(res)
def test_send_invoice_email_url(): res = Request().email(invoice_id=1337, data=dict()).execute(FakeHttpModule) assert ( POST, "https://www.ifirma.pl/iapi/fakturakraj/send/1337.json?wyslijEfaktura=true" ) == get_url(res)
Please note that `api_key_name` is not fully supported and then you need to provide the API key manually. Otherwise value of environment variable of `IFIRMA_API_KEY` will be used. # IMPORTANT: This example requires that "abonent" API key is generated. # See ifirma's "Podstawowe klucze autoryzacji" in the "Settings / API" section """ import os, sys import requests script_path = os.path.realpath(__file__) module_path = os.path.dirname(os.path.dirname(script_path)) sys.path.append(module_path) from ifirma.request import Request, API_URL req = Request(api_key_name="abonent", api_key=os.environ.get("IFIRMA_abonent_API_KEY")) req.url = f"{API_URL}/abonent/miesiacksiegowy.json" # Here the GET request method is used. # `execute` only supports GET / POST requests # For PUT one shall directly use of `requests` library resp = req.execute(requests) resp.raise_for_status() invoices_json = resp.json() print(invoices_json)
def test_submit_invoice_url(): res = Request().submit(data=dict()).execute(FakeHttpModule) assert (POST, "https://www.ifirma.pl/iapi/fakturakraj.json") == get_url(res)
def test_request_assert_data_leaves_string_unchanged(): expected = "json string will not be changed" assert Request.assert_data(expected) is expected
def test_request_assert_data_jsonifies_dictionary(): from json import dumps data = dict(invoice_id=1337, customer="Ala") expected = dumps(data) assert Request.assert_data(data) == expected
def test_request_invoice_submit_data_is_required(): with pytest.raises(AssertionError): Request().submit(data=None)
def test_request_invoice_submit_data_has_to_be_dict_or_str(): with pytest.raises(AssertionError): Request().submit(data=tuple("wrong data type"))
def test_request_passing_params(): req = Request('user', 'api_key') assert req.api_user == 'user' assert req.api_key == 'api_key'
def test_request_email_data_has_to_be_dict_or_str(): with pytest.raises(AssertionError): Request().email(0, data=tuple("wrong data type"))
def test_request_email_data_is_required(): with pytest.raises(AssertionError): Request().email(0, data=None)
def test_sign_get_invoice_details_request(): res = Request(**args).get(invoice_id=1337).execute(FakeHttpModule) expected = 'IAPIS [email protected], hmac-sha1=40600699c8f72e4165b11f98c4cb93253a3cc7dc' assert_auth_header(expected, res)
def test_invoice_download_url(): res = Request().download(invoice_id=1337, print_type='dup').execute(FakeHttpModule) assert ( GET, "https://www.ifirma.pl/iapi/fakturakraj/1337.pdf.dup") == get_url(res)
""" Example shows how to use this package to authenticate arbitrary request from json file. USAGE: $ python sample_invoice/send_arbitrary_requests.py <PATH TO REQUEST.json> """ import os, sys import requests script_path = os.path.realpath(__file__) module_path = os.path.dirname(os.path.dirname(script_path)) sys.path.append(module_path) from ifirma.request import Request if __name__ == "__main__": if len(sys.argv) == 1 or not sys.argv[1].strip(): sys.exit('Provide an ifirma invoice request json') else: filename = sys.argv[1] print('Reading file: ' + filename) file_content = open(filename).read() req = Request() resp = req.submit(file_content).execute(requests) resp.raise_for_status() invoices_json = resp.json() print(invoices_json)
def test_sign_download_invoice_request(): res = Request(**args).download(invoice_id=1337).execute(FakeHttpModule) expected = 'IAPIS [email protected], hmac-sha1=e87ba74ecc88097841fe48d16040aa9675c6cb9a' assert_auth_header(expected, res)