Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: openapi client integrated for statistics-service #1777

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"rss-parser": "^3.7.3",
"rxjs": "^6.6.7",
"symbol-sdk": "^1.0.2-alpha-202108061451",
"symbol-statistics-service-typescript-fetch-client": "1.1.2-alpha-202111170848",
"trezor-connect": "^7.0.5",
"utf8": "^3.0.0",
"vee-validate": "^3.2.3",
Expand Down
1 change: 1 addition & 0 deletions src/core/database/entities/NodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export class NodeModel {
public readonly networkType: NetworkType,
public readonly publicKey?: string,
public nodePublicKey?: string,
public readonly wsUrl?: string,
) {}
}
121 changes: 90 additions & 31 deletions src/services/NodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,8 @@ import { networkConfig } from '@/config';
import { NodeModelStorage } from '@/core/database/storage/NodeModelStorage';
import { ProfileModel } from '@/core/database/entities/ProfileModel';
import fetch from 'node-fetch';
import { Configuration, NodeApi, NodeListFilter, NodeInfo as NodeApiNodeInfo } from 'symbol-statistics-service-typescript-fetch-client';

const requestNodes = async (networkType, searchCriteria?: { nodeFilter: string; limit: number }): Promise<[]> => {
const url =
networkConfig[networkType].statisticServiceUrl + 'nodes?filter=' + searchCriteria.nodeFilter + '&limit=' + searchCriteria.limit;
return new Promise((resolve) => {
fetch(url)
.then((response) => response.json())
.then((responseData) => {
resolve(responseData);
})
.catch((e) => {
console.log(e);
resolve(undefined);
});
});
};
/**
* The service in charge of loading and caching anything related to Node and Peers from Rest.
* The cache is done by storing the payloads in SimpleObjectStorage.
Expand Down Expand Up @@ -86,26 +72,89 @@ export class NodeService {

public async getNodesFromStatisticService(networkType: NetworkType, isOffline?: boolean): Promise<NodeModel[]> {
const nodeSearchCriteria = {
nodeFilter: 'suggested',
nodeFilter: NodeListFilter.Suggested,
limit: 30,
ssl: true,
};
if (!isOffline && navigator.onLine) {
const data = await requestNodes(networkType, nodeSearchCriteria);
if (!data) {
try {
const nodeInfos = await this.createStatisticServiceRestClient(networkConfig[networkType].statisticServiceUrl).getNodes(
nodeSearchCriteria.nodeFilter,
nodeSearchCriteria.limit,
nodeSearchCriteria.ssl,
);
if (!nodeInfos) {
return undefined;
}
return nodeInfos.map((n) =>
this.createNodeModel(
n.apiStatus?.restGatewayUrl,
networkType,
n.friendlyName,
true,
n.publicKey,
n.apiStatus?.nodePublicKey,
n.apiStatus.webSocket.url,
),
);
} catch (error) {
// proceed to return
}
}
return undefined;
}

private async getNodeModelByMethod(
networkType: NetworkType,
nodeGetter: (param: string) => Promise<NodeApiNodeInfo>,
paramValue: string,
): Promise<NodeModel> {
try {
const nodeInfo = await nodeGetter(paramValue);
if (!nodeInfo) {
return undefined;
}
return data.map((n) => {
//@ts-ignore
const url = n.apiStatus?.restGatewayUrl;
// @ts-ignore
const friendlyName = n.friendlyName;
// @ts-ignore
const publicKey = n.publicKey;
// @ts-ignore
const nodePublicKey = n.apiStatus?.nodePublicKey;
// @ts-ignore
return this.createNodeModel(url, networkType, friendlyName, true, publicKey, nodePublicKey);
});
return this.createNodeModel(
nodeInfo.apiStatus?.restGatewayUrl,
networkType,
nodeInfo.friendlyName,
true,
nodeInfo.publicKey,
nodeInfo.apiStatus?.nodePublicKey,
nodeInfo.apiStatus?.webSocket?.url,
);
} catch (error) {
// proceed to return
}
return undefined;
}

public async getNodeFromStatisticServiceByPublicKey(
networkType: NetworkType,
publicKey: string,
isOffline?: boolean,
): Promise<NodeModel> {
if (!isOffline && navigator.onLine) {
return this.getNodeModelByMethod(
networkType,
this.createStatisticServiceRestClient(networkConfig[networkType].statisticServiceUrl).getNode,
publicKey,
);
}
return undefined;
}

public async getNodeFromStatisticServiceByNodePublicKey(
networkType: NetworkType,
nodePublicKey: string,
isOffline?: boolean,
): Promise<NodeModel> {
if (!isOffline && navigator.onLine) {
return this.getNodeModelByMethod(
networkType,
this.createStatisticServiceRestClient(networkConfig[networkType].statisticServiceUrl).getNodeByNodePublicKey,
nodePublicKey,
);
}
return undefined;
}
Expand All @@ -117,8 +166,9 @@ export class NodeService {
isDefault: boolean | undefined = undefined,
publicKey?: string,
nodePublicKey?: string,
wsUrl?: string,
): NodeModel {
return new NodeModel(url, friendlyName, isDefault, networkType, publicKey, nodePublicKey);
return new NodeModel(url, friendlyName, isDefault, networkType, publicKey, nodePublicKey, wsUrl);
}

public loadNodes(profile: ProfileModel): NodeModel[] {
Expand All @@ -135,4 +185,13 @@ export class NodeService {
public reset() {
this.storage.remove();
}

public createStatisticServiceRestClient(statisticsServiceUrl: string): NodeApi {
return new NodeApi(
new Configuration({
fetchApi: fetch as any,
basePath: statisticsServiceUrl,
}),
);
}
}