1
0
Fork 0
mirror of https://github.com/classchartsapi/classcharts-api-js.git synced 2026-05-14 19:59:37 +00:00

feat: badges support

This commit is contained in:
James Cook 2022-01-31 23:13:05 +00:00
parent 5fe9686a51
commit b35dc528a6
3 changed files with 47 additions and 3 deletions

View file

@ -8,7 +8,8 @@
"main": "./dist/index.js", "main": "./dist/index.js",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"generateDocs": "pnpm typedoc --entryPointStrategy expand ./src" "generateDocs": "pnpm typedoc --entryPointStrategy expand ./src",
"test": "echo do tests"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",

View file

@ -2,6 +2,7 @@ import Undici from "undici";
import { RequestOptions } from "undici/types/dispatcher"; import { RequestOptions } from "undici/types/dispatcher";
import { import {
ActivityResponse, ActivityResponse,
BadgesResponse,
BehaviourResponse, BehaviourResponse,
GetActivityOptions, GetActivityOptions,
GetBehaviourOptions, GetBehaviourOptions,
@ -59,7 +60,7 @@ export class ClasschartsClient {
/** /**
* Initialises the client and authenticates with classcharts * Initialises the client and authenticates with classcharts
*/ */
async init(): Promise<void> { async login(): Promise<void> {
if (!this.studentCode) throw new Error("Student Code not inputted"); if (!this.studentCode) throw new Error("Student Code not inputted");
const formData = new URLSearchParams(); const formData = new URLSearchParams();
formData.append("_method", "POST"); formData.append("_method", "POST");
@ -80,7 +81,7 @@ export class ClasschartsClient {
for (let i = 0; i < cookies.length; i++) { for (let i = 0; i < cookies.length; i++) {
cookies[i] = cookies[i].substring(0, cookies[i].indexOf(";")); cookies[i] = cookies[i].substring(0, cookies[i].indexOf(";"));
} }
this.authCookies = cookies this.authCookies = cookies;
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
let sessionID: any = decodeURI(cookies[2]) let sessionID: any = decodeURI(cookies[2])
.replace(/%3A/g, ":") .replace(/%3A/g, ":")
@ -182,4 +183,16 @@ export class ClasschartsClient {
} }
); );
} }
/**
* Gets a list of the students badges
* @returns Array of badges
*/
async getBadges(): Promise<BadgesResponse> {
return await this.makeAuthedRequest(
API_BASE + "/eventbadges/" + this.studentId,
{
method: "GET",
}
);
}
} }

View file

@ -150,3 +150,33 @@ export interface Lesson {
pupil_note_raw: string; pupil_note_raw: string;
} }
export type LessonsResponse = Array<Lesson>; export type LessonsResponse = Array<Lesson>;
export interface LessonPupilBehaviour {
// Not sure what to call this
reason: string;
score: number;
icon: string;
polarity: string;
timestamp: string;
teacher: {
title: string;
first_name: string;
last_name: string;
};
}
export interface PupilEvent {
timestamp: string;
lesson_pupil_behaviour: LessonPupilBehaviour;
event: {
label: string;
};
}
export interface Badge {
id: number;
name: string;
icon: string;
colour: string;
created_date: string;
pupil_badges: Array<PupilEvent>;
icon_url: string;
}
export type BadgesResponse = Array<Badge>;