Skip to content

Instantly share code, notes, and snippets.

@aambrozkiewicz
Created March 21, 2018 12:45
Show Gist options
  • Save aambrozkiewicz/bd759aa27f7e832b34bd080843d0451f to your computer and use it in GitHub Desktop.
Save aambrozkiewicz/bd759aa27f7e832b34bd080843d0451f to your computer and use it in GitHub Desktop.
import collections
import functools
import requests
Invoice = collections.namedtuple('Invoice', [
'id', 'number', 'currency', 'paid_price', 'notes', 'kind', 'payment_method', 'recipient_signature',
'seller_signature', 'invoice_date', 'sale_date', 'status', 'payment_date', 'net_price', 'tax_price',
'gross_price', 'client_id', 'client_company_name', 'client_street', 'client_city', 'client_post_code',
'client_tax_code', 'client_country', 'bank_name', 'bank_account', 'swift',
'sale_type', 'invoice_date_kind', 'services', 'vat_exemption_reason', 'extensions',
])
class InFakt:
def __init__(self, api_key):
self.api_key = api_key
def prepare_request(self, *args, **kwargs):
return functools.partial(requests.request, headers={
'X-inFakt-ApiKey': self.api_key,
})
def create_invoice(self, invoice):
response = self.prepare_request()(
'post',
'https://api.infakt.pl/v3/invoices.json',
json=invoice._asdict(),
)
response.raise_for_status()
def get_invoice(self, invoice_id):
response = self.prepare_request()(
'get',
f'https://api.infakt.pl/v3/invoices/{invoice_id}.json',
)
values = dict(zip(Invoice._fields, [None] * len(Invoice._fields)))
values.update({
k: v for k, v in response.json().items() if k in Invoice._fields
})
return Invoice(**values)
def share_links(self, invoice_id):
return self.prepare_request()(
'post',
f'https://api.infakt.pl/v3/invoices/{invoice_id}/share_links.json',
).json()
infakt = InFakt('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment