-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.py
35 lines (28 loc) · 1020 Bytes
/
helpers.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
import pandas as pd
from urllib.request import Request, urlopen
from urllib.parse import urlencode
from urllib.error import HTTPError
import json
import settings
BASE_URL = settings.INSTANCE + "/api/v1"
def make_request(url, method="GET", post_fields={}):
request = Request("{base_url}/{call_url}".format(base_url=BASE_URL, call_url=url))
request.add_header("Authorization", f"Bearer {settings.TOKEN}")
request.method = method
if post_fields:
request.data = urlencode(post_fields, doseq=True).encode()
try:
response = urlopen(request)
except HTTPError as e:
print(e)
raise (e)
decoded_response = response.readline().decode("utf-8")
response_body = json.loads(decoded_response, object_pairs_hook=dict)
return response_body
def get_user_id(canvas, sis_id):
"""
Given a SIS user id as input and a canvas instance, return a canvas user id
"""
sis_id = int(sis_id)
user = canvas.get_user(sis_id, "sis_user_id")
return user.id