-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sierra.py
95 lines (73 loc) · 2.93 KB
/
Sierra.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
import requests
import json
import time
from base64 import b64encode
from pathlib import Path
class SierraAPI:
# Sierra API client
def __init__(self, apiURL, apiKey, apiSecret, debugMode=False):
self.apiURL = apiURL
self.apiKey = apiKey
self.apiSecret = apiSecret
self.debugMode = debugMode
self.token = None
self.tokenExpiration = 0
self.tokenMinLife = 10
self.cacheFile = "token.json"
cacheFile = Path(self.cacheFile)
if cacheFile.is_file():
with open(str(cacheFile), 'r') as file:
token = json.load(file)
self.token = token['access_token']
self.tokenExpiration = token['expiration_time']
def getToken(self):
if (self.tokenExpiration - time.time()) < self.tokenMinLife:
apiEncodedKey = b64encode(str.encode(self.apiKey + ':' + self.apiSecret))
response = requests.post(self.apiURL + 'token',
headers={'Authorization': 'Basic ' + str(apiEncodedKey, 'utf-8')},
data={'grant_type': 'client_credentials'})
self.token = response.json()['access_token']
self.tokenExpiration = time.time() + response.json()['expires_in']
if self.cacheFile:
data = response.json()
data['expiration_time'] = self.tokenExpiration
cacheFile = Path(self.cacheFile)
cacheFile.write_text(json.dumps(data, indent=4))
if self.debugMode:
print('Token: ' + self.token)
print('Expires in: ' + str(self.tokenExpiration - time.time()))
def get(self, path, params={}):
self.getToken()
response = requests.get(path,
headers={'Authorization': 'Bearer ' + self.token},
params=params)
if self.debugMode:
print(json.dumps(response.json(), indent=4))
return response
def post(self, path, params={}, data={}):
self.getToken()
response = requests.post(path,
headers={'Authorization': 'Bearer ' + self.token},
params=params,
data=data)
if self.debugMode:
try:
print(json.dumps(response.json(), indent=4))
except:
print("Empty response")
return response
def put(self, path, params={}, data={}):
self.getToken()
response = requests.put(path,
headers={'Authorization': 'Bearer ' + self.token},
params=params,
data=data)
if self.debugMode:
try:
print(json.dumps(response.json(), indent=4))
except:
print("Empty response")
return response
def delete(self, path, params={}):
# TODO: Implement delete method
pass