1
0
Fork 0
mirror of https://github.com/classchartsapi/classcharts-api-js.git synced 2026-05-14 11:58:13 +00:00
classcharts-api-js/src/baseClient.ts

246 lines
7 KiB
TypeScript
Raw Normal View History

2022-03-24 18:56:33 +00:00
import axios from "axios";
2022-05-07 12:58:42 +01:00
import type { AxiosRequestConfig, AxiosInstance } from "axios";
2022-03-24 18:56:33 +00:00
import type {
2022-01-31 22:32:29 +00:00
ActivityResponse,
2022-02-06 15:45:28 +00:00
AnnouncementsResponse,
2022-05-25 18:18:31 +01:00
AttendanceResponse,
2022-01-31 23:13:05 +00:00
BadgesResponse,
2022-01-31 22:32:29 +00:00
BehaviourResponse,
DetentionsResponse,
2022-01-31 22:32:29 +00:00
GetActivityOptions,
GetAttendanceOptions,
2022-01-31 22:32:29 +00:00
GetBehaviourOptions,
GetFullActivityOptions,
2022-01-31 22:32:29 +00:00
GetHomeworkOptions,
GetLessonsOptions,
Homework,
HomeworksResponse,
LessonsResponse,
Student,
} from "./types";
2021-10-29 15:46:33 +01:00
/**
* The base client
*/
2021-10-28 16:51:07 +01:00
export class ClasschartsClient {
2022-03-12 11:30:03 +00:00
protected studentId = 0;
protected studentName = "";
2022-07-21 15:46:08 +01:00
public authCookies: Array<string> | undefined;
public sessionId = "";
2022-03-12 11:30:03 +00:00
protected API_BASE = "";
2022-05-07 12:58:42 +01:00
protected axios: AxiosInstance;
2022-01-31 22:32:29 +00:00
/**
*
2022-03-12 11:30:03 +00:00
* @param API_BASE Base API URL, this is different depending if its called as a parent or student
2022-01-31 22:32:29 +00:00
*/
2022-05-07 12:58:42 +01:00
constructor(API_BASE: string, axiosConfig?: AxiosRequestConfig) {
2022-03-12 11:37:28 +00:00
this.API_BASE = API_BASE;
2022-07-21 15:46:08 +01:00
this.axios = axios.create({
...axiosConfig,
headers: {
"User-Agent": "classcharts-api.js",
...axiosConfig?.headers,
},
validateStatus: () => true,
});
2022-01-31 22:32:29 +00:00
}
public async makeAuthedRequest(
path: string,
2022-03-24 18:56:33 +00:00
options: Omit<AxiosRequestConfig, "path">
2022-01-31 22:32:29 +00:00
) {
if (!this.authCookies) throw new Error("Not authenticated");
2022-03-24 18:56:33 +00:00
const requestOptions: AxiosRequestConfig = {
2022-01-31 22:32:29 +00:00
...options,
2022-03-24 18:56:33 +00:00
url: path,
2022-01-31 22:32:29 +00:00
headers: {
Cookie: this.authCookies.join(";"),
authorization: "Basic " + this.sessionId,
2022-07-21 15:46:08 +01:00
...options.headers,
2022-01-31 22:32:29 +00:00
},
};
2022-05-07 12:58:42 +01:00
const request = await this.axios.request(requestOptions);
2022-03-24 18:56:33 +00:00
const responseJSON = request.data;
2022-01-31 22:32:29 +00:00
if (responseJSON.success == 0) {
throw new Error(responseJSON.error);
}
return responseJSON.data;
}
2022-03-12 11:30:03 +00:00
2022-01-31 22:32:29 +00:00
/**
* Gets general information about the logged in student
* @returns Student object
*/
async getStudentInfo(): Promise<Student> {
2022-03-12 11:30:03 +00:00
const data = await this.makeAuthedRequest(this.API_BASE + "/ping", {
2022-01-31 22:32:29 +00:00
method: "POST",
2022-03-24 18:56:33 +00:00
data: "include_data=true",
2022-01-31 22:32:29 +00:00
});
2022-03-12 11:30:03 +00:00
2022-01-31 22:32:29 +00:00
return data?.user;
}
/**
* This function is only used for pagination, you likely want client.getFullActivity
2022-01-31 22:32:29 +00:00
* @param options GetActivityOptions
* @returns Activity data
*/
async getActivity(options?: GetActivityOptions): Promise<ActivityResponse> {
const params = new URLSearchParams();
options?.from && params.append("from", options?.from);
2022-01-31 22:32:29 +00:00
options?.to && params.append("to", options?.to);
2022-02-06 11:13:23 +00:00
options?.last_id && params.append("last_id", options?.last_id);
2022-01-31 22:32:29 +00:00
return this.makeAuthedRequest(
2022-03-12 11:30:03 +00:00
this.API_BASE + "/activity/" + this.studentId + "?" + params.toString(),
2022-01-31 22:32:29 +00:00
{
method: "GET",
}
);
}
/**
* Helper function for getActivity, returns all the data from the selected dates
* @param options GetFullActivityOptions
* @returns Activity Data
*/
async getFullActivity(
options: GetFullActivityOptions
): Promise<ActivityResponse> {
let data: ActivityResponse = [];
let prevLast: number | undefined;
let gotData = true;
while (gotData) {
const params: GetActivityOptions = {
from: options.from,
to: options.to,
};
if (prevLast) {
params.last_id = String(prevLast);
}
const fragment = await this.getActivity(params);
if (!fragment || !fragment.length) {
gotData = false;
} else {
data = data.concat(fragment);
prevLast = fragment[fragment.length - 1].id;
}
}
return data;
}
2022-01-31 22:32:29 +00:00
/**
* Gets the logged in students behaviour points
* @param options GetBehaviourOptions
* @returns Array of behaviour points
*/
async getBehaviour(
options?: GetBehaviourOptions
): Promise<BehaviourResponse> {
const params = new URLSearchParams();
options?.from && params.append("from", options?.from);
2022-01-31 22:32:29 +00:00
options?.to && params.append("to", options?.to);
return await this.makeAuthedRequest(
2022-03-12 11:30:03 +00:00
this.API_BASE + "/behaviour/" + this.studentId + "?" + params.toString(),
2022-01-31 22:32:29 +00:00
{
method: "GET",
}
);
}
/**
* Gets a list of the logged in student's homeworks
* @param options GetHomeworkOptions
* @returns Array of homeworks
*/
async listHomeworks(
options?: GetHomeworkOptions
): Promise<HomeworksResponse> {
const params = new URLSearchParams();
if (options?.displayDate)
params.append("display_date", String(options?.displayDate));
2022-01-31 22:32:29 +00:00
options?.fromDate && params.append("from", String(options?.fromDate));
options?.toDate && params.append("to", String(options?.toDate));
2022-01-31 22:38:17 +00:00
const data: Array<Homework> = await this.makeAuthedRequest(
2022-03-12 11:30:03 +00:00
this.API_BASE + "/homeworks/" + this.studentId + "?" + params.toString(),
2022-01-31 22:32:29 +00:00
{
method: "GET",
}
);
2022-01-31 22:32:29 +00:00
for (let i = 0; i < data.length; i++) {
data[i].description_raw = data[i].description;
2022-01-31 22:32:29 +00:00
// homework.lesson.replace(/\\/g, '')
data[i].description = data[i].description.replace(/(<([^>]+)>)/gi, "");
data[i].description = data[i].description.replace(/&nbsp;/g, "");
data[i].description = data[i].description.trim();
}
return data;
}
/**
* Gets the logged in student's lessons for a day
* @param options GetLessonsOptions
* @returns Array of lessons
*/
async getLessons(options?: GetLessonsOptions): Promise<LessonsResponse> {
if (!options?.date) throw new Error("No date specified");
const params = new URLSearchParams();
params.append("date", String(options?.date));
return await this.makeAuthedRequest(
2022-03-12 11:30:03 +00:00
this.API_BASE + "/timetable/" + this.studentId + "?" + params.toString(),
2022-01-31 22:32:29 +00:00
{
method: "GET",
}
);
}
2022-01-31 23:13:05 +00:00
/**
* Gets a list of the students badges
* @returns Array of badges
*/
async getBadges(): Promise<BadgesResponse> {
return await this.makeAuthedRequest(
2022-03-12 11:30:03 +00:00
this.API_BASE + "/eventbadges/" + this.studentId,
2022-01-31 23:13:05 +00:00
{
method: "GET",
}
);
}
2022-02-06 15:14:48 +00:00
/**
2022-02-06 15:45:28 +00:00
* Lists the logged in student's announcements
* @returns Array of announcements
2022-02-06 15:14:48 +00:00
*/
2022-02-06 15:45:28 +00:00
async listAnnouncements(): Promise<AnnouncementsResponse> {
2022-02-06 15:14:48 +00:00
return await this.makeAuthedRequest(
2022-03-12 11:30:03 +00:00
this.API_BASE + "/announcements/" + this.studentId,
2022-02-06 15:14:48 +00:00
{
method: "GET",
}
);
}
/**
* Gets the logged in student's detentions
* @returns Array of detentions
*/
async getDetentions(): Promise<DetentionsResponse> {
return await this.makeAuthedRequest(
2022-03-12 11:30:03 +00:00
this.API_BASE + "/detentions/" + this.studentId,
{
method: "GET",
}
);
}
2022-05-25 18:18:31 +01:00
/**
* Gets the logged in student's attendance
* @param options GetAttendanceOptions
2022-05-25 18:18:31 +01:00
* @returns Array of dates of attendance
*/
async listAttendance(
options?: GetAttendanceOptions
): Promise<AttendanceResponse> {
const params = new URLSearchParams();
options?.from && params.append("from", options?.from);
options?.to && params.append("to", options?.to);
2022-05-25 18:18:31 +01:00
return await this.makeAuthedRequest(
this.API_BASE + "/attendance/" + this.studentId + "?" + params.toString(),
2022-05-25 18:18:31 +01:00
{
method: "GET",
}
);
}
2021-10-28 16:51:07 +01:00
}