Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API method to archive course #52

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,36 @@ Returns `CourseExport` object
await codio.course.getWorkExportProgress(courseId, taskId)
```

#### List organization courses

List organization courses


Returns

```
ListCoursesResponse = {
courses: Course[],
nextToken: string
}
```

Example

```
await codio.course.list(<next token>, <archived>)
```


#### Archive course

Returns `Date` of archivation

```
await codio.course.archive(<courseId>)
```


## Publish Stack
This method allow to publish the stack
you need to specify
Expand Down
11 changes: 11 additions & 0 deletions examples/course/archive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { codio, auth } = require('../auth.js')
const { courseIdToArchive } = require('../data.js')

async function main() {
await auth

const result = await codio.course.archive(courseIdToArchive)
console.log(result)
}

main()
10 changes: 10 additions & 0 deletions examples/course/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { codio, auth } = require('../auth.js')

async function main() {
await auth

const result = await codio.course.list()
console.log(result)
}

main()
3 changes: 2 additions & 1 deletion examples/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ const stackId = "fb02d280-dfea-4771-9529-e01b7b65b2d5"
const stackVersionId = "d06eada5-f164-4610-82b6-3e9a0c2e5134"
const archivePath = "./examples/project/project.zip"
const yamlMapDir = "./examples/project/.guides/yamlMap"
const courseIdToArchive = "your course id"

module.exports = { courseId, courseName, assignmentId, studentId, studentEmail, studentLogin, libraryId, libraryName, projectPath, stackId, stackVersionId, archivePath, yamlMapDir }
module.exports = { courseId, courseName, assignmentId, studentId, studentEmail, studentLogin, libraryId, libraryName, projectPath, stackId, stackVersionId, archivePath, yamlMapDir, courseIdToArchive }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codio-api-js",
"version": "0.11.0",
"version": "0.12.0",
"description": "JS client to Codio API",
"repository": "https://github.com/codio/codio-api-js",
"author": "Max Kraev <[email protected]>",
Expand Down
5 changes: 3 additions & 2 deletions src/lib/assessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ function updateTags(tags: {name: string, value: string}[], id: string, hash: str
}

async function updateJSON(assessment: Assessment, base: string): Promise<void> {
const assessmentJsonFiles = await glob('*.json', {cwd: path.join(base, ASSESSMENTS_DIR), nodir: true})
const working = path.join(base, ASSESSMENTS_DIR)
const assessmentJsonFiles = await glob('*.json', {cwd: working, nodir: true})
for (const file of assessmentJsonFiles) {
const filePath = path.join(ASSESSMENTS_DIR, file)
const filePath = path.join(working, file)
const assessmentString = await fs.promises.readFile(filePath, {encoding: 'utf8'})
const assessmentData = JSON.parse(assessmentString)
if (assessmentData.taskId === assessment.taskId) {
Expand Down
27 changes: 17 additions & 10 deletions src/lib/bentWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,24 @@ export default (...args: bent.Options[]) => {
}
const resp = await api(url, body, headers) as bent.NodeResponse
const encoding = getEncodingFromArgs(args)
if (!encoding) return resp
else {
if (encoding === 'buffer') {
return resp.arrayBuffer()
} else if (encoding === 'json') {
return resp.json()
} else if (encoding === 'string') {
return resp.text()
}

if (!encoding) {
return resp
}

if (encoding === 'buffer' && resp.arrayBuffer) {
return resp.arrayBuffer()
}

if (encoding === 'json' && resp.json) {
return resp.json()
}

if (encoding === 'string' && resp.text) {
return resp.text()
}
return resp.json()

return resp.json ? resp.json() : resp
} catch (e: any) {
if (e.statusCode === 429) {
const dailyRemaining = e.headers['x-ratelimit-dailylimit-remaining']
Expand Down
67 changes: 65 additions & 2 deletions src/lib/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export type Course = {
name: string
modules: Module[]
assignments: Assignment[]
creation_date: Date
archived_date: Date
archived: boolean
}

export type StudentProgress = {
Expand Down Expand Up @@ -73,8 +76,14 @@ export type TaskResponce = {
taskId: string
}

function flattenAssignments(course: Course) {
function flattenAssignments(course: any) {
course.assignments = _.flatten(_.map(course.modules, 'assignments'))
if (course.creationDate) {
course.creation_date = new Date(course.creationDate)
}
if (course.archivedDate) {
course.archived_date = new Date(course.archivedDate)
}
}

export async function info(courseId: string, withHiddenAssignments = true): Promise<Course> {
Expand Down Expand Up @@ -453,6 +462,58 @@ export async function downloadWorkExport(courseId: string, filePath: string): Pr
return download(filePath, resp.url)
}

export type ListCoursesResponse = {
courses: Course[],
nextToken: string
}

export async function list(nextToken: string, archived?: boolean): Promise<ListCoursesResponse> {
if (!config) {
throw new Error('No Config')
}
try {
const params: any = {}
if (nextToken) {
params.nextToken = nextToken
}
if (archived !== undefined) {
params.archived = archived ? 'true' : 'false'
}
const urlParams = new URLSearchParams(params)

const resp: ListCoursesResponse = await getJson(`${getApiV1Url()}/courses_list?${urlParams.toString()}`, undefined, getBearer())
_.forEach(resp.courses, course => flattenAssignments(course))
return resp
} catch (error: any) {
if (error.json) {
const message = JSON.stringify(await error.json())
throw new Error(message)
}
throw error
}
}

export type ArchiveResponse = {
archivedDate: string
}

export async function archive(courseId: string): Promise<Date> {
if (!config) {
throw new Error('No Config')
}
try {
const api = bent(getApiV1Url(), 'POST', 'json', 200)
const resp: ArchiveResponse = await api(`/courses/${courseId}/archive`, undefined, getBearer())
return new Date(resp.archivedDate)
} catch (error: any) {
if (error.json) {
const message = JSON.stringify(await error.json())
throw new Error(message)
}
throw error
}
}

const course = {
assignmentStudentsProgress,
info,
Expand All @@ -475,7 +536,9 @@ const course = {
getSourceExports,
createSourceExport,
downloadSourceExport,
studentCourseProgress
studentCourseProgress,
list,
archive
}

export default course
Loading