def load_enrollments(request_client: RequestClient, section_id: str, enrollments: List[Dict]) -> List[Dict]: """ Load a list of enrollments for a course via the Schoology API. Parameters ---------- request_client : RequestClient A RequestClient initialized for access to the Schoology API section_id : str The Schoolgy internal section id the enrollments are to be associated with enrollments: List[Dict] A list of JSON-like enrollment objects in a form suitable for submission to the Schoology enrollment creation endpoint. Returns ------- List[Dict] A list of JSON-like enrollment objects incorporating the response values from the Schoology API, e.g. resource ids and status from individual POSTing """ assert (len(enrollments) > 0 and len(enrollments) < 51 ), "Number of enrollments must be between 1 and 50" logger.info("Creating %s enrollments via Schoology API", len(enrollments)) enrollments_json = {"enrollments": {"enrollment": enrollments}} post_response: List[Dict] = request_client.bulk_post( f"sections/{section_id}/enrollments", enrollments_json)["enrollments"]["enrollment"] validate_multi_status(post_response) logger.info("Successfully created %s enrollments", len(post_response)) with_schoology_response_df: DataFrame = merge( json_normalize(enrollments), json_normalize(post_response).drop(["uid"], axis=1), left_index=True, right_index=True, ) return with_schoology_response_df.to_dict("records")
def load_sections(request_client: RequestClient, course_id: str, sections: List[Dict]) -> List[Dict]: """ Load a list of sections for a course via the Schoology API. Parameters ---------- request_client : RequestClient A RequestClient initialized for access to the Schoology API course_id : str The Schoolgy internal course id the sections are to be associated with sections: List[Dict] A list of JSON-like section objects in a form suitable for submission to the Schoology section creation endpoint. Returns ------- List[Dict] A list of JSON-like section objects incorporating the response values from the Schoology API, e.g. resource ids and status from individual POSTing """ assert (len(sections) > 0 and len(sections) < 51), "Number of sections must be between 1 and 50" logger.info("Creating %s sections via Schoology API", len(sections)) sections_json = {"sections": {"section": sections}} post_response: List[Dict] = request_client.bulk_post( f"courses/{course_id}/sections", sections_json)["section"] validate_multi_status(post_response) logger.info("Successfully created %s sections", len(post_response)) with_schoology_response_df: DataFrame = merge( json_normalize(sections), json_normalize(post_response).drop( ["section_school_code", "grading_periods"], axis=1), left_index=True, right_index=True, ) return with_schoology_response_df.to_dict("records")
def load_courses(request_client: RequestClient, courses: List[Dict]) -> List[Dict]: """ Load a list of courses via the Schoology API. Parameters ---------- request_client : RequestClient A RequestClient initialized for access to the Schoology API courses: List[Dict] A list of JSON-like course objects in a form suitable for submission to the Schoology course creation endpoint. Returns ------- List[Dict] A list of JSON-like course objects incorporating the response values from the Schoology API, e.g. resource ids and status from individual POSTing """ assert (len(courses) > 0 and len(courses) < 51), "Number of courses must be between 1 and 50" logger.info("Creating %s courses via Schoology API", len(courses)) courses_json = {"courses": {"course": courses}} post_response: List[Dict] = request_client.bulk_post( "courses", courses_json)["course"] validate_multi_status(post_response) logger.info("Successfully created %s courses", len(post_response)) with_schoology_response_df: DataFrame = merge( json_normalize(courses), json_normalize(post_response).drop("course_code", axis=1), left_index=True, right_index=True, ) return with_schoology_response_df.to_dict("records")