1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-25 06:49:23 +00:00

Can trigger API

This commit is contained in:
Pierre HUBERT 2020-03-25 17:36:50 +01:00
parent 1b584ef360
commit 7a1a26e919

View File

@ -7,6 +7,8 @@
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
import { time } from "../utils/DateUtils";
// Different supported actions // Different supported actions
export enum Action { export enum Action {
LOGIN_FAILED = "login_failed", LOGIN_FAILED = "login_failed",
@ -16,7 +18,7 @@ export enum Action {
/** /**
* The duration entries will be kept in the table * The duration entries will be kept in the table
*/ */
const MAX_TIME = 3600; // 1 hour const MAX_TIME = 20;//3600; // 1 hour
/** /**
* Information about a specific IP & action * Information about a specific IP & action
@ -33,6 +35,18 @@ const list : Array<CountInfo> = [];
export class APILimitHelper { export class APILimitHelper {
/**
* Find an entry in the list
*
* If none is found, returns undefined if none found
*
* @param ip The target IP address
* @param action Target action
*/
private static FindEntry(ip: string, action: Action) : CountInfo {
return list.find((f) => f.ip == ip && f.action == action);
}
/** /**
* Trigger the counter (increase it by one) * Trigger the counter (increase it by one)
* *
@ -40,7 +54,19 @@ export class APILimitHelper {
* @param action The action to check * @param action The action to check
*/ */
public static async Trigger(ip: string, action: Action) { public static async Trigger(ip: string, action: Action) {
// TODO : trigger counter const entry = this.FindEntry(ip, action);
// Check if it is the first time the API is triggered
if(entry == undefined)
list.push({
ip: ip,
action: action,
time: time(),
count: 1
})
else
entry.count++
} }
/** /**
@ -50,8 +76,9 @@ export class APILimitHelper {
* @param action The action to check * @param action The action to check
*/ */
public static async Count(ip: string, action: Action) : Promise<number> { public static async Count(ip: string, action: Action) : Promise<number> {
// TODO : return count const entry = this.FindEntry(ip, action);
return 0; console.log(entry)
return entry == undefined ? 0 : entry.count;
} }
} }