-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
74 lines (54 loc) · 1.64 KB
/
api.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
#!/usr/bin/env python
import requests
import json
user_data = {}
# Open user.json file to get credentials
with open('user.json') as user_file:
user_data = json.load(user_file)
url_root = user_data['api_url']
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'BookFetch Roaming Scanner v1.0'
}
# Returns a string, blank if request failed
def getAccessToken():
url = url_root + '/oauth/token'
# Construct the data dict
data = {
'grant_type': 'password',
'client_id': user_data['client_id'],
'client_secret': user_data['client_secret'],
'username': user_data['username'],
'password': user_data['password'],
'scope': ''
}
try:
r = requests.post( url,
headers=headers,
data=data,
timeout=10)
rVals = json.loads(r.text)
return rVals['access_token']
except:
return ''
# POST a new 'scan' to the API
def record(access_token, shop_code, isbn):
url = url_root + '/api/scan'
# Construct the headers dict
authheaders = headers
authheaders['Authorization'] = 'Bearer ' + access_token
# Construct the data dict
data = {
'isbn': isbn,
'shop_code': shop_code
}
# Make the API call
try:
response = requests.post(url,
headers=authheaders,
data=data,
timeout=10
)
except:
return 404
return response.status_code