Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from requests import Request, Session
from urllib.parse import urljoin
import os
import json
class Economics(object):
_api_endpoint = 'https://restapi.e-conomic.com/'
def __init__(self, appid=None, accessid=None):
self.appid = appid
self.accessid = accessid
def _build_url(self, url_part):
return urljoin(self._api_endpoint, url_part)
def _request(self, url_part, method, data):
if not all((self.appid, self.accessid)):
raise Exception("missing credentials for e-conomics api")
url = self._build_url(url_part)
json_ = json.dumps(data)
headers = {
'appid': self.appid,
'accessid': self.accessid,
'accept': 'application/json',
'content-type': 'application/json'}
session = Session()
request_ = Request(method, url, data=json_, headers=headers)
prepared_request = session.prepare_request(request_)
return session, prepared_request
def post_invoice(self, data):
url_part = '/invoices-experimental/drafts'
session, prepared_request = self._request(url_part, 'POST', data)
return session.send(prepared_request)
def post_invoice_route():
data = {
"date": "2014-09-01",
"currency": "DKK",
"exchangeRate": 10.25,
"netAmount": 10.00,
"netAmountInBaseCurrency": 0.00,
"grossAmount": 12.50,
"marginInBaseCurrency": -46.93,
"marginPercentage": 0.0,
"vatAmount": 2.50,
"roundingAmount": 0.00,
"costPriceInBaseCurrency": 46.93,
"paymentTerms": {
"paymentTermsNumber": 1,
"daysOfCredit": 14,
"name": "Lobende maned 14 dage",
"paymentTermsType": "invoiceMonth"
},
"customer": {
"customerNumber": 1
},
"recipient": {
"name": "GatewayAPI.com Credit",
"address": "Vejlevej 21",
"zip": "7000",
"city": "Fredericia",
"vatZone": {
"name": "Domestic",
"vatZoneNumber": 1,
"enabledForCustomer": True,
"enabledForSupplier": True
}
},
"delivery": {},
"layout": {
"layoutNumber": 9
},
"lines": [
{
"lineNumber": 1,
"sortKey": 1,
"unit": {
"unitNumber": 2,
"name": "Tim"
},
"product": {
"productNumber": "1"
},
"quantity": 1.00,
"unitNetPrice": 10.00,
"discountPercentage": 0.00,
"unitCostPrice": 46.93,
"totalNetAmount": 10.00,
"marginInBaseCurrency": -46.93,
"marginPercentage": 0.0
},
{
"lineNumber": 1,
"sortKey": 1,
"unit": {
"unitNumber": 1,
"name": "stk."
},
"product": {
"productNumber": "1"
},
"quantity": 1.00,
"unitNetPrice": 10.00,
"discountPercentage": 0.00,
"unitCostPrice": 46.93,
"totalNetAmount": 10.00,
"marginInBaseCurrency": -46.93,
"marginPercentage": 0.0
},
{
"lineNumber": 1,
"sortKey": 1,
"unit": {
"unitNumber": 4
},
"product": {
"productNumber": "1"
},
"quantity": 1.00,
"unitNetPrice": 10.00,
"discountPercentage": 0.00,
"unitCostPrice": 46.93,
"totalNetAmount": 10.00,
"marginInBaseCurrency": -46.93,
"marginPercentage": 0.0
}
]
}
economics = Economics(appid=APPID, accessid=ACCESSID)
response = economics.post_invoice(data)
try:
response.raise_for_status()
except Exception as e:
print('error response: {}'.format(e.response.json))
return
access_query = '?appid={}&accessid={}'.format(APPID, ACCESSID)
pdf_url = response.json()['pdf']['download'] + access_query
print('response code: {}'.format(response.status_code))
print('response: {}'.format(response.json()))
print('download pdf: {}'.format(pdf_url))
if __name__ == '__main__':
APPID = os.environ.get('ECONOMICS_DEVS_APPID')
ACCESSID = os.environ.get('ECONOMICS_DEVS_ACCESSID')
post_invoice_route()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.