-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.py
More file actions
38 lines (30 loc) · 1.1 KB
/
connect.py
File metadata and controls
38 lines (30 loc) · 1.1 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
import requests
from exceptions import APIConnectionError
class Connect:
@staticmethod
def get_token(region, client_id, client_secret):
data = {'grant_type': 'client_credentials'}
try:
response = requests.post(
f'https://{region}.battle.net/oauth/token', data=data,
auth=(client_id, client_secret)
)
except requests.exceptions.ConnectionError as e:
raise APIConnectionError(e)
if 'error' in response.json():
e = "Bad credentials"
raise APIConnectionError(e)
return response.json().get('access_token')
@staticmethod
# returns expire time, 0 = expired/invalid
def check_token(region, token):
try:
response = requests.post(
f'https://{region}.battle.net/oauth/check_token?token={token}'
)
except requests.exceptions.ConnectionError as e:
raise APIConnectionError(e)
if 'error' in response.json().keys():
return 0
else:
return int(response.json().get('exp'))