Skip to content

Commit 185797a

Browse files
committed
Added #164
Added NodeHttp
1 parent 709cb4b commit 185797a

File tree

6 files changed

+288
-0
lines changed

6 files changed

+288
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {expect} from 'chai';
18+
import {NodeHttp} from '../../src/infrastructure/NodeHttp';
19+
describe('NodeHttp', () => {
20+
let nodeHttp: NodeHttp;
21+
before((done) => {
22+
const path = require('path');
23+
require('fs').readFile(path.resolve(__dirname, '../conf/network.conf'), (err, data) => {
24+
if (err) {
25+
throw err;
26+
}
27+
const json = JSON.parse(data);
28+
nodeHttp = new NodeHttp(json.apiUrl);
29+
done();
30+
});
31+
});
32+
33+
describe('getNodeInfo', () => {
34+
it('should return node info', (done) => {
35+
nodeHttp.getNodeInfo()
36+
.subscribe((nodeInfo) => {
37+
expect(nodeInfo.friendlyName).not.to.be.undefined;
38+
expect(nodeInfo.host).not.to.be.undefined;
39+
expect(nodeInfo.networkIdentifier).not.to.be.undefined;
40+
expect(nodeInfo.port).not.to.be.undefined;
41+
expect(nodeInfo.publicKey).not.to.be.undefined;
42+
expect(nodeInfo.roles).not.to.be.undefined;
43+
expect(nodeInfo.version).not.to.be.undefined;
44+
done();
45+
});
46+
});
47+
});
48+
49+
describe('getNodeTime', () => {
50+
it('should return node time', (done) => {
51+
nodeHttp.getNodeTime()
52+
.subscribe((nodeTime) => {
53+
expect(nodeTime.receiveTimeStamp).not.to.be.undefined;
54+
expect(nodeTime.sendTimeStamp).not.to.be.undefined;
55+
done();
56+
});
57+
});
58+
});
59+
});

src/infrastructure/NodeHttp.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {from as observableFrom, Observable} from 'rxjs';
18+
import {map} from 'rxjs/operators';
19+
import { NodeInfo } from '../model/node/NodeInfo';
20+
import { NodeTime } from '../model/node/NodeTime';
21+
import { NodeInfoDTO, NodeRoutesApi, NodeTimeDTO } from './api';
22+
import {Http} from './Http';
23+
import {NodeRepository} from './NodeRepository';
24+
25+
/**
26+
* Node http repository.
27+
*
28+
* @since 1.0
29+
*/
30+
export class NodeHttp extends Http implements NodeRepository {
31+
/**
32+
* @internal
33+
* Nem2 Library account routes api
34+
*/
35+
private nodeRoutesApi: NodeRoutesApi;
36+
37+
/**
38+
* Constructor
39+
* @param url
40+
*/
41+
constructor(url: string) {
42+
super();
43+
this.nodeRoutesApi = new NodeRoutesApi(url);
44+
45+
}
46+
47+
/**
48+
* Supplies additional information about the application running on a node.
49+
* @summary Get the node information
50+
*/
51+
public getNodeInfo(): Observable<NodeInfo> {
52+
return observableFrom(this.nodeRoutesApi.getNodeInfo()).pipe(map((nodeInfoDTO: NodeInfoDTO) => {
53+
return new NodeInfo(
54+
nodeInfoDTO.publicKey,
55+
nodeInfoDTO.port,
56+
nodeInfoDTO.networkIdentifier,
57+
nodeInfoDTO.version,
58+
nodeInfoDTO.roles as number,
59+
nodeInfoDTO.host,
60+
nodeInfoDTO.friendlyName,
61+
);
62+
}));
63+
}
64+
65+
/**
66+
* Gets the node time at the moment the reply was sent and received.
67+
* @summary Get the node time
68+
*/
69+
public getNodeTime(): Observable<NodeTime> {
70+
return observableFrom(this.nodeRoutesApi.getNodeTime()).pipe(map((nodeTimeDTO: NodeTimeDTO) => {
71+
return new NodeTime(nodeTimeDTO.communicationTimestamps.sendTimestamp, nodeTimeDTO.communicationTimestamps.receiveTimestamp);
72+
}));
73+
}
74+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {Observable} from 'rxjs';
18+
import { NodeInfo } from '../model/node/NodeInfo';
19+
import { NodeTime } from '../model/node/NodeTime';
20+
21+
/**
22+
* Node interface repository.
23+
*
24+
* @since 1.0
25+
*/
26+
export interface NodeRepository {
27+
28+
/**
29+
* Supplies additional information about the application running on a node.
30+
* @summary Get the node information
31+
*/
32+
getNodeInfo(): Observable<NodeInfo>;
33+
34+
/**
35+
* Gets the node time at the moment the reply was sent and received.
36+
* @summary Get the node time
37+
*/
38+
getNodeTime(): Observable<NodeTime>;
39+
}

src/model/node/NodeInfo.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { NetworkType } from '../blockchain/NetworkType';
17+
import { RoleType } from './RoleType';
18+
/**
19+
* The node info structure describes basic information of a node.
20+
*/
21+
export class NodeInfo {
22+
23+
/**
24+
* @param publicKey
25+
* @param port
26+
* @param networkIdentifier
27+
* @param version
28+
* @param roles
29+
* @param host
30+
* @param friendlyName
31+
*/
32+
constructor(/**
33+
* The public key used to identify the node.
34+
*/
35+
public readonly publicKey: string,
36+
/**
37+
* The port used for the communication.
38+
*/
39+
public readonly port: number,
40+
/**
41+
* The network identifier.
42+
*/
43+
public readonly networkIdentifier: NetworkType,
44+
/**
45+
* The version of the application.
46+
*/
47+
public readonly version: number,
48+
/**
49+
* The roles of the application.
50+
*/
51+
public readonly roles: RoleType,
52+
/**
53+
* The IP address of the endpoint.
54+
*/
55+
public readonly host: string,
56+
/**
57+
* The name of the node.
58+
*/
59+
public readonly friendlyName: string ) {}
60+
}

src/model/node/NodeTime.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { RoleType } from './RoleType';
17+
import { NetworkType } from '../blockchain/NetworkType';
18+
import { UInt64 } from '../UInt64';
19+
/**
20+
* The node info structure describes basic information of a node.
21+
*/
22+
export class NodeTime {
23+
24+
/**
25+
* @param sendTimeStamp
26+
* @param receiveTimeStamp
27+
*/
28+
constructor(/**
29+
* The request send timestamp
30+
*/
31+
public readonly sendTimeStamp?: number[],
32+
/**
33+
* The request received timestamp
34+
*/
35+
public readonly receiveTimeStamp?: number[] ) {}
36+
}

src/model/node/RoleType.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export enum RoleType {
18+
PeerNode = 1,
19+
ApiNode = 2,
20+
}

0 commit comments

Comments
 (0)