Create scaffolding of YAML config
This commit is contained in:
10
src/config/index.ts
Normal file
10
src/config/index.ts
Normal file
@@ -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);
|
||||
}
|
||||
29
src/config/types.ts
Normal file
29
src/config/types.ts
Normal file
@@ -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;
|
||||
};
|
||||
@@ -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"
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user