Skip to content

Commit db77423

Browse files
committed
refactored http codes
1 parent ee30e0b commit db77423

14 files changed

+242
-416
lines changed

src/infrastructure/AccountHttp.ts

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ClientResponse } from 'http';
1817
import {from as observableFrom, Observable, throwError} from 'rxjs';
1918
import {catchError, map} from 'rxjs/operators';
2019
import {AccountInfo} from '../model/account/AccountInfo';
@@ -27,8 +26,7 @@ import {Transaction} from '../model/transaction/Transaction';
2726
import { UInt64 } from '../model/UInt64';
2827
import {AccountRepository} from './AccountRepository';
2928
import { AccountInfoDTO,
30-
AccountRoutesApi,
31-
TransactionInfoDTO } from './api';
29+
AccountRoutesApi } from './api';
3230
import {Http} from './Http';
3331
import {NetworkHttp} from './NetworkHttp';
3432
import {QueryParams} from './QueryParams';
@@ -64,31 +62,28 @@ export class AccountHttp extends Http implements AccountRepository {
6462
*/
6563
public getAccountInfo(address: Address): Observable<AccountInfo> {
6664
return observableFrom(this.accountRoutesApi.getAccountInfo(address.plain())).pipe(
67-
map((response: { response: ClientResponse; body: AccountInfoDTO; }) => {
68-
const accountInfoDTO = response.body;
69-
return new AccountInfo(
70-
Address.createFromEncoded(accountInfoDTO.account.address),
71-
UInt64.fromNumericString(accountInfoDTO.account.addressHeight),
72-
accountInfoDTO.account.publicKey,
73-
UInt64.fromNumericString(accountInfoDTO.account.publicKeyHeight),
74-
accountInfoDTO.account.accountType.valueOf(),
75-
accountInfoDTO.account.linkedAccountKey,
76-
accountInfoDTO.account.activityBuckets.map((bucket) => {
65+
map(({body}) => new AccountInfo(
66+
Address.createFromEncoded(body.account.address),
67+
UInt64.fromNumericString(body.account.addressHeight),
68+
body.account.publicKey,
69+
UInt64.fromNumericString(body.account.publicKeyHeight),
70+
body.account.accountType.valueOf(),
71+
body.account.linkedAccountKey,
72+
body.account.activityBuckets.map((bucket) => {
7773
return new ActivityBucket(
7874
bucket.startHeight,
7975
bucket.totalFeesPaid,
8076
bucket.beneficiaryCount,
8177
bucket.rawScore,
8278
);
8379
}),
84-
accountInfoDTO.account.mosaics.map((mosaicDTO) => new Mosaic(
80+
body.account.mosaics.map((mosaicDTO) => new Mosaic(
8581
new MosaicId(mosaicDTO.id),
8682
UInt64.fromNumericString(mosaicDTO.amount),
8783
)),
88-
UInt64.fromNumericString(accountInfoDTO.account.importance),
89-
UInt64.fromNumericString(accountInfoDTO.account.importanceHeight),
90-
);
91-
}),
84+
UInt64.fromNumericString(body.account.importance),
85+
UInt64.fromNumericString(body.account.importanceHeight),
86+
)),
9287
catchError((error) => throwError(this.errorHandling(error))),
9388
);
9489
}
@@ -104,9 +99,7 @@ export class AccountHttp extends Http implements AccountRepository {
10499
};
105100
return observableFrom(
106101
this.accountRoutesApi.getAccountsInfo(accountIdsBody)).pipe(
107-
map((response: { response: ClientResponse; body: AccountInfoDTO[]; }) => {
108-
const accountsInfoMetaDataDTO = response.body;
109-
return accountsInfoMetaDataDTO.map((accountInfoDTO: AccountInfoDTO) => {
102+
map(({body}) => body.map((accountInfoDTO: AccountInfoDTO) => {
110103
return new AccountInfo(
111104
Address.createFromEncoded(accountInfoDTO.account.address),
112105
UInt64.fromNumericString(accountInfoDTO.account.addressHeight),
@@ -130,8 +123,7 @@ export class AccountHttp extends Http implements AccountRepository {
130123
UInt64.fromNumericString(accountInfoDTO.account.importanceHeight),
131124
);
132125

133-
});
134-
}),
126+
})),
135127
catchError((error) => throwError(this.errorHandling(error))),
136128
);
137129
}
@@ -148,12 +140,9 @@ export class AccountHttp extends Http implements AccountRepository {
148140
this.queryParams(queryParams).pageSize,
149141
this.queryParams(queryParams).id,
150142
this.queryParams(queryParams).order)).pipe(
151-
map((response: { response: ClientResponse; body: TransactionInfoDTO[]; }) => {
152-
const transactionsDTO = response.body;
153-
return transactionsDTO.map((transactionDTO) => {
143+
map(({body}) => body.map((transactionDTO) => {
154144
return CreateTransactionFromDTO(transactionDTO);
155-
});
156-
}),
145+
})),
157146
catchError((error) => throwError(this.errorHandling(error))),
158147
);
159148
}
@@ -171,12 +160,9 @@ export class AccountHttp extends Http implements AccountRepository {
171160
this.queryParams(queryParams).pageSize,
172161
this.queryParams(queryParams).id,
173162
this.queryParams(queryParams).order)).pipe(
174-
map((response: { response: ClientResponse; body: TransactionInfoDTO[]; }) => {
175-
const transactionsDTO = response.body;
176-
return transactionsDTO.map((transactionDTO) => {
163+
map(({body}) => body.map((transactionDTO) => {
177164
return CreateTransactionFromDTO(transactionDTO);
178-
});
179-
}),
165+
})),
180166
catchError((error) => throwError(this.errorHandling(error))),
181167
);
182168
}
@@ -194,12 +180,9 @@ export class AccountHttp extends Http implements AccountRepository {
194180
this.queryParams(queryParams).pageSize,
195181
this.queryParams(queryParams).id,
196182
this.queryParams(queryParams).order)).pipe(
197-
map((response: { response: ClientResponse; body: TransactionInfoDTO[]; }) => {
198-
const transactionsDTO = response.body;
199-
return transactionsDTO.map((transactionDTO) => {
183+
map(({body}) => body.map((transactionDTO) => {
200184
return CreateTransactionFromDTO(transactionDTO);
201-
});
202-
}),
185+
})),
203186
catchError((error) => throwError(this.errorHandling(error))),
204187
);
205188
}
@@ -218,12 +201,9 @@ export class AccountHttp extends Http implements AccountRepository {
218201
this.queryParams(queryParams).pageSize,
219202
this.queryParams(queryParams).id,
220203
this.queryParams(queryParams).order)).pipe(
221-
map((response: { response: ClientResponse; body: TransactionInfoDTO[]; }) => {
222-
const transactionsDTO = response.body;
223-
return transactionsDTO.map((transactionDTO) => {
204+
map(({body}) => body.map((transactionDTO) => {
224205
return CreateTransactionFromDTO(transactionDTO);
225-
});
226-
}),
206+
})),
227207
catchError((error) => throwError(this.errorHandling(error))),
228208
);
229209
}
@@ -241,12 +221,9 @@ export class AccountHttp extends Http implements AccountRepository {
241221
this.queryParams(queryParams).pageSize,
242222
this.queryParams(queryParams).id,
243223
this.queryParams(queryParams).order)).pipe(
244-
map((response: { response: ClientResponse; body: TransactionInfoDTO[]; }) => {
245-
const transactionsDTO = response.body;
246-
return transactionsDTO.map((transactionDTO) => {
224+
map(({body}) => body.map((transactionDTO) => {
247225
return CreateTransactionFromDTO(transactionDTO) as AggregateTransaction;
248-
});
249-
}),
226+
})),
250227
catchError((error) => throwError(this.errorHandling(error))),
251228
);
252229
}

