forked from rrabraham/WebScheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule_api.py
51 lines (41 loc) · 1.46 KB
/
schedule_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
import json
import time
import requests
class ScheduleApiError(Exception):
'''
Raised if there is an error with the schedule API.
'''
pass
# The base API endpoint
base_url = 'http://umich-schedule-api.herokuapp.com/v3'
# the amount of time to wait for the schedule API
timeout_duration = 25
def get_data(relative_path):
'''
Gets data from the schedule API at the specified path.
Will raise a ScheduleApiError if unsuccessful.
Assumes API will return JSON, returns as a dictionary.
'''
timeout_at = time.time() + timeout_duration
while time.time() < timeout_at:
r = requests.get(base_url + relative_path)
if r.status_code == 200:
return json.loads(r.text)
if r.status_code == 400:
break
raise ScheduleApiError('error for url: {0} message: "{1}" code: {2}' \
.format(relative_path, r.text, r.status_code))
def get_terms():
'''
Returns a list of valid terms.
Each item in the list is a dictionary containing:
('TermCode', 'TermDescr', 'TermShortDescr')
'''
return get_data('/get_terms')
'''
You should add more functions below to get information from the API.
- In addition to the information provided in the spec, use the get_terms function
above as a model for how you might setup the other functions.
- Note some functions will need to have arguments (whereas get_terms does not).
- You may remove this comment when you start.
'''