From 59e0829f8abd5147e752149d7ae7430b5ff8e941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Tue, 12 Nov 2024 19:01:19 +0100 Subject: [PATCH] Create scaffolding of YAML config --- src/config/index.ts | 10 ++++++++++ src/config/types.ts | 29 +++++++++++++++++++++++++++++ src/tauron/index.ts | 25 ++++++++++--------------- 3 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 src/config/index.ts create mode 100644 src/config/types.ts diff --git a/src/config/index.ts b/src/config/index.ts new file mode 100644 index 0000000..a1db7d9 --- /dev/null +++ b/src/config/index.ts @@ -0,0 +1,10 @@ +import fs from 'fs'; +import yaml from 'yaml'; +import { Config } from './types'; + +export * from './types'; + +export const parseConfig = (path: string): Config => { + const text = fs.readFileSync(path, 'utf8'); + return yaml.parse(text); +} \ No newline at end of file diff --git a/src/config/types.ts b/src/config/types.ts new file mode 100644 index 0000000..f7cbbe5 --- /dev/null +++ b/src/config/types.ts @@ -0,0 +1,29 @@ + +export type Config = { + /** + * Tauron service configuration + */ + tauron: TauronConfig; +}; + +export type TauronConfig = { + /** + * Path to file containing a username of Tauron account + */ + usernamePath: string; + + /** + * Path to file containing a password of Tauron account + */ + passwordPath: string; + + /** + * The measure point name - should be retrieved with browser dev-tools on website + */ + point: string; + + /** + * The path to the json file with cookie jar + */ + cookiesJarPath?: string; +}; \ No newline at end of file diff --git a/src/tauron/index.ts b/src/tauron/index.ts index e592a7a..8b6ec86 100644 --- a/src/tauron/index.ts +++ b/src/tauron/index.ts @@ -1,8 +1,10 @@ +import fs from 'fs'; import axios, { Axios, AxiosResponse } from "axios"; import { wrapper } from 'axios-cookiejar-support'; import { CookieJar } from 'tough-cookie'; import { FileCookieStore } from 'tough-cookie-file-store'; import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, ReadingDTO, ReadingRequestDTO } from "./types"; +import { TauronConfig } from '../config'; export * from './types'; @@ -14,26 +16,19 @@ const READING_API = '/odczyty/api'; /** * Utility class for Tauron API. - * @param username - file containing a username of Tauron account - * @param password - file containing a password of Tauron account - * @param point - the measure point name - should be retrieved with browser dev-tools on website - * @param cookiePath - the path to the json file with cookie jar + * @param config - configuration of service */ export const Tauron = class { #http: Axios; - #username: string; - #password: string; - #point: string; + #config: TauronConfig; - constructor(username: string, password: string, point: string, cookiePath = "./cookies.json") { + constructor(config: TauronConfig) { // TODO: It should be paths instead of raw credentials - this.#username = username; - this.#password = password; - this.#point = point; + this.#config = config; this.#http = wrapper(axios.create({ baseURL: BASE_URL, - jar: new CookieJar(new FileCookieStore(cookiePath)), + jar: new CookieJar(new FileCookieStore(config.cookiesJarPath || './cookies.json')), headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/jxl,image/webp,image/png,image/svg+xml,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.5', @@ -59,13 +54,13 @@ export const Tauron = class { await this.#http.get(LOGIN_API); await this.#http.postForm(LOGIN_API, { - username: this.#username, - password: this.#password, + username: fs.readFileSync(this.#config.usernamePath, 'utf8'), + password: fs.readFileSync(this.#config.passwordPath, 'utf8'), service: BASE_URL }); await this.#http.postForm('/ustaw_punkt', { - "site[client]": this.#point, + "site[client]": this.#config.point, "page": "energy" }); }