src/infrastructure/BlockHttp.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ClientResponse } from 'http';
1817
import {from as observableFrom, Observable, throwError} from 'rxjs';
19-
import {catchError, map, mergeMap} from 'rxjs/operators';
18+
import {catchError, map} from 'rxjs/operators';
2019
import {PublicAccount} from '../model/account/PublicAccount';
2120
import {BlockInfo} from '../model/blockchain/BlockInfo';
2221
import { MerklePathItem } from '../model/blockchain/MerklePathItem';
2322
import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo';
24-
import { Statement } from '../model/receipt/Statement';
2523
import {Transaction} from '../model/transaction/Transaction';
2624
import {UInt64} from '../model/UInt64';
27-
import { BlockInfoDTO,
28-
BlockRoutesApi,
29-
MerkleProofInfoDTO,
30-
StatementsDTO,
31-
TransactionInfoDTO } from './api';
25+
import { BlockRoutesApi } from './api';
3226
import {BlockRepository} from './BlockRepository';
3327
import {Http} from './Http';
3428
import { NetworkHttp } from './NetworkHttp';
3529
import {QueryParams} from './QueryParams';
36-
import { CreateStatementFromDTO } from './receipt/CreateReceiptFromDTO';
3730
import {CreateTransactionFromDTO, extractBeneficiary} from './transaction/CreateTransactionFromDTO';
3831

