-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestUserController.py
More file actions
165 lines (126 loc) · 6.28 KB
/
RestUserController.py
File metadata and controls
165 lines (126 loc) · 6.28 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import json
from cachetools import TTLCache
import requests
import datetime
import time
import ssl
import sys
import os
import urllib.parse
import Config
class RestUserController():
target_url = ''
def __init__(self, target_url, token):
self.target_url = target_url
self.token = token
self.courseId = "_866_1"
if Config.config['verify_certs'] == 'True':
self.verify_certs = True
else:
self.verify_certs = False
self.user_info = None
def getUserInfo(self):
return(self.user_info)
def createLearnUser(self,user):
endpoint = 'https://' + self.target_url + '/learn/api/public/v1/users'
print('[User:createLearnUser()] token: ' + self.token)
#"Authorization: Bearer $token"
authStr = 'Bearer ' + self.token
print('[User:createLearnUser()] authStr: ' + authStr)
print("[User:createLearnUser()] POST Request URL: " + endpoint)
print("[User:createLearnUser()] JSON Payload: NONE REQUIRED")
r = requests.post(endpoint, json=user, headers={'Authorization':authStr, 'Content-Type' : 'application/json'}, verify=self.verify_certs)
print("[User:createLearnUser()] STATUS CODE: " + str(r.status_code) )
#print("[User:getUser()] RESPONSE:" + str(r.text))
if r.status_code == 201 and r.text:
res = json.loads(r.text)
print(json.dumps(res,indent=4, separators=(',', ': ')))
return(res)
elif r.status_code == 409:
res = self.updateLearnUser(user)
return(res)
else:
print("[User:getUser()] RESPONSE:" + str(r.text))
return({ "http_status" : r.status_code, "result" : r.text })
def updateLearnUser(self,user):
endpoint = 'https://' + self.target_url + '/learn/api/public/v1/users/userName:' + user['userName']
print('[User:createLearnUser()] token: ' + self.token)
#"Authorization: Bearer $token"
authStr = 'Bearer ' + self.token
print('[User:createLearnUser()] authStr: ' + authStr)
print("[User:createLearnUser()] POST Request URL: " + endpoint)
print("[User:createLearnUser()] JSON Payload: NONE REQUIRED")
r = requests.patch(endpoint, json=user, headers={'Authorization':authStr, 'Content-Type' : 'application/json'}, verify=self.verify_certs)
print("[User:createLearnUser()] STATUS CODE: " + str(r.status_code) )
#print("[User:getUser()] RESPONSE:" + str(r.text))
if r.status_code == 200 and r.text:
res = json.loads(r.text)
print(json.dumps(res,indent=4, separators=(',', ': ')))
return(res)
else:
return({ "http_status" : r.status_code, "result" : r.text })
def enrollUserInAllCourses(self, userId):
for id in range(82,97):
courseId = "_" + str(id) + "_1"
endpoint = 'https://' + self.target_url + '/learn/api/public/v1/courses/' + courseId + '/users/' + userId
print('[User:createLearnUser()] token: ' + self.token)
#"Authorization: Bearer $token"
authStr = 'Bearer ' + self.token
print('[User:createLearnUser()] authStr: ' + authStr)
payload = {
"availability": {
"available": "Yes"
},
"courseRoleId": "Student"
}
print("[User:createLearnUser()] POST Request URL: " + endpoint)
print("[User:createLearnUser()] JSON Payload: NONE REQUIRED")
r = requests.put(endpoint, json=payload, headers={'Authorization':authStr, 'Content-Type' : 'application/json'}, verify=self.verify_certs)
print("[User:createLearnUser()] STATUS CODE: " + str(r.status_code) )
#print("[User:getUser()] RESPONSE:" + str(r.text))
if r.status_code == 201 and r.text:
res = json.loads(r.text)
print(json.dumps(res,indent=4, separators=(',', ': ')))
else:
print("Error registering user <" + userId +"> for session <" + courseId + ">")
def enrollUserInCourse(self, userId):
endpoint = 'https://' + self.target_url + '/learn/api/public/v1/courses/' + self.courseId + '/users/' + userId
print('[User:createLearnUser()] token: ' + self.token)
#"Authorization: Bearer $token"
authStr = 'Bearer ' + self.token
print('[User:createLearnUser()] authStr: ' + authStr)
payload = {
"availability": {
"available": "Yes"
},
"courseRoleId": "Student"
}
print("[User:createLearnUser()] POST Request URL: " + endpoint)
print("[User:createLearnUser()] JSON Payload: NONE REQUIRED")
r = requests.put(endpoint, json=payload, headers={'Authorization':authStr, 'Content-Type' : 'application/json'}, verify=self.verify_certs)
print("[User:createLearnUser()] STATUS CODE: " + str(r.status_code) )
#print("[User:getUser()] RESPONSE:" + str(r.text))
if r.status_code == 201 and r.text:
res = json.loads(r.text)
print(json.dumps(res,indent=4, separators=(',', ': ')))
else:
print("[User:getUser()] RESPONSE:" + str(r.text))
print("Error registering user <" + userId +"> for session <" + self.courseId + ">")
def getUserInfoFromLearn(self):
OAUTH_URL = 'https://' + self.target_url + '/learn/api/public/v1/users/me'
print('[User:getUser()] token: ' + self.token)
#"Authorization: Bearer $token"
authStr = 'Bearer ' + self.token
print('[User:getUser()] authStr: ' + authStr)
print("[User:getUser()] GET Request URL: " + OAUTH_URL)
print("[User:getUser()] JSON Payload: NONE REQUIRED")
r = requests.get(OAUTH_URL, headers={'Authorization':authStr}, verify=self.verify_certs)
print("[User:getUser()] STATUS CODE: " + str(r.status_code) )
#print("[User:getUser()] RESPONSE:" + str(r.text))
if r.text:
res = json.loads(r.text)
print(json.dumps(res,indent=4, separators=(',', ': ')))
self.user_info = res
return(self.user_info)
else:
print("NONE")