Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
116 changes: 116 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ import { UpdateGroupData } from "./types/UpdateGroupData";
import { UpdateProjectData } from "./types/UpdateProjectData";
import { UpdateRoleData } from "./types/UpdateRoleData";
import { User } from "./types/User";
import { AssignmentId } from "./types/AssignmentId";
import { CreateAssignmentData } from "./types/CreateAssignmentData";
import { Assignment } from "./types/Assignment";
import { SubmissionId } from "./types/SubmissionId";
import { CreateSubmissionData } from "./types/CreateSubmissionData";
import { UpdateAssignmentData } from "./types/UpdateAssignmentData";
import { Submission } from "./types/Submission";

export default class NetsBloxApi {
private baseUrl: string;
Expand Down Expand Up @@ -237,6 +244,115 @@ export default class NetsBloxApi {
return await this.fetchJson(`/groups/id/${encodeURIComponent(id)}/members`);
}

async createAssignment(
id: GroupId,
data: CreateAssignmentData,
): Promise<Assignment> {
return await this.post(
`/groups/id/${encodeURIComponent(id)}/assignments/`,
data,
);
}

async listGroupAssignments(id: GroupId): Promise<Assignment[]> {
return await this.fetchJson(
`/groups/id/${encodeURIComponent(id)}/assignments/`,
);
}

async viewAssignment(
group_id: GroupId,
id: AssignmentId,
): Promise<Assignment> {
return await this.fetchJson(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${id}/`,
);
}

async editAssignment(
group_id: GroupId,
id: AssignmentId,
data: UpdateAssignmentData,
): Promise<Group> {
const opts = {
method: "patch",
body: JSON.stringify(data),
};
return await this.fetchJson(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${id}/`,
opts,
);
}

async deleteAssignment(
group_id: GroupId,
id: AssignmentId,
): Promise<Assignment> {
const opts = { method: "delete" };
return await this.fetchJson(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(id)}/`,
opts,
);
}

async createSubmission(
group_id: GroupId,
id: AssignmentId,
data: CreateSubmissionData,
): Promise<Submission> {
return await this.post(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(id)}/submissions/`,
data,
);
}

async viewSubmission(group_id: GroupId, assignment_id: AssignmentId, id: SubmissionId): Promise<Submission> {
return await this.fetchJson(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/id/${encodeURIComponent(id)}/`,
);
}

async viewAssignmentSubmissions(
group_id: GroupId,
assignment_id: AssignmentId,
): Promise<Submission[]> {
return await this.fetchJson(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/`,
);
}

async viewUserSubmissions(
group_id: GroupId,
assignment_id: AssignmentId,
username: string,
): Promise<Submission[]> {
return await this.fetchJson(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/user/${encodeURIComponent(username)}/`,
);
}

async viewSubmissionXml(
group_id: GroupId,
assignment_id: AssignmentId,
id: SubmissionId,
): Promise<string> {
return await this.fetchText(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/id/${encodeURIComponent(id)}/xml/`,
);
}

async deleteSubmission(
group_id: GroupId,
assignment_id: AssignmentId,
id: SubmissionId,
): Promise<Submission> {
const opts = { method: "delete" };
return await this.fetchJson(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/id/${encodeURIComponent(id)}/`,
opts,
);
}

////////////////////////////// Projects //////////////////////////////
async createProject(data: CreateProjectData): Promise<ProjectMetadata> {
const opts = {
Expand Down
29 changes: 27 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,27 @@ export default class Cloud {
username: string;
projectId: string | null;
roleId: string | null;
groupId: string | null;
newProjectRequest: Promise<any> | undefined;
localize: (text: string) => string;
token: string | null;
api: NetsBloxApi;

constructor(url, clientId, username, localize = defaultLocalizer) {
constructor(url, clientId, username, groupId, localize = defaultLocalizer) {
this.clientId = clientId;
this.username = username;
this.projectId = null;
this.roleId = null;
this.groupId = groupId;
this.url = url;
this.token = null; // only needed in NodeJs
this.localize = localize;
this.api = new NetsBloxApi(this.url);
}
}

clear() {
this.username = null;
this.groupId = null;
this.token = null;
}

Expand Down Expand Up @@ -96,6 +99,7 @@ export default class Cloud {
const response = await this.post("/users/login", body);
const user = await response.json();
this.username = user.username;
this.groupId = user.groupId;
if (isNodeJs) {
const cookie = response.headers.get("set-cookie");
if (!cookie) throw new CloudError("No cookie received");
Expand Down Expand Up @@ -677,6 +681,27 @@ export default class Cloud {
await this.post(`/libraries/user/${this.username}/${name}/unpublish`);
}

async listGroupAssignments() {
const response = await this.fetch( `/groups/id/${encodeURIComponent(this.groupId)}/assignments/`, );
return await response.json();
}

async saveSubmission(assignmentId, xml) {
const body = { owner: this.username, xml: xml };
const response = await this.post(
`/groups/id/${encodeURIComponent(this.groupId)}/assignments/id/${encodeURIComponent(assignmentId)}/submissions/`,
body,
);
return await response.json();
}

async viewSubmissionXml( group_id, assignment_id, id) {
const response = await this.fetch(
`/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/id/${encodeURIComponent(id)}/xml/`,
);
return await response.text();
}

// Cloud: user messages (to be overridden)

message(string) {
Expand Down
5 changes: 5 additions & 0 deletions src/types/Assignment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { AssignmentId } from "./AssignmentId";
import type { GroupId } from "./GroupId";

export interface Assignment { id: AssignmentId, groupId: GroupId, name: string, originTime: any, dueDate: any, }
3 changes: 3 additions & 0 deletions src/types/AssignmentId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type AssignmentId = string;
4 changes: 4 additions & 0 deletions src/types/CreateAssignmentData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { GroupId } from "./GroupId";

export interface CreateAssignmentData { name: string, dueDate: any, }
3 changes: 3 additions & 0 deletions src/types/CreateSubmissionData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface CreateSubmissionData { owner: string, xml: string, }
5 changes: 5 additions & 0 deletions src/types/Submission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { AssignmentId } from "./AssignmentId";
import type { SubmissionId } from "./SubmissionId";

export interface Submission { id: SubmissionId, assignmentId: AssignmentId, owner: string, originTime: any, }
3 changes: 3 additions & 0 deletions src/types/SubmissionId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type SubmissionId = string;
3 changes: 3 additions & 0 deletions src/types/UpdateAssignmentData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface UpdateAssignmentData { name: string | null, dueDate?: any, }