diff --git a/examples/assignment/createAssignment.js b/examples/assignment/createAssignment.js new file mode 100644 index 0000000..33787a5 --- /dev/null +++ b/examples/assignment/createAssignment.js @@ -0,0 +1,11 @@ +const { codio, auth } = require('../auth.js') +const { courseId, assignmentData } = require('../data.js') + +async function main() { + await auth + + const result = await codio.assignment.createAssignment(courseId, assignmentData) + console.log(result) +} + +main() diff --git a/examples/course/createCourse.js b/examples/course/createCourse.js new file mode 100644 index 0000000..a90264a --- /dev/null +++ b/examples/course/createCourse.js @@ -0,0 +1,11 @@ +const { codio, auth } = require('../auth.js') +const { courseData } = require('../data.js') + +async function main() { + await auth + + const result = await codio.course.createCourse(courseData) + console.log(result) +} + +main() diff --git a/examples/course/createModule.js b/examples/course/createModule.js new file mode 100644 index 0000000..7a283a2 --- /dev/null +++ b/examples/course/createModule.js @@ -0,0 +1,11 @@ +const { codio, auth } = require('../auth.js') +const { courseId, moduleName } = require('../data.js') + +async function main() { + await auth + + const result = await codio.course.createModule(courseId, moduleName) + console.log(result) +} + +main() diff --git a/examples/data.js b/examples/data.js index 19027d1..2131e89 100644 --- a/examples/data.js +++ b/examples/data.js @@ -21,5 +21,27 @@ const leanersMapping = [ ] } ] +const moduleName = "New Module Name" +const courseData = { + name: "Introduction to Programming", + description: "This course covers the basics of programming.", + start: "2025-08-29T09:32:55Z", + end: "2027-09-25T09:12:00Z", + timezone: "America/New_York", + tags: [ + "programming", + "beginner" + ] +} +const assignmentData = { + moduleId: "your module id", + settings: { + name: "My First Assignment", + description: "This assignment covers the basics of programming.", + gigaboxSlot: { + boxType: "1gb" + } + } +} -module.exports = { courseId, courseName, assignmentId, studentId, studentEmail, studentLogin, libraryId, libraryName, projectPath, stackId, stackVersionId, archivePath, yamlMapDir, courseIdToArchive, leanersMapping } +module.exports = { courseId, courseName, assignmentId, studentId, studentEmail, studentLogin, libraryId, libraryName, projectPath, stackId, stackVersionId, archivePath, yamlMapDir, courseIdToArchive, leanersMapping, moduleName, courseData, assignmentData } diff --git a/package.json b/package.json index bb40f76..129fdb8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codio-api-js", - "version": "0.15.3", + "version": "0.16.0", "description": "JS client to Codio API", "repository": "https://github.com/codio/codio-api-js", "author": "Max Kraev ", diff --git a/src/lib/assignment.ts b/src/lib/assignment.ts index 9b34ad6..43340e0 100644 --- a/src/lib/assignment.ts +++ b/src/lib/assignment.ts @@ -447,6 +447,44 @@ export async function updateStudentTimeExtension( } } +export type ProjectSource = { + type: 'git', url: string, credentials?: {username: string, password: string} +} | { + type: 'zip' +} + +export type AssignmentData = { + moduleId: string + settings: { + name: string + description?: string + imageUrl?: string + gigaboxSlot?: { + boxType: string + } + } +} + +export async function createAssignment(courseId: string, assignmentData: AssignmentData): Promise { + const api = bent(getApiV1Url(), 'POST', 'json', 200) + try { + const res = await api(`/courses/${courseId}/assignments`, assignmentData, getBearer()) + const assignmentId = res['id'] + if (!assignmentId) { + throw new Error('assignmentId not found in response') + } + return assignmentId + } catch (error: any) { + if (error.json) { + const message = JSON.stringify(await error.json()) + console.log(message) + throw new Error(message) + } + console.log(error) + throw error + } +} + const assignment = { publish: async (courseId: string, assignmentId: string, projectPath: string, changelogOrOptions: string | PublishOptions): Promise => { @@ -459,7 +497,8 @@ const assignment = { reducePublish, updateSettings, getSettings, - updateStudentTimeExtension + updateStudentTimeExtension, + createAssignment } export default assignment diff --git a/src/lib/course.ts b/src/lib/course.ts index 97dd306..fef4b43 100644 --- a/src/lib/course.ts +++ b/src/lib/course.ts @@ -580,6 +580,51 @@ export async function filterLearnersForMentors(courseId: string, mapping: Filter } } +export type CreateCourseRequest = { + name: string, + description?: string, + start?: string, + end?: string, + timezone?: string, + tags?: string[] +} + +export async function createCourse(courseData: CreateCourseRequest): Promise { + const api = bent(getApiV1Url(), 'POST', 'json', 200) + try { + const res = await api(`/courses`, courseData, getBearer()) + const courseId = res['id'] + if (!courseId) { + throw new Error('courseId not found in response') + } + return courseId + } catch (error: any) { + if (error.json) { + const message = JSON.stringify(await error.json()) + throw new Error(message) + } + throw error + } +} + +export async function createModule(courseId: string, moduleName: string): Promise { + const api = bent(getApiV1Url(), 'POST', 'json', 200) + try { + const res = await api(`/courses/${courseId}/modules`, { name: moduleName }, getBearer()) + const moduleId = res['id'] + if (!moduleId) { + throw new Error('moduleId not found in response') + } + return moduleId + } catch (error: any) { + if (error.json) { + const message = JSON.stringify(await error.json()) + throw new Error(message) + } + throw error + } +} + const course = { assignmentStudentsProgress, info, @@ -607,7 +652,9 @@ const course = { archive, exportCoachData, exportLLMProxyData, - filterLearnersForMentors + filterLearnersForMentors, + createCourse, + createModule } export default course