Added the create project and item functions for the mark a course as completed app. Reorganized a bit and added classes for the project as well.

This commit is contained in:
Norm Rasmussen
2024-08-31 08:26:18 -04:00
parent 6936530289
commit e49d3d0a01
8 changed files with 152 additions and 5 deletions

View File

@ -25,7 +25,7 @@ def get(url):
def post(url, payload):
try:
post_response = requests.get(url, headers=HEADERS, json=payload)
post_response = requests.post(url, headers=HEADERS, json=payload)
print(f"Executed Post Request. Status code is {get_response.status_code}")
except HTTPError as h:
print(
@ -34,3 +34,15 @@ def post(url, payload):
finally:
json_post = get_response.json()
# PP.pprint(json_post)
def delete(url):
try:
get_response = requests.delete(url, headers=HEADERS)
# print(f"Executed Get Request. Status code is {get_response.status_code}")
except HTTPError as h:
print(
f"Error occurred. Here's the info: {h} and status code: {get_response.status_code}"
)
finally:
return get_response

View File

@ -0,0 +1 @@
SANDBOX = "SlpQlju219WnWogn94dQUT6Yt"

View File

@ -0,0 +1,60 @@
import requests
import Apikeys
import pprint
import json
PP = pprint.PrettyPrinter(indent=4)
APIKEY = Apikeys.SANDBOX
HEADERS = {"content-type": "application/json", "X-Api-Key": APIKEY}
BASEURL = "https://api.northpass.com/v2"
def get(url):
try:
get_response = requests.get(url, headers=HEADERS)
# print(f"Executed Get Request. Status code is {get_response.status_code}")
except HTTPError as h:
print(
f"Error occurred. Here's the info: {h} and status code: {get_response.status_code}"
)
finally:
json_get = get_response.json()
# PP.pprint(json_get)
return json_get
def post(url, payload):
try:
post_response = requests.post(url, headers=HEADERS, json=payload)
print(f"Executed Post Request. Status code is {post_response.status_code}")
# if post_response.status_code == 404:
# print(f"Received 404 Response. Here's the returned text: {post_response.text}")
except HTTPError as h:
print(
f"Error occurred. Here's the info: {h} and status code: {post_response.status_code}"
)
finally:
try:
json_post = post_response.json()
# PP.pprint(json_post)
return json_post
except JSONDecodeError as j:
print(
f"Error occurred. Here's the info: {h} and status code: {post_response.status_code}"
)
return post_response
finally:
pass
def delete(url):
try:
get_response = requests.delete(url, headers=HEADERS)
# print(f"Executed Get Request. Status code is {get_response.status_code}")
except HTTPError as h:
print(
f"Error occurred. Here's the info: {h} and status code: {get_response.status_code}"
)
finally:
return get_response

View File

@ -14,6 +14,37 @@ Order of operations:
8. Run migration.
"""
class Project:
proj_url = "projects"
def __init__(self, proj_id: str):
self.proj_id = proj_id
class Item:
item_url = "items"
def __init__(self, item_id: str):
self.item_id = item_id
class Courses:
course_url = "courses"
def __init__(self, course_uuid, course_name):
self.course_uuid = course_uuid
self.course_name = course_name
class People:
ppl_url = "people"
def __init__(self, ppl_uuid, ppl_email):
self.ppl_uuid = ppl_uuid
self.ppl_email = ppl_email
def get_people():
email = "norm@rsmsn.co"
if type(email) is str:
@ -44,10 +75,13 @@ def get_individual_course(name, learner_uuid):
print(f"Cool. Course {single_uuid} exists. Checking enrollments.")
enrollment = get_enrollment_status(single_uuid, learner_uuid)
def get_enrollment_status(uuid, learner_uuid):
enrollment_url = f"{baseurl}/courses/{uuid}/enrollments?filter[person_id][eq]={learner_uuid}"
enrollment_url = (
f"{baseurl}/courses/{uuid}/enrollments?filter[person_id][eq]={learner_uuid}"
)
enrolled = Calls.get(enrollment_url)
if enrolled['data'] == "":
if enrolled["data"] == "":
print("Oof, no enrollments. Not to worry. We'll get one created for you.")
"""
If the learner isn't enrolled yet, we need to create one first. Here's the URL:
@ -66,6 +100,32 @@ def get_enrollment_status(uuid, learner_uuid):
)"""
def create_project_item():
project_name = "Mark Course as Complete"
item_name = "Courses to Mark as Complete"
proj_payload = {
"data": {
"type": "migration_projects",
"attributes": {"name": project_name},
}
}
proj_url = f"{baseurl}/migration/{Project.proj_url}"
print(proj_url)
tmp_p = Calls.post(proj_url, proj_payload)
p = Project(tmp_p['data']['id'])
print(p.proj_id)
item_url = f"{baseurl}/migration/{Project.proj_url}/{p.proj_id}/{Item.item_url}"
print(item_url)
item_payload = {
"data": {
"type": "migration_items",
"attributes": {"type": "courses"},
}
}
i = Item(Calls.post(item_url, item_payload))
def get_group_course(list):
multiple_uuids = []
for person in email:
@ -107,6 +167,7 @@ def get_group_person(list):
return multiple_uuids
if name == "main":
get_people()
if __name__ == "__main__":
create_project_item()
# get_people()
# get_courses()

View File

@ -123,12 +123,24 @@ def get_all_projects():
"""
Returns all projects.
"""
project_ids = []
url = f"{BASEURL}/projects"
ret = Calls.get(url)
for items in ret["data"]:
project_ids.append(items['id'])
print(f"{ items['attributes']['name'] } -- { items['id'] }")
return project_ids
def delete_all_projects():
"""
Deletes all projects after returning the uuids of the projects currently available.
"""
project_ids = get_all_projects()
for proj in project_ids:
url = f"{BASEURL}/projects/{proj}"
Calls.delete(url)
def get_specific_project():
"""
@ -140,6 +152,7 @@ def get_specific_project():
if __name__ == "__main__":
# delete_all_projects()
# read_json_docs()
# create_project()
get_all_projects()