Skip to content

Commit

Permalink
feat: openapi client integrated for statistics-service (#1777)
Browse files Browse the repository at this point in the history
* feat: openapi client integrated for statistics-service

* wsUrl is added to NodeModel

Co-authored-by: Baha <[email protected]>
  • Loading branch information
yilmazbahadir and Baha authored Nov 17, 2021
1 parent c778282 commit e1103f8
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 31 deletions.
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,
}),
);
}
}

0 comments on commit e1103f8

Please sign in to comment.