2022-05-07 12:58:42 +01:00
|
|
|
import type { AxiosRequestConfig } from "axios";
|
2022-03-12 11:30:03 +00:00
|
|
|
import { API_BASE_STUDENT, BASE_URL } from "./consts";
|
2022-03-12 12:08:41 +00:00
|
|
|
import { ClasschartsClient } from "./baseClient";
|
2022-08-03 16:53:46 +01:00
|
|
|
import { parseCookies } from "./utils";
|
2022-03-12 11:30:03 +00:00
|
|
|
/**
|
|
|
|
|
* The base client
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export class ClasschartsStudentClient extends ClasschartsClient {
|
|
|
|
|
public studentCode = "";
|
|
|
|
|
public dateOfBirth = "";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param studentCode Classcharts student code
|
|
|
|
|
* @param dateOfBirth Student's date of birth
|
|
|
|
|
*/
|
2022-05-07 12:58:42 +01:00
|
|
|
constructor(
|
|
|
|
|
studentCode: string,
|
|
|
|
|
dateOfBirth?: string,
|
|
|
|
|
axiosConfig?: AxiosRequestConfig
|
|
|
|
|
) {
|
|
|
|
|
super(API_BASE_STUDENT, axiosConfig);
|
2022-03-12 11:30:03 +00:00
|
|
|
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");
|
2022-05-07 12:58:42 +01:00
|
|
|
const request = await this.axios.request({
|
2022-03-24 18:56:33 +00:00
|
|
|
url: BASE_URL + "/student/login",
|
2022-03-12 11:30:03 +00:00
|
|
|
method: "POST",
|
2022-03-24 18:56:33 +00:00
|
|
|
data: formData.toString(),
|
|
|
|
|
maxRedirects: 0,
|
2022-03-12 11:30:03 +00:00
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
|
},
|
2022-03-24 18:56:33 +00:00
|
|
|
validateStatus: () => true,
|
2022-03-12 11:30:03 +00:00
|
|
|
});
|
2022-03-24 18:56:33 +00:00
|
|
|
if (request.status != 302 || !request.headers["set-cookie"])
|
2022-03-12 11:30:03 +00:00
|
|
|
throw new Error("Unauthenticated: Classcharts returned an error");
|
2022-08-03 16:53:46 +01:00
|
|
|
const cookies = String(request.headers["set-cookie"]);
|
|
|
|
|
this.authCookies = cookies.split(";");
|
|
|
|
|
const sessionCookies = parseCookies(cookies);
|
|
|
|
|
const sessionID = JSON.parse(
|
|
|
|
|
String(sessionCookies["student_session_credentials"])
|
2022-03-12 11:30:03 +00:00
|
|
|
);
|
|
|
|
|
this.sessionId = sessionID.session_id;
|
|
|
|
|
const user = await this.getStudentInfo();
|
|
|
|
|
this.studentId = user.id;
|
|
|
|
|
this.studentName = user.name;
|
2022-09-23 19:40:32 +01:00
|
|
|
const pingFormData = new URLSearchParams();
|
|
|
|
|
pingFormData.append("include_data", "true");
|
|
|
|
|
const pingData = await this.makeAuthedRequest(
|
|
|
|
|
this.API_BASE + "/ping",
|
|
|
|
|
{
|
|
|
|
|
method: "POST",
|
|
|
|
|
data: pingFormData.toString(),
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ includeMeta: true }
|
|
|
|
|
);
|
|
|
|
|
this.sessionId = pingData.meta.session_id;
|
2022-03-12 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
}
|