-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytoast.py
207 lines (112 loc) · 6.36 KB
/
pytoast.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import requests
import json
from datetime import datetime
import dateutil.parser
import os
TOKEN_FN = "toast-token.txt"
AUTH_ENDPOINT = "usermgmt/v1/oauth/token"
ORDERS_ENDPOINT = "orders/v2/orders/"
LABOR_ENDPOINT = "labor/v1/"
EMPLOYEES_ENDPOINT = "employees/"
JOBS_ENDPOINT = "jobs/"
ISO_TIMEZONE = ".000-0400"
class PyToast(object):
def __init__(self, client_id, secret, rguid=None, sandbox=False):
self.client_id = client_id
self.secret = secret
if rguid is not None:
self.rguid = None
else:
self.rguid = rguid
if sandbox == True:
self.base_url = "https://ws-sandbox-api.eng.toasttab.com/"
else:
self.base_url = "https://ws-api.toasttab.com/"
self.auth_url = self.base_url + AUTH_ENDPOINT
self.auth_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
self.data = [
('grant_type', 'client_credentials'),
('client_id', self.client_id),
('client_secret', self.secret),
]
try:
with open(TOKEN_FN,'r') as f:
self.auth_token = f.readline()
except IOError:
self.auth_token = self.create_auth_token(self.auth_url, self.data, self.auth_headers)
with open(TOKEN_FN, 'w') as f:
f.write(self.auth_token)
self.headers = {
'Toast-Restaurant-External-ID': self.rguid,
'Authorization': 'Bearer {}'.format(self.auth_token),
'Content-Type': "application/json"
}
def get_order(self, o_guid, rguid=None):
''' Returns a JSON object containing all data relating to a specific order. '''
endpoint = self.base_url + ORDERS_ENDPOINT + o_guid
if rguid is not None: self.headers['Toast-Restaurant-External-ID'] = rguid
r = requests.get(endpoint, headers=self.headers)
if self.auth_expired(json.loads(r.content)) == True:
self.headers['Authorization'] = 'Bearer {}'.format(self.create_auth_token(self.auth_url, self.data, self.headers))
r = requests.get(endpoint, headers=self.headers)
return json.loads(r.content)
def get_multiple_orders(self, start_date, end_date, rguid=None):
''' Returns a JSON object containing all data relating to multiple orders sent within a specified time period. '''
iso_start_date = dateutil.parser.parse(start_date).isoformat() + ISO_TIMEZONE
iso_end_date = dateutil.parser.parse(end_date).isoformat() + ISO_TIMEZONE
endpoint = self.base_url + ORDERS_ENDPOINT + "?startDate={}&endDate={}".format(iso_start_date, iso_end_date)
if rguid is not None: self.headers['Toast-Restaurant-External-ID'] = rguid
r = requests.get(endpoint, headers=self.headers)
if self.auth_expired(json.loads(r.content)) == True:
self.headers['Authorization'] = 'Bearer {}'.format(self.create_auth_token(self.auth_url, self.data, self.headers))
r = requests.get(endpoint, headers=self.headers)
return json.loads(r.content)
def get_orders_by_day(self, date, rguid=None):
endpoint = self.base_url + ORDERS_ENDPOINT + "?businessDate={}".format(date)
if rguid is not None: self.headers['Toast-Restaurant-External-ID'] = rguid
r = requests.get(endpoint, headers=self.headers)
if self.auth_expired(json.loads(r.content)) == True:
self.headers['Authorization'] = 'Bearer {}'.format(self.create_auth_token(self.auth_url, self.data, self.headers))
r = requests.get(endpoint, headers=self.headers)
return json.loads(r.content)
def get_jobs(self, rguid=None):
endpoint = self.base_url + JOBS_ENDPOINT
if rguid is not None: self.headers['Toast-Restaurant-External-ID'] = rguid
r = requests.get(endpoint, headers=self.headers)
if self.auth_expired(json.loads(r.content)) == True:
self.headers['Authorization'] = 'Bearer {}'.format(self.create_auth_token(self.auth_url, self.data, self.headers))
r = requests.get(endpoint, headers=self.headers)
return json.loads(r.content)
def get_employee(self, eguid, rguid=None):
endpoint = self.base_url + LABOR_ENDPOINT + "employee/" + eguid
if rguid is not None: self.headers['Toast-Restaurant-External-ID'] = rguid
r = requests.get(endpoint, headers=self.headers)
if self.auth_expired(json.loads(r.content)) == True:
self.headers['Authorization'] = 'Bearer {}'.format(self.create_auth_token(self.auth_url, self.data, self.headers))
r = requests.get(endpoint, headers=self.headers)
return json.loads(r.content)
def get_employees(self, rguid=None):
''' Returns a list of JSON objects representing data for all employees for a specific restaurant. '''
endpoint = self.base_url + LABOR_ENDPOINT + EMPLOYEES_ENDPOINT
if rguid is not None: self.headers['Toast-Restaurant-External-ID'] = rguid
r = requests.get(endpoint, headers=self.headers)
if self.auth_expired(json.loads(r.content)) == True:
self.headers['Authorization'] = 'Bearer {}'.format(self.create_auth_token(self.auth_url, self.data, self.headers))
r = requests.get(endpoint, headers=self.headers)
return json.loads(r.content)
def create_auth_token(self, url, data, headers):
auth_token = json.loads(requests.post(url, data, headers).content)["access_token"]
return auth_token
def auth_expired(self, response):
try:
if response['message'] == 'invalid_token':
print('Token is expired/invalid. Generating new token ... ')
return True
else:
return False
except KeyError:
return False
except TypeError:
return False