Clean up code and add logging framework for backend
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-06-02 10:33:59 +02:00
parent bb828386b9
commit aa1c3d1464
6 changed files with 262 additions and 119 deletions

View File

@@ -3,6 +3,7 @@ import { TunakillUser } from "../models/TunakillUser";
import {HttpException, HttpStatus, Injectable} from "@nestjs/common";
import {TunakillLogin} from "../models/TunakillLogin";
import fetch from 'node-fetch';
import logger from "../logger/Logger";
@Injectable()
export class SinusBotService {
@@ -25,23 +26,27 @@ export class SinusBotService {
public async fetchStats(): Promise<TableEntry[]> {
if (this.bearer == null) {
console.log(`Hey! I'm trying to get my Bearer token!`);
logger.info(`Hey! I'm trying to get my Bearer token!`);
await this.login()
.then(res => this.logResponse(res))
.then(token => this.bearer = token)
.catch(error => {
logger.error(`Oh oh! Something went wrong while fetching Bearer token.`, error);
throw this.createHttpException(HttpStatus.UNAUTHORIZED, error.message);
});
console.log(`Seems like I have my Bearer token! It's called: ${this.bearer}`);
logger.debug(
`Seems like I have my Bearer token!
Looks like it's ${this.bearer == null ? 'undefined' : 'not undefined'}`
);
}
console.log(`I try to fetch user data now!`)
logger.info(`I try to fetch user data now! The URL is called ${this.tunaKillURL}`);
return await fetch(this.tunaKillURL, this.requestConfig(null, this.bearer))
.then(res => this.checkStatus(res))
.then(res => res.json())
.then(data => this.consumeTunakillResponse(data))
.catch(error => {
throw this.createHttpException(HttpStatus.UNAUTHORIZED, error.message);
logger.error(`I couldn't fetch user data.`, error);
throw this.createHttpException(error.statusCode, error.message);
});
}
@@ -50,16 +55,17 @@ export class SinusBotService {
*/
private async login(): Promise<string> {
if (this.botInfo.id == null) {
console.log(`I have to fetch a bot ID before I can continue!`);
logger.debug(`I have to fetch a bot ID before I can continue!`);
await this.fetchDefaultBotId()
.then(defaultBotId => this.botInfo.id = defaultBotId)
.catch(() => {
.catch(error => {
logger.warn(`I couldn't retrieve SinusBot bot information. Login is likely to fail!`, error);
throw this.createHttpException(
HttpStatus.NOT_FOUND,
`Could not fetch enough bot information for further requests.`
);
});
console.log(`Looks like everything went fine. The bot ID now is ${this.botInfo.id}`);
logger.info(`The bot ID now is ${this.botInfo.id}`);
}
const body: TunakillLogin = {
@@ -68,7 +74,7 @@ export class SinusBotService {
botId: this.botInfo.id
}
console.log(`> I try to login now! My credentials are: ${JSON.stringify(body)}`)
logger.info(`Logging in for Bearer token!`)
return await fetch(this.loginURL, this.requestConfig(JSON.stringify(body)))
.then(res => this.checkStatus(res))
.then(res => res.json())
@@ -154,8 +160,8 @@ export class SinusBotService {
return `${d}d ${h}h ${m}m ${s}s`;
}
private logResponse(res: any) {
console.log(res);
private static logResponse(res: any) {
logger.debug(res);
return res;
}
}