Getting all our nym-api tests to run via mixfetch
This commit is contained in:
@@ -1,21 +1,17 @@
|
||||
import { dir } from "console";
|
||||
import { readFileSync } from "fs";
|
||||
import { dirname } from "path";
|
||||
import { TLogLevelName } from "tslog";
|
||||
|
||||
import YAML from "yaml";
|
||||
import * as dotenv from 'dotenv';
|
||||
import path from "path";
|
||||
|
||||
class ConfigHandler {
|
||||
private static instance: ConfigHandler;
|
||||
|
||||
private validEnvironments = ["sandbox", "prod"];
|
||||
private baseWorkingDirectory: string;
|
||||
|
||||
public commonConfig: { request_headers: object };
|
||||
|
||||
private currentEnvironment: string;
|
||||
|
||||
public environment: string;
|
||||
|
||||
public environmentConfig: {
|
||||
log_level: TLogLevelName;
|
||||
time_zone: string;
|
||||
@@ -26,8 +22,21 @@ class ConfigHandler {
|
||||
};
|
||||
|
||||
private constructor() {
|
||||
this.baseWorkingDirectory = __dirname;
|
||||
const environment = process.env.TEST_ENV || "sandbox" || "prod";
|
||||
this.loadEnvironment(environment);
|
||||
}
|
||||
|
||||
private loadEnvironment(environment: string): void {
|
||||
this.loadEnvironmentVariables(environment);
|
||||
this.setCommonConfig();
|
||||
this.setEnvironmentConfig(process.env.TEST_ENV || "sandbox" || "prod");
|
||||
this.setEnvironmentConfig(environment);
|
||||
}
|
||||
|
||||
private loadEnvironmentVariables(environment: string): void {
|
||||
const envFileName = `${environment}.env`;
|
||||
const envFilePath = path.resolve(this.baseWorkingDirectory, `../${envFileName}`);
|
||||
dotenv.config({ path: envFilePath });
|
||||
}
|
||||
|
||||
public static getInstance(): ConfigHandler {
|
||||
@@ -39,10 +48,7 @@ class ConfigHandler {
|
||||
|
||||
private setCommonConfig(): void {
|
||||
try {
|
||||
const baseWorkingDirectory = __dirname;
|
||||
this.commonConfig = YAML.parse(
|
||||
readFileSync(baseWorkingDirectory + "/config.yaml", "utf8"),
|
||||
).common;
|
||||
this.commonConfig = this.readConfigFile().common;
|
||||
} catch (error) {
|
||||
throw Error(`Error reading common config: (${error})`);
|
||||
}
|
||||
@@ -51,21 +57,15 @@ class ConfigHandler {
|
||||
private setEnvironmentConfig(environment: string): void {
|
||||
this.ensureEnvironmentIsValid(environment);
|
||||
try {
|
||||
const baseWorkingDirectory = __dirname;
|
||||
this.environmentConfig = YAML.parse(
|
||||
readFileSync(baseWorkingDirectory + "/config.yaml", "utf8"),
|
||||
)[environment];
|
||||
this.environmentConfig = this.readConfigFile()[environment];
|
||||
} catch (error) {
|
||||
console.log("fadsfasdfasdfsdfsa")
|
||||
throw Error(`Error reading environment config: (${error})`);
|
||||
}
|
||||
}
|
||||
|
||||
public getEnvironmentConfig(environment: string): any {
|
||||
const baseWorkingDirectory = __dirname;
|
||||
return (
|
||||
this.environmentConfig ||
|
||||
YAML.parse(readFileSync(baseWorkingDirectory + "/config.yaml", "utf8"))[environment]
|
||||
private readConfigFile(): any {
|
||||
return YAML.parse(
|
||||
readFileSync(path.join(this.baseWorkingDirectory, "/config.yaml"), "utf8")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module.exports = {
|
||||
ConfigHandler: require('./config/configHandler.ts'),
|
||||
RestClient: require('./restClient/RestClient.ts')
|
||||
MixFetchClient: require('./restClient/MixFetchClient.ts')
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
HIDDEN_GATEWAY_OWNER=n1kymvkx6vsq7pvn6hfurkpg06h3j4gxj4em7tlg
|
||||
HIDDEN_GATEWAY_EXPLICIT_IP=213.219.38.119
|
||||
HIDDEN_GATEWAY_HOST=mainnet-gateway.nymte.ch
|
||||
HIDDEN_GATEWAY_SPHINX_KEY=CYcrjoJ8GT7Dp54zViUyyRUfegeRCyPifWQZHRgMZrfX
|
||||
HIDDEN_GATEWAY_IDENTITY_KEY=E3mvZTHQCdBvhfr178Swx9g4QG3kkRUun7YnToLMcMbM
|
||||
PREFFERED_NETWORK_REQUESTER="RwMHfHBCxSYprzPPSayzJeTGThAVJdohfJfMBhv7JnK.2XTA2PpFqxWNUHT1QdGvHjGSkciEcre1rbzdRnjmAvnZ@EmksoVk8Q7RZZH8atvZspDShD7Ekq6vDPnjK4LCQ7DUv"
|
||||
PREFERRED_GATEWAY=E3mvZTHQCdBvhfr178Swx9g4QG3kkRUun7YnToLMcMbM
|
||||
PREFERRED_VALIDATOR=https://validator.nymtech.net/api
|
||||
@@ -0,0 +1,59 @@
|
||||
import ConfigHandler from "../config/configHandler";
|
||||
import { createMixFetch } from "@nymproject/mix-fetch-node-commonjs";
|
||||
import * as dotenv from 'dotenv';
|
||||
import path from "path";
|
||||
|
||||
dotenv.config({ path: path.join(__dirname, '../.env') });;
|
||||
const config = ConfigHandler.getInstance();
|
||||
|
||||
export class MixFetchClient {
|
||||
public static authToken: string;
|
||||
private baseUrl: string;
|
||||
|
||||
constructor(baseUrl: string) {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
public async sendGet({
|
||||
route,
|
||||
}: any): Promise<any> {
|
||||
|
||||
const extra = {
|
||||
hiddenGateways: [
|
||||
{
|
||||
owner: process.env.HIDDEN_GATEWAY_OWNER,
|
||||
host: process.env.HIDDEN_GATEWAY_HOST,
|
||||
explicitIp: process.env.HIDDEN_GATEWAY_EXPLICIT_IP,
|
||||
identityKey: process.env.HIDDEN_GATEWAY_IDENTITY_KEY,
|
||||
sphinxKey: process.env.HIDDEN_GATEWAY_SPHINX_KEY,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const mixFetchOptions = {
|
||||
nymApiUrl: process.env.PREFERRED_VALIDATOR,
|
||||
preferredGateway: process.env.PREFERRED_GATEWAY,
|
||||
preferredNetworkRequester: process.env.PREFFERED_NETWORK_REQUESTER,
|
||||
mixFetchOverride: {
|
||||
requestTimeoutMs: 60_000,
|
||||
},
|
||||
forceTls: true,
|
||||
extra,
|
||||
};
|
||||
|
||||
const { mixFetch } = await createMixFetch(mixFetchOptions);
|
||||
|
||||
let args = {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
mode: "unsafe-ignore-cors"
|
||||
};
|
||||
|
||||
const response = await mixFetch(`${this.baseUrl}${route}`, args);
|
||||
const json = await response.json();
|
||||
return json;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
|
||||
import { Logger } from "tslog";
|
||||
import { stringify } from "yaml";
|
||||
import https from "https";
|
||||
import ConfigHandler from "../config/configHandler";
|
||||
const { createMixFetch } = require("@nymproject/mix-fetch-node-commonjs");
|
||||
|
||||
const config = ConfigHandler.getInstance();
|
||||
const log = new Logger({
|
||||
minLevel: config.environmentConfig.log_level,
|
||||
dateTimeTimezone:
|
||||
config.environmentConfig.time_zone ||
|
||||
Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
});
|
||||
|
||||
export class RestClient {
|
||||
public static authToken: string;
|
||||
private baseUrl: string;
|
||||
private mixFetch: any;
|
||||
|
||||
constructor(baseUrl: string) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.mixFetch = this.initialiseMixFetch
|
||||
}
|
||||
|
||||
|
||||
// MIXFETCH
|
||||
private initialiseMixFetch() {
|
||||
const extra = {
|
||||
hiddenGateways: [
|
||||
{
|
||||
owner: "n1ns3v70ul9gnl9l9fkyz8cyxfq75vjcmx8el0t3",
|
||||
host: "sandbox-gateway1.nymtech.net",
|
||||
explicitIp: "35.158.238.80",
|
||||
identityKey: "HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua",
|
||||
sphinxKey: "BoXeUD7ERGmzRauMjJD3itVNnQiH42ncUb6kcVLrb3dy",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const mixFetchOptions = {
|
||||
nymApiUrl: "https://sandbox-nym-api1.nymtech.net/api",
|
||||
preferredGateway: "HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua",
|
||||
preferredNetworkRequester:
|
||||
"AzGdJ4MU78Ex22NEWfeycbN7bt3PFZr1MtKstAdhfELG.GSxnKnvKPjjQm3FdtsgG5KyhP6adGbPHRmFWDH4XfUpP@HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua",
|
||||
mixFetchOverride: {
|
||||
requestTimeoutMs: 60_000,
|
||||
},
|
||||
forceTls: true,
|
||||
extra,
|
||||
};
|
||||
|
||||
const { mixFetch } = await createMixFetch(mixFetchOptions);
|
||||
return mixFetch;
|
||||
}
|
||||
|
||||
static async getToken(requestHeaders: object) {
|
||||
requestHeaders["Authorization"] = `asdf`;
|
||||
}
|
||||
|
||||
public async callEndpoint({
|
||||
route,
|
||||
method,
|
||||
authToken,
|
||||
headers,
|
||||
data,
|
||||
additionalConfigs,
|
||||
params,
|
||||
}: any): Promise<any> {
|
||||
let response;
|
||||
let responseLog = "Response: ";
|
||||
let requestHeaders = headers || {};
|
||||
|
||||
// if authToken is passed in, add it to the request headers
|
||||
if (authToken !== undefined) {
|
||||
requestHeaders = {
|
||||
...requestHeaders,
|
||||
...{
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
},
|
||||
};
|
||||
} else if (!requestHeaders.Authorization) {
|
||||
await RestClient.getToken(requestHeaders);
|
||||
}
|
||||
|
||||
log.debug(
|
||||
RestClient.prepareLogRecord({
|
||||
route,
|
||||
method,
|
||||
headers: requestHeaders,
|
||||
data,
|
||||
additionalConfigs,
|
||||
params,
|
||||
}),
|
||||
);
|
||||
|
||||
const mixRequestInit: MixRequestInit = {
|
||||
method,
|
||||
headers: requestHeaders,
|
||||
agent: new https.Agent({
|
||||
rejectUnauthorized: false,
|
||||
}),
|
||||
body: data,
|
||||
params,
|
||||
...additionalConfigs,
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await this.mixFetch(`${this.baseUrl}${route}`, mixRequestInit);
|
||||
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 the server! (${error.message})`;
|
||||
else
|
||||
responseLog = `<Error> Status = ${response.status} ${response.statusText}, ${error.message}`;
|
||||
}
|
||||
|
||||
log.debug(responseLog);
|
||||
return response;
|
||||
}
|
||||
|
||||
private static prepareLogRecord({
|
||||
route,
|
||||
method,
|
||||
headers,
|
||||
data,
|
||||
additionalConfigs,
|
||||
params,
|
||||
}: any): string {
|
||||
let logRecord = `Request: ${method} ${route}`;
|
||||
if (headers) logRecord = `${logRecord}\nHeaders: ${stringify(headers)}`;
|
||||
if (params) logRecord = `${logRecord}\nParams: ${stringify(params)}`;
|
||||
if (additionalConfigs)
|
||||
logRecord = `${logRecord}\nAdditional Configuration: ${stringify(
|
||||
additionalConfigs,
|
||||
)}`;
|
||||
if (data) {
|
||||
const jsonData = stringify(data);
|
||||
logRecord = `${logRecord}\nData: ${
|
||||
jsonData === undefined ? "Some data, not JSON!" : jsonData
|
||||
}`;
|
||||
}
|
||||
return logRecord;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
HIDDEN_GATEWAY_OWNER=n1ns3v70ul9gnl9l9fkyz8cyxfq75vjcmx8el0t3
|
||||
HIDDEN_GATEWAY_EXPLICIT_IP=35.158.238.80
|
||||
HIDDEN_GATEWAY_HOST=sandbox-gateway1.nymtech.net
|
||||
HIDDEN_GATEWAY_SPHINX_KEY=BoXeUD7ERGmzRauMjJD3itVNnQiH42ncUb6kcVLrb3dy
|
||||
HIDDEN_GATEWAY_IDENTITY_KEY=HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua
|
||||
PREFFERED_NETWORK_REQUESTER="AzGdJ4MU78Ex22NEWfeycbN7bt3PFZr1MtKstAdhfELG.GSxnKnvKPjjQm3FdtsgG5KyhP6adGbPHRmFWDH4XfUpP@HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua"
|
||||
PREFERRED_GATEWAY=HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua
|
||||
PREFERRED_VALIDATOR=https://sandbox-nym-api1.nymtech.net/api
|
||||
Reference in New Issue
Block a user