-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPylogy.py
152 lines (123 loc) · 5.2 KB
/
Pylogy.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
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
#Author: Blastomussa
#Date: 1/7/22
import secrets
import requests #pip3 install requests
import configparser #for test runs
from requests_oauthlib import OAuth1 #pip3 install requests_oauthlib
class Pylogy:
def __init__(self,api_key,secret):
self.api_key = api_key
self.secret = secret
# REST Methods
def _get(self,uri):
headeroauth = OAuth1(self.api_key,self.secret,signature_method='PLAINTEXT')
return requests.get(uri, auth=headeroauth)
def _post(self,uri,data):
headeroauth = OAuth1(self.api_key,self.secret,signature_method='PLAINTEXT')
return requests.post(uri, json=data, auth=headeroauth)
def _put(self,uri,data):
headeroauth = OAuth1(self.api_key,self.secret,signature_method='PLAINTEXT')
return requests.post(uri, json=data, auth=headeroauth)
def _delete(self,uri):
headeroauth = OAuth1(self.api_key,self.secret,signature_method='PLAINTEXT')
return requests.delete(uri, auth=headeroauth)
# Schoology API calls
def view_user(self,id):
uri = 'https://api.schoology.com/v1/users/{0}'.format(id)
return self._get(uri)
def delete_user(self,id):
uri = 'https://api.schoology.com/v1/users/{0}?email_notification=0'.format(id)
return self._delete(uri)
def get_user_id(self,email,users=None):
if(not users): users = self.list_users()
for user in users:
if(user['primary_email'] == email): return user['id']
return "No Schoology ID found for: {}".format(email)
def get_school_uid(self,email,users=None):
if(not users): users = self.list_users()
for user in users:
if(user['primary_email'] == email): return user['school_uid']
return "No Schoology ID found for: {}".format(email)
def list_users(self,role_id=None):
uri = 'https://api.schoology.com/v1/users?limit=150'
if(role_id): uri += "&role_ids={}".format(role_id)
response = self._get(uri).json()
if('do not exist' not in response):
users = response['user']
links = response['links']
try:
while(links['next'] != ''):
uri = links['next']
response = self._get(uri).json()
links = response['links']
u = response['user']
users += u # append paginated results to users json
except KeyError: pass # no next page will throw KeyError
return users
else:
return "Role {} does not exist.".format(role_id)
def create_user(self, user):
uri = 'https://api.schoology.com/v1/users'
return self._post(uri,user)
def create_association(self, student_suid, parent_suid):
uri = 'https://api.schoology.com/v1/users/import/associations/parents'
association = {
'associations': {
'association': {
"student_school_uid" : student_suid,
"parent_school_uid": parent_suid
}
}
}
return self._post(uri,association)
def delete_association(self, student_suid, parent_suid):
uri = 'https://api.schoology.com/v1/users/import/associations/parents'
association = {
'associations': {
'association': {
"student_school_uid" : student_suid,
"parent_school_uid": parent_suid,
"delete": 1
}
}
}
return self._post(uri,association)
# Author specific class methods
def create_parent(self, fname, lname, email):
# school_uid should be sufficiently random to prevent collisionsq
school_uid = lname.lower() + fname.lower() + "_" + secrets.token_hex(4)
# cleanup text
s = school_uid.replace(" ","") #no spaces in school_uid
school_uid = s.encode(encoding="ascii",errors="ignore").decode() #only ascii chars
email = email.lower() #only lowercase chars emails
# create and return new user json
user = {
'school_uid': school_uid, #REQUIRED: Needs to be Unique
'name_first': fname, #REQUIRED
'name_last': lname, #REQUIRED
'primary_email': email, #REQUIRED
'role_id': 817463, #REQUIRED
'email_login_info': 1, #NEEDED FOR ONBOARDING
'tz_offset': -4,
'tz_name': 'America/New_York',
'update_existing': 1 #NEEDED
}
return self.create_user(user)
def test():
# Get API key and secret from config file
try:
config = configparser.ConfigParser()
config.read('config.ini')
API_KEY = config['SCHOOLOGY_CLIENT']['API_KEY']
SECRET = config['SCHOOLOGY_CLIENT']['SECRET']
except configparser.Error:
print("Configuration Error...config.ini not found")
exit()
except KeyError:
print("Configuration Error...config not found")
exit()
URI = 'https://api.schoology.com/v1/users'
#initialize Pylogy
pylogy = Pylogy(API_KEY,SECRET)
if __name__ == '__main__':
test()