refactoring the utils

This commit is contained in:
benedettadavico
2023-10-24 11:16:06 +02:00
parent 604098844a
commit 0486cd2e63
11 changed files with 51 additions and 366 deletions
+6
View File
@@ -4,9 +4,15 @@ common:
Content-Type: application/json
sandbox:
api_base_url: https://sandbox-nym-api1.nymtech.net/api/v1
mix_id: 7
identity_key: 2wEzyW6Pihk19xbCK9kB81vYuDFUvTzTAr77arvxdfSg
gateway_identity: HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua
log_level: error
time_zone: utc
prod:
api_base_url: https://validator.nymtech.net/api/v1
mix_id: 730
mixnode_identity: 3pMCJswCyA19MGYWGDWT5fBk2M8ybSZGXttyAoNY5gBB
gateway_identity: 2BuMSfMW3zpeAjKXyKLhmY4QW1DXurrtSPEJ6CjX3SEh
log_level: error
time_zone: utc
@@ -40,9 +40,8 @@ class ConfigHandler {
private setCommonConfig(): void {
try {
const baseWorkingDirectory = __dirname;
console.log(`Base Working Directory: ${baseWorkingDirectory}`);
this.commonConfig = YAML.parse(
readFileSync("/Users/benedetta/nym/workspace/nym/common/api-test-utils/config/config.yaml", "utf8"),
readFileSync(baseWorkingDirectory + "/config.yaml", "utf8"),
).common;
} catch (error) {
throw Error(`Error reading common config: (${error})`);
@@ -52,8 +51,9 @@ class ConfigHandler {
private setEnvironmentConfig(environment: string): void {
this.ensureEnvironmentIsValid(environment);
try {
const baseWorkingDirectory = __dirname;
this.environmentConfig = YAML.parse(
readFileSync("/Users/benedetta/nym/workspace/nym/common/api-test-utils/config/config.yaml", "utf8"),
readFileSync(baseWorkingDirectory + "/config.yaml", "utf8"),
)[environment];
} catch (error) {
console.log("fadsfasdfasdfsdfsa")
@@ -62,9 +62,10 @@ class ConfigHandler {
}
public getEnvironmentConfig(environment: string): any {
const baseWorkingDirectory = __dirname;
return (
this.environmentConfig ||
YAML.parse(readFileSync("api-test-utils/config/config.yaml", "utf8"))[environment]
YAML.parse(readFileSync(baseWorkingDirectory + "/config.yaml", "utf8"))[environment]
);
}
+17 -14
View File
@@ -1,26 +1,29 @@
CONFIGURED=true
NETWORK_NAME=qa
RUST_LOG=info
RUST_BACKTRACE=1
NETWORK_NAME=qa
BECH32_PREFIX=n
MIX_DENOM=unym
MIX_DENOM_DISPLAY=nym
STAKE_DENOM=unyx
STAKE_DENOM_DISPLAY=nyx
DENOMS_EXPONENT=6
MIXNET_CONTRACT_ADDRESS=n1rjzps6qrmdqmf0xz4cn4x4rcmqeqzq6hnzqg4wcvd0r2lyasdq5sepn5s8
VESTING_CONTRACT_ADDRESS=n1xr3rq8yvd7qplsw5yx90ftsr2zdhg4e9z60h5duusgxpv72hud3sjkxkav
COCONUT_BANDWIDTH_CONTRACT_ADDRESS=n1ghd753shjuwexxywmgs4xz7x2q732vcn7ty4yw
GROUP_CONTRACT_ADDRESS=n1rw8fw2mpcpzzq3jpa4e52ufawnmj5a4u68p35umvgskewuw0nlzsaa5w4m
MULTISIG_CONTRACT_ADDRESS=n17p9rzwnnfxcjp32un9ug7yhhzgtkhvl988qccs
COCONUT_DKG_CONTRACT_ADDRESS=n17p9rzwnnfxcjp32un9ug7yhhzgtkhvl988qccs
EPHEMERA_CONTRACT_ADDRESS=n1fc7nakzuyfn2qzkclafcsc54asamnclg064962lwne40w2lq558qftzjza
REWARDING_VALIDATOR_ADDRESS=n1tfzd4qz3a45u8p4mr5zmzv66457uwjgcl05jdq
STATISTICS_SERVICE_DOMAIN_ADDRESS="http://0.0.0.0"
NYXD="https://qa-validator.nymtech.net"
NYM_API="https://qa-validator-api.nymtech.net/api/"
MIXNET_CONTRACT_ADDRESS=n14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sjyvg3g
VESTING_CONTRACT_ADDRESS=n1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrq73f2nw
BANDWIDTH_CLAIM_CONTRACT_ADDRESS=n19lc9u84cz0yz3fww5283nucc9yvr8gsjmgeul0
COCONUT_BANDWIDTH_CONTRACT_ADDRESS=n1w798gp0zqv3s9hjl3jlnwxtwhykga6rn93p46q2crsdqhaj3y4gs68f74j
GROUP_CONTRACT_ADDRESS=n1sthrn5ep8ls5vzz8f9gp89khhmedahhdqd244dh9uqzk3hx2pzrsvf7zgk
MULTISIG_CONTRACT_ADDRESS=n1sr06m8yqg0wzqqyqvzvp5t07dj4nevx9u8qc7j4qa72qu8e3ct8qledthy
COCONUT_DKG_CONTRACT_ADDRESS=n1udfs22xpxle475m2nz7u47jfa3vngncdegmczwwdx00cmetypa3s7uyuqn
REWARDING_VALIDATOR_ADDRESS=n1rfvpsynktze6wvn6ldskj8xgwfzzk5v6pnff39
NAME_SERVICE_CONTRACT_ADDRESS=n1qum2tr7hh4y7ruzew68c64myjec0dq2s2njf6waja5t0w879lutqadamme
SERVICE_PROVIDER_DIRECTORY_CONTRACT_ADDRESS=n13ehuhysn5mqjeaheeuew2gjs785f6k7jm8vfsqg3jhtpkwppcmzq6m2hmz
STATISTICS_SERVICE_DOMAIN_ADDRESS="https://mainnet-stats.nymte.ch:8090"
EXPLORER_API=https://qa-network-explorer.qa.nymte.ch/api
NYXD="https://qa-validator.qa.nymte.ch/"
NYM_API="https://qa-nym-api.qa.nymte.ch/api"
DKG_TIME_CONFIGURATION="600,300,300,60,60,1209600"
@@ -1,5 +1,5 @@
import ContractCache from "../../../src/endpoints/ContractCache";
import ConfigHandler from "../../../src/config/configHandler";
import ConfigHandler from "../../../../../common/api-test-utils/config/configHandler"
let contract: ContractCache;
let config: ConfigHandler;
@@ -1,5 +1,5 @@
import Status from "../../src/endpoints/Status";
import ConfigHandler from "../../src/config/configHandler";
import ConfigHandler from "../../../../common/api-test-utils/config/configHandler"
let status: Status;
let config: ConfigHandler;
@@ -29,7 +29,7 @@ describe("Get gateway data", (): void => {
});
it("Get a gateway history", async (): Promise<void> => {
const identity_key = config.environmnetConfig.gateway_identity;
const identity_key = config.environmentConfig.gateway_identity;
const response = await status.getGatewayHistory(identity_key);
if ("identity" in response) {
@@ -47,14 +47,14 @@ describe("Get gateway data", (): void => {
});
it("Get gateway core status count", async (): Promise<void> => {
const identity_key = config.environmnetConfig.gateway_identity;
const identity_key = config.environmentConfig.gateway_identity;
const response = await status.getGatewayCoreCount(identity_key);
expect(identity_key).toStrictEqual(response.identity);
expect(typeof response.count).toBe("number");
});
it("Get gateway average uptime", async (): Promise<void> => {
const identity_key = config.environmnetConfig.gateway_identity;
const identity_key = config.environmentConfig.gateway_identity;
const response = await status.getGatewayAverageUptime(identity_key);
if ("mix_id" in response) {
expect(identity_key).toStrictEqual(response.identity);
@@ -67,7 +67,7 @@ describe("Get gateway data", (): void => {
});
it("Get a gateway status report", async (): Promise<void> => {
const identity_key = config.environmnetConfig.gateway_identity;
const identity_key = config.environmentConfig.gateway_identity;
const response = await status.getGatewayStatusReport(identity_key);
if ("mix_id" in response) {
expect(identity_key).toStrictEqual(response.identity);
@@ -1,5 +1,5 @@
import Status from "../../src/endpoints/Status";
import ConfigHandler from "../../src/config/configHandler";
import ConfigHandler from "../../../../common/api-test-utils/config/configHandler"
let status: Status;
let config: ConfigHandler;
@@ -11,7 +11,7 @@ describe("Get mixnode data", (): void => {
});
it("Get a mixnode report", async (): Promise<void> => {
const identity_key = config.environmnetConfig.mix_id;
const identity_key = config.environmentConfig.mix_id;
const response = await status.getMixnodeStatusReport(identity_key);
if ("mix_id" in response) {
expect(typeof response.last_day).toBe("number");
@@ -22,7 +22,7 @@ describe("Get mixnode data", (): void => {
});
it("Get a mixnode stake saturation", async (): Promise<void> => {
const identity_key = config.environmnetConfig.mix_id;
const identity_key = config.environmentConfig.mix_id;
const response = await status.getMixnodeStakeSaturation(identity_key);
if ("saturation" in response) {
expect(typeof response.as_at).toBe("number");
@@ -34,7 +34,7 @@ describe("Get mixnode data", (): void => {
});
it("Get a mixnode average uptime", async (): Promise<void> => {
const identity_key = config.environmnetConfig.mix_id;
const identity_key = config.environmentConfig.mix_id;
const response = await status.getMixnodeAverageUptime(identity_key);
if ("mix_id" in response) {
expect(identity_key).toStrictEqual(response.mix_id);
@@ -45,7 +45,7 @@ describe("Get mixnode data", (): void => {
});
it("Get a mixnode history", async (): Promise<void> => {
const identity_key = config.environmnetConfig.mix_id;
const identity_key = config.environmentConfig.mix_id;
const response = await status.getMixnodeHistory(identity_key);
if ("mix_id" in response) {
response.history.forEach((x) => {
@@ -62,14 +62,14 @@ describe("Get mixnode data", (): void => {
});
it("Get a mixnode core count", async (): Promise<void> => {
const identity_key = config.environmnetConfig.mix_id;
const identity_key = config.environmentConfig.mix_id;
const response = await status.getMixnodeCoreCount(identity_key);
expect(identity_key).toStrictEqual(response.mix_id);
expect(typeof response.count).toBe("number");
});
it("Get a mixnode reward estimation", async (): Promise<void> => {
const identity_key = config.environmnetConfig.mix_id;
const identity_key = config.environmentConfig.mix_id;
const response = await status.getMixnodeRewardComputation(identity_key);
if ("estimation" in response) {
expect(response.reward_params.interval.sybil_resistance).toStrictEqual(
@@ -83,7 +83,7 @@ describe("Get mixnode data", (): void => {
});
it("Get a mixnode inclusion probability", async (): Promise<void> => {
const identity_key = config.environmnetConfig.mix_id;
const identity_key = config.environmentConfig.mix_id;
const response = await status.getMixnodeInclusionProbability(identity_key);
if ("mix_id" in response) {
expect(typeof response.in_active).toBe("string");
@@ -119,7 +119,7 @@ describe("Get mixnode data", (): void => {
});
it("Get a mixnode status", async (): Promise<void> => {
const identity_key = config.environmnetConfig.mix_id;
const identity_key = config.environmentConfig.mix_id;
const response = await status.getMixnodeStatus(identity_key);
const unfiltered_mixnodes_response = await status.getUnfilteredMixnodes();
const mixnode = unfiltered_mixnodes_response.find(
@@ -155,7 +155,7 @@ describe("Get mixnode data", (): void => {
});
it("with correct data", async (): Promise<void> => {
const mix_id = config.environmnetConfig.mix_id;
const mix_id = config.environmentConfig.mix_id;
const response = await status.sendMixnodeRewardEstimatedComputation(
mix_id
);
-18
View File
@@ -1,18 +0,0 @@
common:
request_headers:
Accept: application/json
Content-Type: application/json
sandbox:
api_base_url: https://sandbox-nym-api1.nymtech.net/api/v1
mix_id: 7
identity_key: 2wEzyW6Pihk19xbCK9kB81vYuDFUvTzTAr77arvxdfSg
gateway_identity: HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua
log_level: error
time_zone: utc
prod:
api_base_url: https://validator.nymtech.net/api/v1
mix_id: 730
mixnode_identity: 3pMCJswCyA19MGYWGDWT5fBk2M8ybSZGXttyAoNY5gBB
gateway_identity: 2BuMSfMW3zpeAjKXyKLhmY4QW1DXurrtSPEJ6CjX3SEh
log_level: error
time_zone: utc
-64
View File
@@ -1,64 +0,0 @@
import { readFileSync } from "fs";
import { TLogLevelName } from "tslog";
import YAML from "yaml";
class ConfigHandler {
private static instance: ConfigHandler;
private validEnvironments = ["sandbox", "prod"];
public commonConfig: { request_headers: object };
public environment: string;
public environmnetConfig: {
log_level: TLogLevelName;
time_zone: string;
api_base_url: string;
mix_id: number;
identity_key: string;
gateway_identity: string;
};
private constructor() {
this.setCommonConfig();
this.setEnvironmentConfig(process.env.TEST_ENV || "sandbox" || "prod");
}
public static getInstance(): ConfigHandler {
if (!ConfigHandler.instance) {
ConfigHandler.instance = new ConfigHandler();
}
return ConfigHandler.instance;
}
private setCommonConfig(): void {
try {
this.commonConfig = YAML.parse(
readFileSync("src/config/config.yaml", "utf8")
).common;
} catch (error) {
throw Error(`Error reading common config: (${error})`);
}
}
private setEnvironmentConfig(environment: string): void {
this.ensureEnvironmentIsValid(environment);
try {
this.environmnetConfig = YAML.parse(
readFileSync("src/config/config.yaml", "utf8")
)[environment];
} catch (error) {
throw Error(`Error reading environment config: (${error})`);
}
}
private ensureEnvironmentIsValid(environment: string): void {
if (this.validEnvironments.indexOf(environment) === -1) {
throw Error(`Config environment is not valid: "${environment}"`);
}
}
}
export default ConfigHandler;
@@ -1,10 +1,10 @@
import { Logger } from "tslog";
import ConfigHandler from "../../config/configHandler";
import { RestClient } from "../../restClient/RestClient";
import ConfigHandler from "../../../../../common/api-test-utils/config/configHandler";
import { RestClient } from "../../../../../common/api-test-utils/restClient/RestClient";
export abstract class APIClient {
protected constructor(serviceUrl: string) {
const baseUrl: string = this.config.environmnetConfig.api_base_url;
const baseUrl: string = this.config.environmentConfig.api_base_url;
this.url = baseUrl + serviceUrl;
this.restClient = new RestClient(this.url);
this.serviceName = this.constructor.toString().match(/\w+/g)[1];
@@ -16,9 +16,9 @@ export abstract class APIClient {
protected config = ConfigHandler.getInstance();
protected log: Logger = new Logger({
minLevel: this.config.environmnetConfig.log_level,
minLevel: this.config.environmentConfig.log_level,
dateTimeTimezone:
this.config.environmnetConfig.time_zone ||
this.config.environmentConfig.time_zone ||
Intl.DateTimeFormat().resolvedOptions().timeZone,
});
-243
View File
@@ -1,243 +0,0 @@
import axios, {
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
Method,
} from "axios";
import { Logger } from "tslog";
import { stringify } from "yaml";
import https from "https";
import ConfigHandler from "../config/configHandler";
const config = ConfigHandler.getInstance();
const log = new Logger({
minLevel: config.environmnetConfig.log_level,
dateTimeTimezone:
config.environmnetConfig.time_zone ||
Intl.DateTimeFormat().resolvedOptions().timeZone,
});
function isSet(property): boolean {
return property !== undefined && property !== null;
}
export class RestClient {
private static authToken: string;
private axiosInstance: AxiosInstance;
constructor(baseUrl: string) {
this.axiosInstance = axios.create({ baseURL: baseUrl });
}
private httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
// Not returning an actual auth token for this example project.
// Just showing how it can be done!
static async getToken(requestHeaders: object) {
requestHeaders["Authorization"] = `asdf`;
}
public async callEndpoint({
route,
method,
authToken,
headers,
data,
additionalConfigs,
params,
}: IAxiosCallEndpointArgs): Promise<AxiosResponse> {
let response;
let responseLog = "Response: ";
let requestHeaders = headers;
// if headers are not passed in, use the default headers
if (requestHeaders == undefined) {
requestHeaders = { ...config.commonConfig.request_headers };
}
// if authToken is passed in, add it to the request headers
if (authToken !== undefined) {
requestHeaders = {
...requestHeaders,
...{
Authorization: `Bearer ${authToken}`,
},
};
}
// if we have not set the auth headers yet, set them
else if (!requestHeaders.Authorization) {
await RestClient.getToken(requestHeaders);
}
log.debug(
RestClient.prepareLogRecord({
route,
method,
headers: requestHeaders,
data,
additionalConfigs,
params,
})
);
await this.axiosInstance
.request({
url: route,
method,
data,
headers: requestHeaders,
httpsAgent: this.httpsAgent,
params,
...additionalConfigs,
})
.then((res) => {
response = res;
responseLog = `<Success> Status = ${res.status} ${res.statusText}`;
})
.catch((error) => {
response = error.response;
if (response === undefined)
responseLog = `<Error> Something wrong happened, did not get proper error from server! (${error.message})`;
else
responseLog = `<Error> Status = ${response.status} ${response.statusText}, ${error.message}`;
});
log.debug(responseLog);
return response;
}
public async sendPost({
route,
authToken,
data,
params,
headers,
additionalConfigs,
}: IAxiosHttpRequestArgs): Promise<any> {
return this.callEndpoint({
route,
method: "POST",
authToken,
data,
params,
headers,
additionalConfigs,
});
}
public async sendGet({
route,
authToken,
params,
headers,
additionalConfigs,
}: IAxiosHttpRequestArgs): Promise<any> {
return this.callEndpoint({
route,
method: "GET",
authToken,
params,
headers,
additionalConfigs,
});
}
public async sendDelete({
route,
authToken,
params,
headers,
additionalConfigs,
}: IAxiosHttpRequestArgs): Promise<any> {
return this.callEndpoint({
route,
method: "DELETE",
authToken,
params,
headers,
additionalConfigs,
});
}
public async sendPatch({
route,
authToken,
data,
headers,
additionalConfigs,
}: IAxiosHttpRequestArgs): Promise<any> {
return this.callEndpoint({
route,
method: "PATCH",
authToken,
data,
headers,
additionalConfigs,
});
}
public async sendPut({
route,
authToken,
data,
headers,
additionalConfigs,
}: IAxiosHttpRequestArgs): Promise<any> {
return this.callEndpoint({
route,
method: "PUT",
authToken,
data,
headers,
additionalConfigs,
});
}
private static prepareLogRecord({
route,
method,
headers,
data,
additionalConfigs,
params,
}: IAxiosCallEndpointArgs): string {
let logRecord = `Request: ${method} ${route}`;
if (isSet(headers))
logRecord = `${logRecord}\nHeaders: ${stringify(headers)}`;
if (isSet(params)) logRecord = `${logRecord}\nParams: ${stringify(params)}`;
if (isSet(additionalConfigs)) {
logRecord = `${logRecord}\nAdditional Configuration: ${stringify(
additionalConfigs
)}`;
}
if (isSet(data)) {
const jsonData = stringify(data);
// We don't want to log anything that isn't json data
logRecord = `${logRecord}\nData: ${
jsonData === undefined ? "Some data, not JSON!" : jsonData
}`;
}
return logRecord;
}
}
export interface IAxiosHttpRequestArgs {
route: string;
authToken?: string;
data?: object;
params?: object;
headers?: any;
additionalConfigs?: AxiosRequestConfig;
}
export interface IAxiosCallEndpointArgs extends IAxiosHttpRequestArgs {
method: Method;
}
@@ -7,7 +7,7 @@ describe("Get gateway related info", (): void => {
beforeAll(async (): Promise<void> => {
try {
gatewayHosts = await getGatewayIPAddresses();
// console.log(gatewayHosts);
console.log(gatewayHosts);
} catch (error) {
throw new Error(`Error fetching gateway IP addresses: ${error.message}`);
}