Skip to content

Commit 198e8d6

Browse files
committed
Improved rest dto mappings
1 parent 2f70b52 commit 198e8d6

File tree

4 files changed

+130
-146
lines changed

4 files changed

+130
-146
lines changed

src/infrastructure/AccountHttp.ts

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,10 @@ export class AccountHttp extends Http implements AccountRepository {
6161
*/
6262
public getAccountInfo(address: Address): Observable<AccountInfo> {
6363
return observableFrom(this.accountRoutesApi.getAccountInfo(address.plain())).pipe(
64-
map(({body}) => new AccountInfo(
65-
Address.createFromEncoded(body.account.address),
66-
UInt64.fromNumericString(body.account.addressHeight),
67-
body.account.publicKey,
68-
UInt64.fromNumericString(body.account.publicKeyHeight),
69-
body.account.accountType.valueOf(),
70-
body.account.linkedAccountKey,
71-
body.account.activityBuckets.map((bucket) => {
72-
return new ActivityBucket(
73-
bucket.startHeight,
74-
bucket.totalFeesPaid,
75-
bucket.beneficiaryCount,
76-
bucket.rawScore,
77-
);
78-
}),
79-
body.account.mosaics.map((mosaicDTO) => new Mosaic(
80-
new MosaicId(mosaicDTO.id),
81-
UInt64.fromNumericString(mosaicDTO.amount),
82-
)),
83-
UInt64.fromNumericString(body.account.importance),
84-
UInt64.fromNumericString(body.account.importanceHeight),
85-
)),
64+
map(({body}) => this.toAccountInfo(body)),
8665
catchError((error) => throwError(this.errorHandling(error))),
8766
);
8867
}
89-
9068
/**
9169
* Gets AccountsInfo for different accounts.
9270
* @param addresses List of Address
@@ -98,35 +76,45 @@ export class AccountHttp extends Http implements AccountRepository {
9876
};
9977
return observableFrom(
10078
this.accountRoutesApi.getAccountsInfo(accountIdsBody)).pipe(
101-
map(({body}) => body.map((accountInfoDTO: AccountInfoDTO) => {
102-
return new AccountInfo(
103-
Address.createFromEncoded(accountInfoDTO.account.address),
104-
UInt64.fromNumericString(accountInfoDTO.account.addressHeight),
105-
accountInfoDTO.account.publicKey,
106-
UInt64.fromNumericString(accountInfoDTO.account.publicKeyHeight),
107-
accountInfoDTO.account.accountType.valueOf(),
108-
accountInfoDTO.account.linkedAccountKey,
109-
accountInfoDTO.account.activityBuckets.map((bucket) => {
110-
return new ActivityBucket(
111-
bucket.startHeight,
112-
bucket.totalFeesPaid,
113-
bucket.beneficiaryCount,
114-
bucket.rawScore,
115-
);
116-
}),
117-
accountInfoDTO.account.mosaics.map((mosaicDTO) => new Mosaic(
118-
new MosaicId(mosaicDTO.id),
119-
UInt64.fromNumericString(mosaicDTO.amount),
120-
)),
121-
UInt64.fromNumericString(accountInfoDTO.account.importance),
122-
UInt64.fromNumericString(accountInfoDTO.account.importanceHeight),
123-
);
124-
125-
})),
79+
map(({body}) => body.map(this.toAccountInfo)),
12680
catchError((error) => throwError(this.errorHandling(error))),
12781
);
12882
}
12983

84+
85+
/**
86+
* This method maps a AccountInfoDTO from rest to the SDK's AccountInfo model object.
87+
*
88+
* @internal
89+
* @param {AccountInfoDTO} dto AccountInfoDTO the dto object from rest.
90+
* @returns AccountInfo model
91+
*/
92+
private toAccountInfo(dto: AccountInfoDTO):AccountInfo {
93+
return new AccountInfo(
94+
Address.createFromEncoded(dto.account.address),
95+
UInt64.fromNumericString(dto.account.addressHeight),
96+
dto.account.publicKey,
97+
UInt64.fromNumericString(dto.account.publicKeyHeight),
98+
dto.account.accountType.valueOf(),
99+
dto.account.linkedAccountKey,
100+
dto.account.activityBuckets.map((bucket) => {
101+
return new ActivityBucket(
102+
bucket.startHeight,
103+
bucket.totalFeesPaid,
104+
bucket.beneficiaryCount,
105+
bucket.rawScore,
106+
);
107+
}),
108+
dto.account.mosaics.map((mosaicDTO) => new Mosaic(
109+
new MosaicId(mosaicDTO.id),
110+
UInt64.fromNumericString(mosaicDTO.amount),
111+
)),
112+
UInt64.fromNumericString(dto.account.importance),
113+
UInt64.fromNumericString(dto.account.importanceHeight),
114+
);
115+
}
116+
117+
130118
/**
131119
* Gets an array of confirmed transactions for which an account is signer or receiver.
132120
* @param address - * Address can be created rawAddress or publicKey

src/infrastructure/BlockHttp.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,30 @@ export class BlockHttp extends Http implements BlockRepository {
116116
* This method maps a BlockInfoDTO from rest to the SDK's BlockInfo model object.
117117
*
118118
* @internal
119-
* @param {BlockInfoDTO} blockDTO the dto object from rest.
119+
* @param {BlockInfoDTO} dto the dto object from rest.
120120
* @returns {BlockInfo} a BlockInfo model
121121
*/
122-
private toBlockInfo(blockDTO: BlockInfoDTO): BlockInfo {
123-
const networkType = blockDTO.block.network.valueOf();
122+
private toBlockInfo(dto: BlockInfoDTO): BlockInfo {
123+
const networkType = dto.block.network.valueOf();
124124
return new BlockInfo(
125-
blockDTO.meta.hash,
126-
blockDTO.meta.generationHash,
127-
UInt64.fromNumericString(blockDTO.meta.totalFee),
128-
blockDTO.meta.numTransactions,
129-
blockDTO.block.signature,
130-
PublicAccount.createFromPublicKey(blockDTO.block.signerPublicKey, networkType),
125+
dto.meta.hash,
126+
dto.meta.generationHash,
127+
UInt64.fromNumericString(dto.meta.totalFee),
128+
dto.meta.numTransactions,
129+
dto.block.signature,
130+
PublicAccount.createFromPublicKey(dto.block.signerPublicKey, networkType),
131131
networkType,
132-
blockDTO.block.version,
133-
blockDTO.block.type,
134-
UInt64.fromNumericString(blockDTO.block.height),
135-
UInt64.fromNumericString(blockDTO.block.timestamp),
136-
UInt64.fromNumericString(blockDTO.block.difficulty),
137-
blockDTO.block.feeMultiplier,
138-
blockDTO.block.previousBlockHash,
139-
blockDTO.block.transactionsHash,
140-
blockDTO.block.receiptsHash,
141-
blockDTO.block.stateHash,
142-
extractBeneficiary(blockDTO, networkType),
132+
dto.block.version,
133+
dto.block.type,
134+
UInt64.fromNumericString(dto.block.height),
135+
UInt64.fromNumericString(dto.block.timestamp),
136+
UInt64.fromNumericString(dto.block.difficulty),
137+
dto.block.feeMultiplier,
138+
dto.block.previousBlockHash,
139+
dto.block.transactionsHash,
140+
dto.block.receiptsHash,
141+
dto.block.stateHash,
142+
extractBeneficiary(dto, networkType),
143143
);
144144
}
145145

src/infrastructure/RestrictionMosaicHttp.ts

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { MosaicGlobalRestrictionItem } from '../model/restriction/MosaicGlobalRe
2525
import { RestrictionMosaicRoutesApi } from './api/restrictionMosaicRoutesApi';
2626
import {Http} from './Http';
2727
import { RestrictionMosaicRepository } from './RestrictionMosaicRespository';
28+
import { MosaicAddressRestrictionDTO } from "./model/mosaicAddressRestrictionDTO";
29+
import { MosaicGlobalRestrictionDTO } from "./model/mosaicGlobalRestrictionDTO";
2830

2931
/**
3032
* RestrictionMosaic http repository.
@@ -58,20 +60,7 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
5860
getMosaicAddressRestriction(mosaicId: MosaicId, address: Address): Observable<MosaicAddressRestriction> {
5961
return observableFrom(
6062
this.restrictionMosaicRoutesApi.getMosaicAddressRestriction(mosaicId.toHex(), address.plain())).pipe(
61-
map(({body}) => {
62-
const payload = body.mosaicRestrictionEntry;
63-
const restirctionItems = new Map<string, string>();
64-
payload.restrictions.forEach((restriction) => {
65-
restirctionItems.set(restriction.key, restriction.value);
66-
});
67-
return new MosaicAddressRestriction(
68-
payload.compositeHash,
69-
payload.entryType.valueOf(),
70-
new MosaicId(payload.mosaicId),
71-
Address.createFromEncoded(payload.targetAddress),
72-
restirctionItems,
73-
);
74-
}),
63+
map(({body}) => this.toMosaicAddressRestriction(body)),
7564
catchError((error) => throwError(this.errorHandling(error))),
7665
);
7766
}
@@ -89,23 +78,32 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
8978
};
9079
return observableFrom(
9180
this.restrictionMosaicRoutesApi.getMosaicAddressRestrictions(mosaicId.toHex(), accountIds)).pipe(
92-
map(({body}) => body.map((payload) => {
93-
const restirctionItems = new Map<string, string>();
94-
payload.mosaicRestrictionEntry.restrictions.forEach((restriction) => {
95-
restirctionItems.set(restriction.key, restriction.value);
96-
});
97-
return new MosaicAddressRestriction(
98-
payload.mosaicRestrictionEntry.compositeHash,
99-
payload.mosaicRestrictionEntry.entryType.valueOf(),
100-
new MosaicId(payload.mosaicRestrictionEntry.mosaicId),
101-
Address.createFromEncoded(payload.mosaicRestrictionEntry.targetAddress),
102-
restirctionItems,
103-
);
104-
})),
81+
map(({body}) => body.map(this.toMosaicAddressRestriction)),
10582
catchError((error) => throwError(this.errorHandling(error))),
10683
);
10784
}
10885

86+
/**
87+
* This method maps a MosaicAddressRestrictionDTO from rest to the SDK's MosaicAddressRestriction model object.
88+
*
89+
* @internal
90+
* @param {MosaicAddressRestrictionDTO} dto the MosaicAddressRestrictionDTO object from rest.
91+
* @returns {MosaicAddressRestriction} a MosaicAddressRestriction model
92+
*/
93+
private toMosaicAddressRestriction(dto: MosaicAddressRestrictionDTO): MosaicAddressRestriction {
94+
const restrictionItems = new Map<string, string>();
95+
dto.mosaicRestrictionEntry.restrictions.forEach((restriction) => {
96+
restrictionItems.set(restriction.key, restriction.value);
97+
});
98+
return new MosaicAddressRestriction(
99+
dto.mosaicRestrictionEntry.compositeHash,
100+
dto.mosaicRestrictionEntry.entryType.valueOf(),
101+
new MosaicId(dto.mosaicRestrictionEntry.mosaicId),
102+
Address.createFromEncoded(dto.mosaicRestrictionEntry.targetAddress),
103+
restrictionItems,
104+
);
105+
}
106+
109107
/**
110108
* Get mosaic global restriction.
111109
* @summary Get mosaic global restrictions for a given mosaic identifier.
@@ -115,23 +113,7 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
115113
getMosaicGlobalRestriction(mosaicId: MosaicId): Observable<MosaicGlobalRestriction> {
116114
return observableFrom(
117115
this.restrictionMosaicRoutesApi.getMosaicGlobalRestriction(mosaicId.toHex())).pipe(
118-
map(({body}) => {
119-
const payload = body.mosaicRestrictionEntry;
120-
const restirctionItems = new Map<string, MosaicGlobalRestrictionItem>();
121-
payload.restrictions.forEach((restriction) =>
122-
restirctionItems.set(restriction.key,
123-
new MosaicGlobalRestrictionItem(
124-
new MosaicId(restriction.restriction.referenceMosaicId),
125-
restriction.restriction.restrictionValue,
126-
restriction.restriction.restrictionType.valueOf(),
127-
)));
128-
return new MosaicGlobalRestriction(
129-
payload.compositeHash,
130-
payload.entryType.valueOf(),
131-
new MosaicId(payload.mosaicId),
132-
restirctionItems,
133-
);
134-
}),
116+
map(({body}) => this.toMosaicGlobalRestriction(body)),
135117
catchError((error) => throwError(this.errorHandling(error))),
136118
);
137119
}
@@ -148,23 +130,32 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
148130
};
149131
return observableFrom(
150132
this.restrictionMosaicRoutesApi.getMosaicGlobalRestrictions(mosaicIdsBody)).pipe(
151-
map(({body}) => body.map((payload) => {
152-
const restirctionItems = new Map<string, MosaicGlobalRestrictionItem>();
153-
payload.mosaicRestrictionEntry.restrictions.forEach((restriction) =>
154-
restirctionItems.set(restriction.key,
155-
new MosaicGlobalRestrictionItem(
156-
new MosaicId(restriction.restriction.referenceMosaicId),
157-
restriction.restriction.restrictionValue,
158-
restriction.restriction.restrictionType.valueOf(),
159-
)));
160-
return new MosaicGlobalRestriction(
161-
payload.mosaicRestrictionEntry.compositeHash,
162-
payload.mosaicRestrictionEntry.entryType.valueOf(),
163-
new MosaicId(payload.mosaicRestrictionEntry.mosaicId),
164-
restirctionItems,
165-
);
166-
})),
133+
map(({body}) => body.map(this.toMosaicGlobalRestriction)),
167134
catchError((error) => throwError(this.errorHandling(error))),
168135
);
169136
}
137+
138+
/**
139+
* This method maps a MosaicGlobalRestrictionDTO from rest to the SDK's MosaicGlobalRestriction model object.
140+
*
141+
* @internal
142+
* @param {MosaicGlobalRestrictionDTO} dto the MosaicGlobalRestrictionDTO object from rest.
143+
* @returns {MosaicGlobalRestriction} a MosaicGlobalRestriction model
144+
*/
145+
private toMosaicGlobalRestriction(dto: MosaicGlobalRestrictionDTO): MosaicGlobalRestriction {
146+
const restirctionItems = new Map<string, MosaicGlobalRestrictionItem>();
147+
dto.mosaicRestrictionEntry.restrictions.forEach((restriction) =>
148+
restirctionItems.set(restriction.key,
149+
new MosaicGlobalRestrictionItem(
150+
new MosaicId(restriction.restriction.referenceMosaicId),
151+
restriction.restriction.restrictionValue,
152+
restriction.restriction.restrictionType.valueOf(),
153+
)));
154+
return new MosaicGlobalRestriction(
155+
dto.mosaicRestrictionEntry.compositeHash,
156+
dto.mosaicRestrictionEntry.entryType.valueOf(),
157+
new MosaicId(dto.mosaicRestrictionEntry.mosaicId),
158+
restirctionItems,
159+
);
160+
}
170161
}

src/infrastructure/TransactionHttp.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ import {TransactionInfo} from '../model/transaction/TransactionInfo';
3030
import {TransactionStatus} from '../model/transaction/TransactionStatus';
3131
import {TransactionType} from '../model/transaction/TransactionType';
3232
import {UInt64} from '../model/UInt64';
33-
import { BlockInfoDTO, BlockRoutesApi,
34-
TransactionRoutesApi } from './api';
33+
import {
34+
BlockInfoDTO, BlockRoutesApi,
35+
TransactionRoutesApi, TransactionStatusDTO
36+
} from './api';
3537
import {Http} from './Http';
3638
import {CreateTransactionFromDTO} from './transaction/CreateTransactionFromDTO';
3739
import {TransactionRepository} from './TransactionRepository';
@@ -102,13 +104,7 @@ export class TransactionHttp extends Http implements TransactionRepository {
102104
*/
103105
public getTransactionStatus(transactionHash: string): Observable<TransactionStatus> {
104106
return observableFrom(this.transactionRoutesApi.getTransactionStatus(transactionHash)).pipe(
105-
map(({body}) => new TransactionStatus(
106-
body.status,
107-
body.group,
108-
body.hash,
109-
body.deadline ?
110-
Deadline.createFromDTO(UInt64.fromNumericString(body.deadline).toDTO()) : undefined,
111-
body.height ? UInt64.fromNumericString(body.height) : undefined)),
107+
map(({body}) => this.toTransactionStatus(body)),
112108
catchError((error) => throwError(this.errorHandling(error))),
113109
);
114110
}
@@ -124,19 +120,28 @@ export class TransactionHttp extends Http implements TransactionRepository {
124120
};
125121
return observableFrom(
126122
this.transactionRoutesApi.getTransactionsStatuses(transactionHashesBody)).pipe(
127-
map(({body}) => body.map((transactionStatusDTO) => {
128-
return new TransactionStatus(
129-
transactionStatusDTO.status,
130-
transactionStatusDTO.group,
131-
transactionStatusDTO.hash,
132-
transactionStatusDTO.deadline ?
133-
Deadline.createFromDTO(UInt64.fromNumericString(transactionStatusDTO.deadline).toDTO()) : undefined,
134-
transactionStatusDTO.height ? UInt64.fromNumericString(transactionStatusDTO.height) : undefined);
135-
})),
123+
map(({body}) => body.map(this.toTransactionStatus)),
136124
catchError((error) => throwError(this.errorHandling(error))),
137125
);
138126
}
139127

128+
/**
129+
* This method maps a TransactionStatusDTO from rest to the SDK's TransactionStatus model object.
130+
*
131+
* @internal
132+
* @param {TransactionStatusDTO} dto the TransactionStatusDTO object from rest.
133+
* @returns {TransactionStatus} a TransactionStatus model
134+
*/
135+
private toTransactionStatus(dto: TransactionStatusDTO): TransactionStatus {
136+
return new TransactionStatus(
137+
dto.status,
138+
dto.group,
139+
dto.hash,
140+
dto.deadline ?
141+
Deadline.createFromDTO(UInt64.fromNumericString(dto.deadline).toDTO()) : undefined,
142+
dto.height ? UInt64.fromNumericString(dto.height) : undefined);
143+
}
144+
140145
/**
141146
* Send a signed transaction
142147
* @param signedTransaction - Signed transaction

0 commit comments

Comments
 (0)