3932
/**
@@ -80,8 +73,8 @@ export class BlockHttp extends Http implements BlockRepository {
8073
*/
8174
public getBlockByHeight(height: string): Observable<BlockInfo> {
8275
return observableFrom(this.blockRoutesApi.getBlockByHeight(height)).pipe(
83-
map((response: { response: ClientResponse; body: BlockInfoDTO; } ) => {
84-
const blockDTO = response.body;
76+
map(({body}) => {
77+
const blockDTO = body;
8578
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
8679
return new BlockInfo(
8780
blockDTO.meta.hash,
@@ -121,12 +114,9 @@ export class BlockHttp extends Http implements BlockRepository {
121114
this.queryParams(queryParams).pageSize,
122115
this.queryParams(queryParams).id,
123116
this.queryParams(queryParams).order))
124-
.pipe(map((response: { response: ClientResponse; body: TransactionInfoDTO[]; }) => {
125-
const transactionsDTO = response.body;
126-
return transactionsDTO.map((transactionDTO) => {
117+
.pipe(map(({body}) => body.map((transactionDTO) => {
127118
return CreateTransactionFromDTO(transactionDTO);
128-
});
129-
}),
119+
})),
130120
catchError((error) => throwError(this.errorHandling(error))),
131121
);
132122
}
@@ -140,9 +130,7 @@ export class BlockHttp extends Http implements BlockRepository {
140130
public getBlocksByHeightWithLimit(height: string, limit: LimitType = LimitType.N_25): Observable<BlockInfo[]> {
141131
return observableFrom(
142132
this.blockRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe(
143-
map((response: { response: ClientResponse; body: BlockInfoDTO[]; }) => {
144-
const blocksDTO = response.body;
145-
return blocksDTO.map((blockDTO) => {
133+
map(({body}) => body.map((blockDTO) => {
146134
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
147135
return new BlockInfo(
148136
blockDTO.meta.hash,
@@ -164,8 +152,7 @@ export class BlockHttp extends Http implements BlockRepository {
164152
blockDTO.block.stateHash,
165153
extractBeneficiary(blockDTO, networkType),
166154
);
167-
});
168-
}),
155+
})),
169156
catchError((error) => throwError(this.errorHandling(error))),
170157
);
171158
}
@@ -183,13 +170,9 @@ export class BlockHttp extends Http implements BlockRepository {
183170
public getMerkleTransaction(height: string, hash: string): Observable<MerkleProofInfo> {
184171
return observableFrom(
185172
this.blockRoutesApi.getMerkleTransaction(height, hash)).pipe(
186-
map((response: { response: ClientResponse; body: MerkleProofInfoDTO; } ) => {
187-
const merkleProofTransaction = response.body;
188-
return new MerkleProofInfo(
189-
merkleProofTransaction.merklePath!.map(
190-
(payload) => new MerklePathItem(payload.position, payload.hash)),
191-
);
192-
}),
173+
map(({body}) => new MerkleProofInfo(
174+
body.merklePath!.map((payload) => new MerklePathItem(payload.position, payload.hash)),
175+
)),
193176
catchError((error) => throwError(this.errorHandling(error))),
194177
);
195178
}

src/infrastructure/ChainHttp.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ClientResponse } from 'http';
1817
import {from as observableFrom, Observable, throwError} from 'rxjs';
1918
import {catchError, map} from 'rxjs/operators';
2019
import {BlockchainScore} from '../model/blockchain/BlockchainScore';
2120
import {UInt64} from '../model/UInt64';
22-
import { ChainRoutesApi,
23-
HeightInfoDTO } from './api';
21+
import { ChainRoutesApi } from './api';
2422
import { ChainRepository } from './ChainRepository';
2523
import {Http} from './Http';
26-
import { ChainScoreDTO } from './model/chainScoreDTO';
2724

2825
/**
2926
* Chian http repository.
@@ -52,10 +49,7 @@ export class ChainHttp extends Http implements ChainRepository {
5249
*/
5350
public getBlockchainHeight(): Observable<UInt64> {
5451
return observableFrom(this.chainRoutesApi.getChainHeight()).pipe(
55-
map((response: { response: ClientResponse; body: HeightInfoDTO; } ) => {
56-
const heightDTO = response.body;
57-
return UInt64.fromNumericString(heightDTO.height);
58-
}),
52+
map(({body}) => UInt64.fromNumericString(body.height)),
5953
catchError((error) => throwError(this.errorHandling(error))),
6054
);
6155
}
@@ -66,13 +60,10 @@ export class ChainHttp extends Http implements ChainRepository {
6660
*/
6761
public getChainScore(): Observable<BlockchainScore> {
6862
return observableFrom(this.chainRoutesApi.getChainScore()).pipe(
69-
map((response: { response: ClientResponse; body: ChainScoreDTO; } ) => {
70-
const blockchainScoreDTO = response.body;
71-
return new BlockchainScore(
72-
UInt64.fromNumericString(blockchainScoreDTO.scoreLow),
73-
UInt64.fromNumericString(blockchainScoreDTO.scoreHigh),
74-
);
75-
}),
63+
map(({body}) => new BlockchainScore(
64+
UInt64.fromNumericString(body.scoreLow),
65+
UInt64.fromNumericString(body.scoreHigh),
66+
)),
7667
catchError((error) => throwError(this.errorHandling(error))),
7768
);
7869
}

src/infrastructure/DiagnosticHttp.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ClientResponse } from 'http';
1817
import {from as observableFrom, Observable, throwError} from 'rxjs';
1918
import {catchError, map} from 'rxjs/operators';
2019
import {BlockchainStorageInfo} from '../model/blockchain/BlockchainStorageInfo';
2120
import { ServerInfo } from '../model/diagnostic/ServerInfo';
22-
import { DiagnosticRoutesApi, ServerDTO, StorageInfoDTO } from './api';
21+
import { DiagnosticRoutesApi } from './api';
2322
import {DiagnosticRepository} from './DiagnosticRepository';
2423
import {Http} from './Http';
2524

@@ -51,14 +50,11 @@ export class DiagnosticHttp extends Http implements DiagnosticRepository {
5150
public getDiagnosticStorage(): Observable<BlockchainStorageInfo> {
5251
return observableFrom(
5352
this.diagnosticRoutesApi.getDiagnosticStorage()).pipe(
54-
map((response: { response: ClientResponse; body: StorageInfoDTO; } ) => {
55-
const blockchainStorageInfoDTO = response.body;
56-
return new BlockchainStorageInfo(
57-
blockchainStorageInfoDTO.numBlocks,
58-
blockchainStorageInfoDTO.numTransactions,
59-
blockchainStorageInfoDTO.numAccounts,
60-
);
61-
}),
53+
map(({body}) => new BlockchainStorageInfo(
54+
body.numBlocks,
55+
body.numTransactions,
56+
body.numAccounts,
57+
)),
6258
catchError((error) => throwError(this.errorHandling(error))),
6359
);
6460
}
@@ -70,11 +66,7 @@ export class DiagnosticHttp extends Http implements DiagnosticRepository {
7066
public getServerInfo(): Observable<ServerInfo> {
7167
return observableFrom(
7268
this.diagnosticRoutesApi.getServerInfo()).pipe(
73-
map((response: { response: ClientResponse; body: ServerDTO; } ) => {
74-
const serverDTO = response.body;
75-
return new ServerInfo(serverDTO.restVersion,
76-
serverDTO.sdkVersion);
77-
}),
69+
map(({body}) => new ServerInfo(body.serverInfo.restVersion, body.serverInfo.sdkVersion)),
7870
catchError((error) => throwError(this.errorHandling(error))),
7971
);
8072
}

0 commit comments

Comments
 (0)