Skip to content

Commit

Permalink
Spread files
Browse files Browse the repository at this point in the history
For better readability
  • Loading branch information
a2br committed Dec 25, 2020
1 parent f9e9590 commit be4ae06
Show file tree
Hide file tree
Showing 16 changed files with 569 additions and 557 deletions.
4 changes: 1 addition & 3 deletions __tests__/isFailure.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as fs from "fs";
import * as path from "path";

import { isFailure } from "../lib/functions";

import { _loginRes } from "../lib/types";
import { _loginRes, isFailure } from "../lib/types";

const getDirectories = (srcPath: string) =>
fs
Expand Down
8 changes: 4 additions & 4 deletions lib/Session.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { _loginRes, account } from "./types";
import {
getMainAccount,
_loginRes,
account,
isFailure,
isFamilyAccount,
isStaffAccount,
isStudentAccount,
isTeacherAccount,
login,
} from "./functions";
} from "./types";
import { getMainAccount, login } from "./functions";
import { Family, Staff, Student, Teacher } from "./account_types";

export class Session {
Expand Down
4 changes: 2 additions & 2 deletions lib/account_types/Account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Session } from "../Session";
import { account } from "../types";
import { getMainAccount, isFailure } from "../functions";
import { account, isFailure } from "../types";
import { getMainAccount } from "../functions";

export class Account {
private __account: account;
Expand Down
4 changes: 2 additions & 2 deletions lib/account_types/Family.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Account } from "./Account";
import { Session } from "../Session";

import { _loginResSuccess, familyAccount } from "../types";
import { getMainAccount, isFamilyAccount } from "../functions";
import { _loginResSuccess, familyAccount, isFamilyAccount } from "../types";
import { getMainAccount } from "../functions";

export class Family extends Account {
public type: "family" = "family";
Expand Down
4 changes: 2 additions & 2 deletions lib/account_types/Staff.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Account } from "./Account";
import { Session } from "../Session";

import { _loginResSuccess, staffAccount } from "../types";
import { getMainAccount, isStaffAccount } from "../functions";
import { _loginResSuccess, staffAccount, isStaffAccount } from "../types";
import { getMainAccount } from "../functions";

export class Staff extends Account {
public type: "staff" = "staff";
Expand Down
8 changes: 4 additions & 4 deletions lib/account_types/Student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { htmlToText } from "html-to-text";
import { Account } from "./Account";
import { Session } from "../Session";

import { _loginResSuccess, studentAccount } from "../types";
import {
getMainAccount,
_loginResSuccess,
studentAccount,
isStudentAccount,
getTextbookPage,
isFailure,
} from "../functions";
} from "../types";
import { getMainAccount, getTextbookPage } from "../functions";

export class Student extends Account {
public type: "student" = "student";
Expand Down
4 changes: 2 additions & 2 deletions lib/account_types/Teacher.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Account } from "./Account";
import { Session } from "../Session";

import { _loginResSuccess, teacherAccount } from "../types";
import { getMainAccount, isTeacherAccount } from "../functions";
import { _loginResSuccess, teacherAccount, isTeacherAccount } from "../types";
import { getMainAccount } from "../functions";

