-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTripletexClient.py
More file actions
73 lines (59 loc) · 3.15 KB
/
TripletexClient.py
File metadata and controls
73 lines (59 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import base64
import json
import requests
class TripletexClient:
def __init__(self,
consumer_token=None,
employee_token=None,
expiration_date="2050-01-01",
session_token=None,
host="https://tripletex.no",
request_headers=None,
):
self.host = host
if not consumer_token and not employee_token and not session_token and not request_headers:
print("Missing needed credentials to communicate with Tripletex")
if not session_token and consumer_token is not None:
url = "{}/v2/token/session/:create".format(self.host)
querystring = {
"consumerToken": consumer_token,
"employeeToken": employee_token,
"expirationDate": expiration_date
}
response = requests.request("PUT", url, params=querystring)
session_token = response.json()['value']['token']
if session_token is not None:
auth_payload = base64.b64encode(str.encode("0:{}".format(session_token))).decode('utf-8')
self.headers_with_auth = {'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Basic {}'.format(auth_payload)}
if request_headers is not None:
self.headers_with_auth = request_headers
def make_order(self, payload):
url = "{}/v2/order".format(self.host)
payload = json.dumps(payload)
response = requests.request("POST", url, data=payload, headers=self.headers_with_auth)
order_id = response.json()['value']['id']
return order_id
def get_products(self):
url = "{}/v2/product".format(self.host)
response = requests.request("GET", url, headers=self.headers_with_auth)
tripletex_products = response.json()['values']
return tripletex_products
def make_product(self, payload):
url = "{}/v2/product".format(self.host)
return requests.request("POST", url, data=json.dumps(payload), headers=self.headers_with_auth).json()
def make_orderlines(self, payload):
url = "{}/v2/order/orderline/list".format(self.host)
requests.request("POST", url, data=json.dumps(payload), headers=self.headers_with_auth).json()
def make_employee(self, payload):
url = "{}/v2/employee".format(self.host)
return requests.request("POST", url, data=json.dumps(payload), headers=self.headers_with_auth).json()
def make_employment(self, payload):
url = "{}/v2/employee/employment".format(self.host)
return requests.request("POST", url, data=json.dumps(payload), headers=self.headers_with_auth).json()
def make_employment_details(self, payload):
url = "{}/v2/employee/employment/details".format(self.host)
return requests.request("POST", url, data=json.dumps(payload), headers=self.headers_with_auth).json()
def get_divisions(self):
url = "{}/v2/division".format(self.host)
return requests.request("GET", url, headers=self.headers_with_auth).json()