-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfetch.py
More file actions
46 lines (34 loc) · 1.24 KB
/
fetch.py
File metadata and controls
46 lines (34 loc) · 1.24 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
import requests
ENDPOINT_OAUTH = 'https://api.yeeloc.com/oauth/access_token'
ENDPOINT_LOCKS = 'https://api.yeeloc.com/lock'
CLIENT_ID = 'yeeloc'
CLIENT_SECRET = 'adb03414981961952ccf40a1b4031d12'
def get_access_token(zone: str, username: str, password: str) -> str:
params = {
'grant_type': 'password',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'username': username,
'password': password,
'zone': zone
}
headers = {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
response = requests.post(ENDPOINT_OAUTH, params=params, headers=headers)
try:
return response.json()['access_token']
except KeyError:
raise ValueError("Invalid response from server, received: " + str(response.json()))
def get_locks(access_token: str):
response = requests.get(ENDPOINT_LOCKS, headers={'Authorization': 'Bearer ' + access_token}).json()
result = []
for row in response:
result.append({
'name': row['lock_name'],
'sn': row['lock_sn'],
'sign_key': row['ble_sign_key'],
'unlock_count': row['unlock_times'],
'add_time': row['add_time']
})
return result