export class Teacher extends Account {
public type: "teacher" = "teacher";
Expand Down
164 changes: 4 additions & 160 deletions lib/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,161 +1,5 @@
import { default as fetch } from "node-fetch";
import {
account,
studentAccount,
teacherAccount,
familyAccount,
_failureRes,
_loginRes,
_loginResSuccess,
_textbookDateRes,
_textbookDateResSuccess,
_textbookRes,
staffAccount,
_mailboxRes,
} from "../types";
import { login, getMainAccount } from "./login";
import { getTextbook, getTextbookPage } from "./student/textbook";
import { getMessages } from "./student/mailbox";

//! FUNCTIONS

//? LOGIN
/**
* @returns EcoleDirecte `/v3/login.awp` response
*/
export async function login(username: string, password: string) {
let urlencoded = new URLSearchParams();
urlencoded.append(
"data",
JSON.stringify({
identifiant: username,
motdepasse: password,
acceptationCharte: true,
})
);

let edRes = await fetch("https://api.ecoledirecte.com/v3/login.awp", {
method: "POST",
body: urlencoded,
});
let body: _loginRes = await edRes.json();
return body;
}

/**
* @returns The main account of the array
*/
export function getMainAccount(accounts: Array<account>) {
const mainAccount = accounts.find((acc) => acc.main) || accounts[0] || null;
return mainAccount;
}

//? TEXTBOOK

/**
* @param id Account id
* @param token Auth token
*/
export async function getTextbook(id: number, token: string) {
let urlencoded = new URLSearchParams();
urlencoded.append(
"data",
JSON.stringify({
token,
})
);

let edRes = await fetch(
`https://api.ecoledirecte.com/v3/Eleves/${id}/cahierdetexte.awp?verbe=get`,
{
method: "POST",
body: urlencoded,
}
);
let body: _textbookRes = await edRes.json();
return body;
}

/**
* @param id Account id
* @param token Auth token
* @param date Date of the textbook page (YYYY-MM-DD)
*/
export async function getTextbookPage(id: number, token: string, date: string) {
let urlencoded = new URLSearchParams();
urlencoded.append(
"data",
JSON.stringify({
token,
})
);
let edRes = await fetch(
`https://api.ecoledirecte.com/v3/Eleves/${id}/cahierdetexte/${date}.awp?verbe=get`,
{
method: "POST",
body: urlencoded,
}
);
let body: _textbookDateRes = await edRes.json();
return body;
}

//? MAILBOX

export async function getMessages(
id: number,
token: string,
direction: "received" | "sent" = "received"
) {
let urlencoded = new URLSearchParams();
urlencoded.append(
"data",
JSON.stringify({
token: token,
})
);
let edRes = await fetch(
`https://api.ecoledirecte.com/v3/eleves/${id}/messages.awp?verbe=getall&typeRecuperation=${direction}&orderBy=date&order=desc&page=0&itemsPerPage=100&onlyRead=&query=&idClasseur=0`,
{
method: "POST",
body: urlencoded,
}
);
let body: _mailboxRes = await edRes.json();
return body;
}

//! TYPE GUARDS

export function isFailure(data: any): data is _failureRes {
try {
return !data.token && data.code !== 200;
} catch {
return true;
}
}

/**
* @returns Whether `account` is `studentAccount`
*/
export function isStudentAccount(account: account): account is studentAccount {
return account.typeCompte === "E";
}

/**
* @returns Whether `account` is `teacherAccount`
*/
export function isTeacherAccount(account: account): account is teacherAccount {
return account.typeCompte === "P";
}

/**
* @returns Whether `account` is `familyAccount`
*/
export function isFamilyAccount(account: account): account is familyAccount {
return account.typeCompte === "1";
}

/**
* @returns Whether `account` is `staffAccount`
*/
export function isStaffAccount(account: account): account is staffAccount {
return account.typeCompte === "A";
}
export { login, getMainAccount, getTextbook, getTextbookPage, getMessages };
33 changes: 33 additions & 0 deletions lib/functions/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { default as fetch } from "node-fetch";

import { _loginRes, account } from "../types";

/**
* @returns EcoleDirecte `/v3/login.awp` response
*/
export async function login(username: string, password: string) {
let urlencoded = new URLSearchParams();
urlencoded.append(
"data",
JSON.stringify({
identifiant: username,
motdepasse: password,
acceptationCharte: true,
})
);

let edRes = await fetch("https://api.ecoledirecte.com/v3/login.awp", {
method: "POST",
body: urlencoded,
});
let body: _loginRes = await edRes.json();
return body;
}

/**
* @returns The main account of the array
*/
export function getMainAccount(accounts: Array<account>) {
const mainAccount = accounts.find((acc) => acc.main) || accounts[0] || null;
return mainAccount;
}
26 changes: 26 additions & 0 deletions lib/functions/student/mailbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { default as fetch } from "node-fetch";

import { _mailboxRes } from "../../types";

export async function getMessages(
id: number,
token: string,
direction: "received" | "sent" = "received"
) {
let urlencoded = new URLSearchParams();
urlencoded.append(
"data",
JSON.stringify({
token: token,
})
);
let edRes = await fetch(
`https://api.ecoledirecte.com/v3/eleves/${id}/messages.awp?verbe=getall&typeRecuperation=${direction}&orderBy=date&order=desc&page=0&itemsPerPage=100&onlyRead=&query=&idClasseur=0`,
{
method: "POST",
body: urlencoded,
}
);
let body: _mailboxRes = await edRes.json();
return body;
}
51 changes: 51 additions & 0 deletions lib/functions/student/textbook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { default as fetch } from "node-fetch";

import { _textbookRes, _textbookDateRes } from "../../types";

/**
* @param id Account id
* @param token Auth token
*/
export async function getTextbook(id: number, token: string) {
let urlencoded = new URLSearchParams();
urlencoded.append(
"data",
JSON.stringify({
token,
})
);

let edRes = await fetch(
`https://api.ecoledirecte.com/v3/Eleves/${id}/cahierdetexte.awp?verbe=get`,
{
method: "POST",
body: urlencoded,
}
);
let body: _textbookRes = await edRes.json();
return body;
}

/**
* @param id Account id
* @param token Auth token
* @param date Date of the textbook page (YYYY-MM-DD)
*/
export async function getTextbookPage(id: number, token: string, date: string) {
let urlencoded = new URLSearchParams();
urlencoded.append(
"data",
JSON.stringify({
token,
})
);
let edRes = await fetch(
`https://api.ecoledirecte.com/v3/Eleves/${id}/cahierdetexte/${date}.awp?verbe=get`,
{
method: "POST",
body: urlencoded,
}
);
let body: _textbookDateRes = await edRes.json();
return body;
}
17 changes: 17 additions & 0 deletions lib/types/failureRes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type _failureRes = {
code: number;
token: "";
host?: string;
message: string;
data?: {
accounts?: [];
};
};

export function isFailure(data: any): data is _failureRes {
try {
return !data.token && data.code !== 200;
} catch {
return true;
}
}
Loading

0 comments on commit be4ae06

Please sign in to comment.