This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
69 lines (55 loc) · 2.25 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
from datetime import datetime
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import LegacyApplicationClient
class CybApi:
_base_url = "https://in.cyb.no/"
def __init__(self, username, password, client_id, client_secret):
self.username = username
self.password = password
self.client_id = client_id
self.client_secret = client_secret
# Get a token via the Resource Owner Password Credential Grant OAuth2 API
oauth = OAuth2Session(client=LegacyApplicationClient(client_id=client_id))
self._token = oauth.fetch_token(
verify=False,
token_url=self._base_url + "o/token/",
username=username, password=password,
client_id=client_id, client_secret=client_secret
)
# Make a client for connecting to the API.
self._client = OAuth2Session(
client_id,
token=self._token,
auto_refresh_url=self._base_url + "o/token/",
auto_refresh_kwargs={
"client_id": client_id,
"client_secret": client_secret
},
token_updater=self._token_updater
)
def _token_updater(self, token):
self._token = token
def register_internrole(self, username, roles):
url = self._base_url + "api/intern/internroles"
data = {"username": username, "role": roles}
request = self._client.post(url, data=data, verify=False)
if request.status_code == 201:
return True
else:
return False
def register_card(self, user_id, card_num):
url = self._base_url + "api/core/cards"
data = {"card_number": str(card_num), "user": int(user_id)}
print(data)
request = self._client.post(url, data=data, verify=False)
print(request)
if request.status_code == 201:
return True
else:
return False
def get_users(self):
url = self._base_url + 'api/core/users'
response = self._client.get(url)
if response.status_code != 200:
raise Exception('%s: %s' % (response.status_code, response.text))
return response.json()