This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreqlib.py
62 lines (50 loc) · 1.95 KB
/
reqlib.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
#!/usr/bin/env python3
# ---------------------------------------------------------------------
# reqlib.py
# Back end: Connects to OIT APIs
# adapted from https://github.com/vr2amesh/COS333-API-Code-Examples/
# blob/master/MobileApp/python/req_lib.py
# ---------------------------------------------------------------------
import requests
import json
from configs import Configs
import xmltodict
class ReqLib:
def __init__(self):
self.configs = Configs()
'''
This function allows a user to make a request to
a certain endpoint, with the BASE_URL of
https://api.princeton.edu:443/mobile-app
The parameters kwargs are keyword arguments. It
symbolizes a variable number of arguments
'''
def getJSON(self, endpoint, **kwargs):
req = requests.get(self.configs.BASE_URL + endpoint,
params=kwargs if "kwargs" not in kwargs else kwargs["kwargs"],
headers={"Authorization": "Bearer " + self.configs.ACCESS_TOKEN},)
text = req.text
# Check to see if the response failed due to invalid
# credentials
text = self._updateConfigs(text, endpoint, **kwargs)
return json.loads(text)
def _updateConfigs(self, text, endpoint, **kwargs):
if text.startswith("<ams:fault"):
self.configs._refreshToken(grant_type="client_credentials")
# Redo the request with the new access token
req = requests.get(self.configs.BASE_URL + endpoint,
params=kwargs if "kwargs" not in kwargs else kwargs["kwargs"],
headers={"Authorization": "Bearer " + self.configs.ACCESS_TOKEN},)
text = req.text
return text
'''
Convers XML data from API endpoint into JSON output.
'''
def getJSONfromXML(self, endpoint, **kwargs):
req = requests.get(self.configs.BASE_URL + endpoint,
params=kwargs if "kwargs" not in kwargs else kwargs["kwargs"],
headers={"Authorization": "Bearer " + self.configs.ACCESS_TOKEN},)
text = self._updateConfigs(req.text, endpoint, **kwargs)
data_dict = xmltodict.parse(text)
json_data = json.dumps(data_dict)
return json_data