Skip to content

Commit 42cca90

Browse files
authored
TASK-3_1: Refactor blockchain related repositories codes to make them easier to read and maintain (#11)
1 parent 62549c2 commit 42cca90

8 files changed

Lines changed: 70 additions & 75 deletions

File tree

lib/application/blockchain_info/blockchain_info_bloc.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:rxdart/rxdart.dart';
22

3-
import 'package:hnotes/Infrastructure/blockchain/services/blockchain_repository.dart';
3+
import 'package:hnotes/Infrastructure/blockchain/blockchain_info_repository.dart';
44

55
class BlockchainInfoBloc {
6-
final BlockchainRepository _blockchainRepository = BlockchainRepository();
6+
final BlockchainInfoRepository _blockchainInfoRepository = BlockchainInfoRepository();
77

88
final _latestBlockNumberData = new PublishSubject<Map<String, dynamic>>();
99
final _currentGasPriceData = new PublishSubject<Map<String, dynamic>>();
@@ -26,16 +26,16 @@ class BlockchainInfoBloc {
2626

2727
fetchAllBlockchainInfo() async {
2828
Future.wait([
29-
_sinkData(_latestBlockNumberData, _blockchainRepository.getLatestBlockNumber()),
30-
_sinkData(_currentGasPriceData, _blockchainRepository.getCurrentGasPrice()),
31-
_sinkData(_currentNetworkData, _blockchainRepository.getNetwork()),
32-
_sinkData(_chainIdData, _blockchainRepository.getChainId()),
33-
_sinkData(_nodeClientVersionData, _blockchainRepository.getNodeClientVersion())
29+
_sinkData(_latestBlockNumberData, _blockchainInfoRepository.getLatestBlockNumber()),
30+
_sinkData(_currentGasPriceData, _blockchainInfoRepository.getCurrentGasPrice()),
31+
_sinkData(_currentNetworkData, _blockchainInfoRepository.getNetwork()),
32+
_sinkData(_chainIdData, _blockchainInfoRepository.getChainId()),
33+
_sinkData(_nodeClientVersionData, _blockchainInfoRepository.getNodeClientVersion())
3434
]);
3535
}
3636

3737
fetchNetworkData() async {
38-
await _sinkData(_currentNetworkData, _blockchainRepository.getNetwork());
38+
await _sinkData(_currentNetworkData, _blockchainInfoRepository.getNetwork());
3939
}
4040

4141
Future<void> _sinkData(PublishSubject data, Future fetchFunction) async {

lib/domain/blockchain/boolean_dto.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import 'dart:core';
2+
import 'dart:convert';
23

34
import 'package:http/http.dart' as http;
45

56
import 'base_dto.dart';
67
import 'package:hnotes/domain/common_data.dart';
7-
import 'package:hnotes/infrastructure/blockchain/services/request_helper.dart';
88

99

1010
// For response results that only contain a text string
@@ -14,7 +14,7 @@ class BooleanResultDto extends BaseResultDto {
1414
BooleanResultDto.fromResponse(http.Response response, String method) {
1515
int statusCode = response.statusCode;
1616
if (statusCode == 200) {
17-
final bool result = phraseResponseBooleanData(response.body, "result");
17+
final bool result = jsonDecode(response.body)["result"];
1818
this.boolean = result;
1919
} else {
2020
final String errorMsg = "Query $method failed ($statusCode): ${response.body}";

lib/domain/blockchain/number_dto.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import 'dart:convert';
12
import 'package:http/http.dart' as http;
23

34
import 'package:hnotes/domain/common_data.dart';
45
import 'package:hnotes/domain/blockchain/base_dto.dart';
5-
import 'package:hnotes/infrastructure/blockchain/services/request_helper.dart';
66

77

88
// For response results that only contain one number
@@ -13,7 +13,7 @@ class NumberResultDto extends BaseResultDto {
1313
NumberResultDto.fromResponse(http.Response response, String method) {
1414
int statusCode = response.statusCode;
1515
if (statusCode == 200) {
16-
final String result = phraseResponseData(response.body, "result");
16+
final String result = jsonDecode(response.body)["result"];
1717
if (result.contains("0x")) {
1818
this.hexNumber = result;
1919
final String number = int.tryParse(result).toString();

lib/domain/blockchain/text_dto.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import 'dart:convert';
12
import 'package:http/http.dart' as http;
23

34
import 'base_dto.dart';
45
import 'package:hnotes/domain/common_data.dart';
5-
import 'package:hnotes/infrastructure/blockchain/services/request_helper.dart';
66

77

88
// For response results that only contain a text string
@@ -12,7 +12,7 @@ class TextResultDto extends BaseResultDto {
1212
TextResultDto.fromResponse(http.Response response, String method) {
1313
int statusCode = response.statusCode;
1414
if (statusCode == 200) {
15-
final String result = phraseResponseData(response.body, "result");
15+
final String result = jsonDecode(response.body)["result"];
1616
this.text = result;
1717
} else {
1818
final String errorMsg = "Query $method failed ($statusCode): ${response.body}";
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
import 'package:http/http.dart';
4+
import 'package:http/http.dart' as http;
5+
6+
import 'package:hnotes/domain/secret/secret_model.dart';
7+
import 'package:hnotes/infrastructure/local_storage/secrets/secrets_repository.dart';
8+
9+
10+
class BaseBlockchainRepository {
11+
final client = http.Client();
12+
13+
List _defaultParameter = [];
14+
final _requestHeaders = {'Content-type': 'application/json'};
15+
final SecretRepository _secretRepository = new SecretRepository();
16+
17+
Future<SecretModel> _readSecrets() async {
18+
SecretModel _secretModel = await _secretRepository.getApiSecret();
19+
return _secretModel;
20+
}
21+
22+
String formPostRequestBody(String method, [var parameter]) {
23+
parameter ??= _defaultParameter;
24+
25+
return jsonEncode({
26+
"jsonrpc":"2.0",
27+
"method": method,
28+
"params": parameter,
29+
"id": DateTime.now().millisecondsSinceEpoch
30+
});
31+
}
32+
33+
Future<Response> makePostRequest(String requestBody) async {
34+
SecretModel secret = await _readSecrets();
35+
return await client.post(
36+
Uri.parse(secret.urlWithKey),
37+
headers: _requestHeaders,
38+
body: requestBody,
39+
);
40+
}
41+
}

lib/infrastructure/blockchain/services/blockchain_repository.dart renamed to lib/infrastructure/blockchain/blockchain_info_repository.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import 'dart:async';
2-
import 'package:http/http.dart' as http;
32

43
import 'package:hnotes/domain/blockchain/dto_collections.dart';
5-
import 'package:hnotes/infrastructure/blockchain/services/request_helper.dart';
4+
import 'package:hnotes/infrastructure/blockchain/base_blockchain_repository.dart';
65

76

8-
final requestHeaders = {'Content-type': 'application/json'};
9-
10-
11-
class BlockchainRepository {
12-
final client = http.Client();
7+
class BlockchainInfoRepository extends BaseBlockchainRepository {
138

149
/// Returns the number of the most recent block.
1510
Future<Map<String, dynamic>> getLatestBlockNumber() async {
@@ -54,9 +49,9 @@ class BlockchainRepository {
5449

5550
/// For API responses that only contain a number (hex or decimal)
5651
Future<NumberResultDto> _parseNumberResultDto(String methodName) async {
57-
final String requestBody = formRequestBody(methodName);
52+
final String requestBody = formPostRequestBody(methodName);
5853

59-
return await makeRequest(requestBody)
54+
return await makePostRequest(requestBody)
6055
.then((response) {
6156
NumberResultDto dto = NumberResultDto.fromResponse(response, methodName);
6257
return dto;
@@ -65,9 +60,9 @@ class BlockchainRepository {
6560

6661
/// For API responses that only contain a text string
6762
Future<TextResultDto> _parseTextResult(String methodName) async {
68-
final String requestBody = formRequestBody(methodName);
63+
final String requestBody = formPostRequestBody(methodName);
6964

70-
return await makeRequest(requestBody)
65+
return await makePostRequest(requestBody)
7166
.then((response) {
7267
TextResultDto dto = TextResultDto.fromResponse(response, methodName);
7368
return dto;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'dart:async';
2+
3+
import 'package:hnotes/domain/blockchain/dto_collections.dart';
4+
import 'package:hnotes/infrastructure/blockchain/base_blockchain_repository.dart';
5+
6+
7+
class NftRepository extends BaseBlockchainRepository {
8+
9+
}

lib/infrastructure/blockchain/services/request_helper.dart

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)