mirror of
https://github.com/classchartsapi/classcharts-api-js.git
synced 2026-05-14 19:59:37 +00:00
Docs need updating
This commit is contained in:
parent
02a2e00fee
commit
2ee2b47e41
12 changed files with 3850 additions and 74 deletions
103
src/client.ts
103
src/client.ts
|
|
@ -15,30 +15,29 @@ import {
|
|||
LessonsResponse,
|
||||
Student,
|
||||
} from "./types";
|
||||
import { API_BASE, BASE_URL } from "./consts";
|
||||
|
||||
/**
|
||||
* The base client
|
||||
*/
|
||||
export class ClasschartsClient {
|
||||
public studentCode = "";
|
||||
public dateOfBirth = "";
|
||||
public studentId = 0;
|
||||
public studentName = "";
|
||||
private authCookies: Array<string> | undefined;
|
||||
private sessionId = "";
|
||||
protected studentId = 0;
|
||||
protected studentName = "";
|
||||
protected authCookies: Array<string> | undefined;
|
||||
protected sessionId = "";
|
||||
protected API_BASE = "";
|
||||
/**
|
||||
*
|
||||
* @param studentCode Classcharts student code
|
||||
* @param dateOfBirth Student's date of birth
|
||||
*/
|
||||
constructor(studentCode: string, dateOfBirth?: string) {
|
||||
this.studentCode = String(studentCode);
|
||||
this.dateOfBirth = String(dateOfBirth);
|
||||
constructor(API_BASE: string) {
|
||||
this.API_BASE = API_BASE
|
||||
}
|
||||
public async makeAuthedRequest(
|
||||
path: string,
|
||||
options: Omit<RequestOptions, "origin" | "path">
|
||||
) {
|
||||
|
||||
if (!this.authCookies) throw new Error("Not authenticated");
|
||||
const requestOptions: Omit<RequestOptions, "origin" | "path"> = {
|
||||
...options,
|
||||
|
|
@ -47,7 +46,10 @@ export class ClasschartsClient {
|
|||
authorization: "Basic " + this.sessionId,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
const request = await Undici.request(path, requestOptions);
|
||||
|
||||
let responseJSON;
|
||||
try {
|
||||
responseJSON = await request.body.json();
|
||||
|
|
@ -55,63 +57,24 @@ export class ClasschartsClient {
|
|||
throw new Error("Invalid JSON response recieved");
|
||||
}
|
||||
if (responseJSON.success == 0) {
|
||||
|
||||
throw new Error(responseJSON.error);
|
||||
}
|
||||
|
||||
return responseJSON.data;
|
||||
}
|
||||
/**
|
||||
* Initialises the client and authenticates with classcharts
|
||||
*/
|
||||
async login(): Promise<void> {
|
||||
if (!this.studentCode) throw new Error("Student Code not inputted");
|
||||
const formData = new URLSearchParams();
|
||||
formData.append("_method", "POST");
|
||||
formData.append("code", this.studentCode.toUpperCase());
|
||||
formData.append("dob", this.dateOfBirth);
|
||||
formData.append("remember_me", "1");
|
||||
formData.append("recaptcha-token", "no-token-avaliable");
|
||||
const request = await Undici.request(BASE_URL + "/student/login", {
|
||||
method: "POST",
|
||||
body: formData.toString(),
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
if (request.statusCode != 302 || !request.headers["set-cookie"])
|
||||
throw new Error("Unauthenticated: Classcharts returned an error");
|
||||
const cookies = request.headers["set-cookie"];
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
cookies[i] = cookies[i].substring(0, cookies[i].indexOf(";"));
|
||||
}
|
||||
this.authCookies = cookies;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let sessionID: any = decodeURI(cookies[2])
|
||||
.replace(/%3A/g, ":")
|
||||
.replace(/%2C/g, ",");
|
||||
sessionID = JSON.parse(
|
||||
sessionID.substring(sessionID.indexOf("{"), sessionID.length)
|
||||
);
|
||||
this.sessionId = sessionID.session_id;
|
||||
const user = await this.getStudentInfo();
|
||||
this.studentId = user.id;
|
||||
this.studentName = user.name;
|
||||
}
|
||||
/**
|
||||
* @deprecated Use .login() instead
|
||||
*/
|
||||
async init(): Promise<void> {
|
||||
console.warn(".init() is deprecated, please use .login()");
|
||||
await this.login();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets general information about the logged in student
|
||||
* @returns Student object
|
||||
*/
|
||||
async getStudentInfo(): Promise<Student> {
|
||||
const data = await this.makeAuthedRequest(API_BASE + "/ping", {
|
||||
const data = await this.makeAuthedRequest(this.API_BASE + "/ping", {
|
||||
method: "POST",
|
||||
body: "include_date=true",
|
||||
body: "include_data=true",
|
||||
});
|
||||
|
||||
return data?.user;
|
||||
}
|
||||
/**
|
||||
|
|
@ -125,12 +88,26 @@ export class ClasschartsClient {
|
|||
options?.to && params.append("to", options?.to);
|
||||
options?.last_id && params.append("last_id", options?.last_id);
|
||||
return this.makeAuthedRequest(
|
||||
API_BASE + "/activity/" + this.studentId + "?" + params.toString(),
|
||||
this.API_BASE + "/activity/" + this.studentId + "?" + params.toString(),
|
||||
{
|
||||
method: "GET",
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
async getPupil(options?: GetActivityOptions): Promise<ActivityResponse> {
|
||||
let pupils = this.makeAuthedRequest(
|
||||
this.API_BASE + "/pupils",
|
||||
{
|
||||
method: "GET",
|
||||
}
|
||||
);
|
||||
|
||||
return pupils
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the logged in students behaviour points
|
||||
* @param options GetBehaviourOptions
|
||||
|
|
@ -143,7 +120,7 @@ export class ClasschartsClient {
|
|||
options?.from && params.append("from", options?.from);
|
||||
options?.to && params.append("to", options?.to);
|
||||
return await this.makeAuthedRequest(
|
||||
API_BASE + "/behaviour/" + this.studentId + "?" + params.toString(),
|
||||
this.API_BASE + "/behaviour/" + this.studentId + "?" + params.toString(),
|
||||
{
|
||||
method: "GET",
|
||||
}
|
||||
|
|
@ -164,7 +141,7 @@ export class ClasschartsClient {
|
|||
options?.fromDate && params.append("from", String(options?.fromDate));
|
||||
options?.toDate && params.append("to", String(options?.toDate));
|
||||
const data: Array<Homework> = await this.makeAuthedRequest(
|
||||
API_BASE + "/homeworks/" + this.studentId + "?" + params.toString(),
|
||||
this.API_BASE + "/homeworks/" + this.studentId + "?" + params.toString(),
|
||||
{
|
||||
method: "GET",
|
||||
}
|
||||
|
|
@ -191,7 +168,7 @@ export class ClasschartsClient {
|
|||
const params = new URLSearchParams();
|
||||
params.append("date", String(options?.date));
|
||||
return await this.makeAuthedRequest(
|
||||
API_BASE + "/timetable/" + this.studentId + "?" + params.toString(),
|
||||
this.API_BASE + "/timetable/" + this.studentId + "?" + params.toString(),
|
||||
{
|
||||
method: "GET",
|
||||
}
|
||||
|
|
@ -203,7 +180,7 @@ export class ClasschartsClient {
|
|||
*/
|
||||
async getBadges(): Promise<BadgesResponse> {
|
||||
return await this.makeAuthedRequest(
|
||||
API_BASE + "/eventbadges/" + this.studentId,
|
||||
this.API_BASE + "/eventbadges/" + this.studentId,
|
||||
{
|
||||
method: "GET",
|
||||
}
|
||||
|
|
@ -215,7 +192,7 @@ export class ClasschartsClient {
|
|||
*/
|
||||
async listAnnouncements(): Promise<AnnouncementsResponse> {
|
||||
return await this.makeAuthedRequest(
|
||||
API_BASE + "/announcements/" + this.studentId,
|
||||
this.API_BASE + "/announcements/" + this.studentId,
|
||||
{
|
||||
method: "GET",
|
||||
}
|
||||
|
|
@ -227,7 +204,7 @@ export class ClasschartsClient {
|
|||
*/
|
||||
async getDetentions(): Promise<DetentionsResponse> {
|
||||
return await this.makeAuthedRequest(
|
||||
API_BASE + "/detentions/" + this.studentId,
|
||||
this.API_BASE + "/detentions/" + this.studentId,
|
||||
{
|
||||
method: "GET",
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
export const BASE_URL = "https://www.classcharts.com";
|
||||
export const API_BASE = `${BASE_URL}/apiv2student`;
|
||||
export const API_BASE_STUDENT = `${BASE_URL}/apiv2student`;
|
||||
export const API_BASE_PARENT = `${BASE_URL}/apiv2parent`;
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
export * from "./client";
|
||||
export * from "./parent";
|
||||
export * from "./student";
|
||||
|
|
|
|||
82
src/parent.ts
Normal file
82
src/parent.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import Undici from "undici";
|
||||
import { RequestOptions } from "undici/types/dispatcher";
|
||||
import {
|
||||
ActivityResponse,
|
||||
AnnouncementsResponse,
|
||||
BadgesResponse,
|
||||
BehaviourResponse,
|
||||
DetentionsResponse,
|
||||
GetActivityOptions,
|
||||
GetBehaviourOptions,
|
||||
GetHomeworkOptions,
|
||||
GetLessonsOptions,
|
||||
Homework,
|
||||
HomeworksResponse,
|
||||
LessonsResponse,
|
||||
Student,
|
||||
} from "./types";
|
||||
|
||||
import {ClasschartsClient} from "./client"
|
||||
import { API_BASE_PARENT, BASE_URL } from "./consts";
|
||||
/**
|
||||
* The base client
|
||||
*/
|
||||
export class ClasschartsClientParent extends ClasschartsClient {
|
||||
public password = "";
|
||||
public email = "";
|
||||
/**
|
||||
*
|
||||
* @param studentCode Classcharts student code
|
||||
* @param dateOfBirth Student's date of birth
|
||||
*/
|
||||
constructor(email: string, password: string) {
|
||||
super(API_BASE_PARENT)
|
||||
this.email = String(email);
|
||||
this.password = String(password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises the client and authenticates with classcharts
|
||||
*/
|
||||
async login(): Promise<void> {
|
||||
if (!this.email) throw new Error("Email not inputted");
|
||||
const formData = new URLSearchParams();
|
||||
formData.append("_method", "POST");
|
||||
formData.append("email", this.email);
|
||||
formData.append("logintype", "existing");
|
||||
formData.append("password", this.password);
|
||||
formData.append("recaptcha-token", "no-token-avaliable");
|
||||
|
||||
const request = await Undici.request(BASE_URL + "/parent/login", {
|
||||
method: "POST",
|
||||
body: formData.toString(),
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
if (request.statusCode != 302 || !request.headers["set-cookie"])
|
||||
throw new Error("Unauthenticated: Classcharts returned an error");
|
||||
|
||||
const cookies = request.headers["set-cookie"];
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
cookies[i] = cookies[i].substring(0, cookies[i].indexOf(";"));
|
||||
}
|
||||
|
||||
this.authCookies = cookies;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let sessionID: any = decodeURI(cookies[2])
|
||||
.replace(/%3A/g, ":")
|
||||
.replace(/%2C/g, ",");
|
||||
sessionID = JSON.parse(
|
||||
sessionID.substring(sessionID.indexOf("{"), sessionID.length)
|
||||
);
|
||||
|
||||
this.sessionId = sessionID.session_id;
|
||||
|
||||
let pupil = await this.getPupil();
|
||||
|
||||
this.studentId = pupil[0].id;
|
||||
this.studentName = pupil[0].pupil_name;
|
||||
}
|
||||
|
||||
}
|
||||
76
src/student.ts
Normal file
76
src/student.ts
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
import Undici from "undici";
|
||||
import { RequestOptions } from "undici/types/dispatcher";
|
||||
import {
|
||||
ActivityResponse,
|
||||
AnnouncementsResponse,
|
||||
BadgesResponse,
|
||||
BehaviourResponse,
|
||||
DetentionsResponse,
|
||||
GetActivityOptions,
|
||||
GetBehaviourOptions,
|
||||
GetHomeworkOptions,
|
||||
GetLessonsOptions,
|
||||
Homework,
|
||||
HomeworksResponse,
|
||||
LessonsResponse,
|
||||
Student,
|
||||
} from "./types";
|
||||
import { API_BASE_STUDENT, BASE_URL } from "./consts";
|
||||
import {ClasschartsClient} from "./client"
|
||||
/**
|
||||
* The base client
|
||||
*/
|
||||
|
||||
export class ClasschartsClientStudent extends ClasschartsClient {
|
||||
public studentCode = "";
|
||||
public dateOfBirth = "";
|
||||
|
||||
/**
|
||||
*
|
||||
* @param studentCode Classcharts student code
|
||||
* @param dateOfBirth Student's date of birth
|
||||
*/
|
||||
constructor(studentCode: string, dateOfBirth?: string) {
|
||||
super(API_BASE_STUDENT)
|
||||
this.studentCode = String(studentCode);
|
||||
this.dateOfBirth = String(dateOfBirth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises the client and authenticates with classcharts
|
||||
*/
|
||||
async login(): Promise<void> {
|
||||
if (!this.studentCode) throw new Error("Student Code not inputted");
|
||||
const formData = new URLSearchParams();
|
||||
formData.append("_method", "POST");
|
||||
formData.append("code", this.studentCode.toUpperCase());
|
||||
formData.append("dob", this.dateOfBirth);
|
||||
formData.append("remember_me", "1");
|
||||
formData.append("recaptcha-token", "no-token-avaliable");
|
||||
const request = await Undici.request(BASE_URL + "/student/login", {
|
||||
method: "POST",
|
||||
body: formData.toString(),
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
if (request.statusCode != 302 || !request.headers["set-cookie"])
|
||||
throw new Error("Unauthenticated: Classcharts returned an error");
|
||||
const cookies = request.headers["set-cookie"];
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
cookies[i] = cookies[i].substring(0, cookies[i].indexOf(";"));
|
||||
}
|
||||
this.authCookies = cookies;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let sessionID: any = decodeURI(cookies[2])
|
||||
.replace(/%3A/g, ":")
|
||||
.replace(/%2C/g, ",");
|
||||
sessionID = JSON.parse(
|
||||
sessionID.substring(sessionID.indexOf("{"), sessionID.length)
|
||||
);
|
||||
this.sessionId = sessionID.session_id;
|
||||
const user = await this.getStudentInfo();
|
||||
this.studentId = user.id;
|
||||
this.studentName = user.name;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue