-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(requests): Add custom request handler
- Loading branch information
Marco Crespi
committed
Mar 30, 2020
1 parent
6c29944
commit eb3adc7
Showing
5 changed files
with
55 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { IMClient } from './client'; | ||
|
||
// tslint:disable-next-line: variable-name | ||
const RequestHandler = require('eris/lib/rest/RequestHandler'); | ||
|
||
export interface RequestStat { | ||
total: number; | ||
succeeded: number; | ||
errors: number; | ||
} | ||
|
||
export class IMRequestHandler extends RequestHandler { | ||
public requestStats: Map<string, RequestStat> = new Map(); | ||
|
||
public constructor(client: IMClient, forceQueueing?: boolean) { | ||
super(client, forceQueueing); | ||
} | ||
|
||
public request(method: string, url: string, auth: boolean, body: any, file: any, _route: string, short: string) { | ||
// This is similar to https://github.com/abalabahaha/eris/blob/master/lib/rest/RequestHandler.js#L46 | ||
// but we don't actually care about rate limits, so no exceptions in grouping | ||
const route = url | ||
.replace(/\/([a-z-]+)\/(?:[0-9]{17,19})/g, (_, p) => `/${p}/:id`) | ||
.replace(/\/reactions\/[^/]+/g, '/reactions/:id') | ||
.replace(/^\/webhooks\/(\d+)\/[A-Za-z0-9-_]{64,}/, '/webhooks/$1/:token'); | ||
|
||
const statKey = `${method}:${route}`; | ||
let info = this.requestStats.get(statKey); | ||
if (!info) { | ||
info = { total: 0, succeeded: 0, errors: 0 }; | ||
this.requestStats.set(statKey, info); | ||
} | ||
|
||
info.total++; | ||
|
||
return super | ||
.request(method, url, auth, body, file, _route, short) | ||
.then((res: any) => { | ||
info.succeeded++; | ||
return res; | ||
}) | ||
.catch((err: any) => { | ||
info.errors++; | ||
throw err; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters