Skip to content

Commit

Permalink
TASK-3_1: Refactor blockchain related repositories codes to make them…
Browse files Browse the repository at this point in the history
… easier to read and maintain (#11)
  • Loading branch information
BigtoC committed May 9, 2022
1 parent 62549c2 commit 42cca90
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 75 deletions.
16 changes: 8 additions & 8 deletions lib/application/blockchain_info/blockchain_info_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:rxdart/rxdart.dart';

import 'package:hnotes/Infrastructure/blockchain/services/blockchain_repository.dart';
import 'package:hnotes/Infrastructure/blockchain/blockchain_info_repository.dart';

class BlockchainInfoBloc {
final BlockchainRepository _blockchainRepository = BlockchainRepository();
final BlockchainInfoRepository _blockchainInfoRepository = BlockchainInfoRepository();

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

fetchAllBlockchainInfo() async {
Future.wait([
_sinkData(_latestBlockNumberData, _blockchainRepository.getLatestBlockNumber()),
_sinkData(_currentGasPriceData, _blockchainRepository.getCurrentGasPrice()),
_sinkData(_currentNetworkData, _blockchainRepository.getNetwork()),
_sinkData(_chainIdData, _blockchainRepository.getChainId()),
_sinkData(_nodeClientVersionData, _blockchainRepository.getNodeClientVersion())
_sinkData(_latestBlockNumberData, _blockchainInfoRepository.getLatestBlockNumber()),
_sinkData(_currentGasPriceData, _blockchainInfoRepository.getCurrentGasPrice()),
_sinkData(_currentNetworkData, _blockchainInfoRepository.getNetwork()),
_sinkData(_chainIdData, _blockchainInfoRepository.getChainId()),
_sinkData(_nodeClientVersionData, _blockchainInfoRepository.getNodeClientVersion())
]);
}

fetchNetworkData() async {
await _sinkData(_currentNetworkData, _blockchainRepository.getNetwork());
await _sinkData(_currentNetworkData, _blockchainInfoRepository.getNetwork());
}

Future<void> _sinkData(PublishSubject data, Future fetchFunction) async {
Expand Down
4 changes: 2 additions & 2 deletions lib/domain/blockchain/boolean_dto.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:core';
import 'dart:convert';

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

import 'base_dto.dart';
import 'package:hnotes/domain/common_data.dart';
import 'package:hnotes/infrastructure/blockchain/services/request_helper.dart';


// For response results that only contain a text string
Expand All @@ -14,7 +14,7 @@ class BooleanResultDto extends BaseResultDto {
BooleanResultDto.fromResponse(http.Response response, String method) {
int statusCode = response.statusCode;
if (statusCode == 200) {
final bool result = phraseResponseBooleanData(response.body, "result");
final bool result = jsonDecode(response.body)["result"];
this.boolean = result;
} else {
final String errorMsg = "Query $method failed ($statusCode): ${response.body}";
Expand Down
4 changes: 2 additions & 2 deletions lib/domain/blockchain/number_dto.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:convert';
import 'package:http/http.dart' as http;

import 'package:hnotes/domain/common_data.dart';
import 'package:hnotes/domain/blockchain/base_dto.dart';
import 'package:hnotes/infrastructure/blockchain/services/request_helper.dart';


// For response results that only contain one number
Expand All @@ -13,7 +13,7 @@ class NumberResultDto extends BaseResultDto {
NumberResultDto.fromResponse(http.Response response, String method) {
int statusCode = response.statusCode;
if (statusCode == 200) {
final String result = phraseResponseData(response.body, "result");
final String result = jsonDecode(response.body)["result"];
if (result.contains("0x")) {
this.hexNumber = result;
final String number = int.tryParse(result).toString();
Expand Down
4 changes: 2 additions & 2 deletions lib/domain/blockchain/text_dto.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:convert';
import 'package:http/http.dart' as http;

import 'base_dto.dart';
import 'package:hnotes/domain/common_data.dart';
import 'package:hnotes/infrastructure/blockchain/services/request_helper.dart';


// For response results that only contain a text string
Expand All @@ -12,7 +12,7 @@ class TextResultDto extends BaseResultDto {
TextResultDto.fromResponse(http.Response response, String method) {
int statusCode = response.statusCode;
if (statusCode == 200) {
final String result = phraseResponseData(response.body, "result");
final String result = jsonDecode(response.body)["result"];
this.text = result;
} else {
final String errorMsg = "Query $method failed ($statusCode): ${response.body}";
Expand Down
41 changes: 41 additions & 0 deletions lib/infrastructure/blockchain/base_blockchain_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart';
import 'package:http/http.dart' as http;

import 'package:hnotes/domain/secret/secret_model.dart';
import 'package:hnotes/infrastructure/local_storage/secrets/secrets_repository.dart';


class BaseBlockchainRepository {
final client = http.Client();

List _defaultParameter = [];
final _requestHeaders = {'Content-type': 'application/json'};
final SecretRepository _secretRepository = new SecretRepository();

Future<SecretModel> _readSecrets() async {
SecretModel _secretModel = await _secretRepository.getApiSecret();
return _secretModel;
}

String formPostRequestBody(String method, [var parameter]) {
parameter ??= _defaultParameter;

return jsonEncode({
"jsonrpc":"2.0",
"method": method,
"params": parameter,
"id": DateTime.now().millisecondsSinceEpoch
});
}

Future<Response> makePostRequest(String requestBody) async {
SecretModel secret = await _readSecrets();
return await client.post(
Uri.parse(secret.urlWithKey),
headers: _requestHeaders,
body: requestBody,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import 'dart:async';
import 'package:http/http.dart' as http;

import 'package:hnotes/domain/blockchain/dto_collections.dart';
import 'package:hnotes/infrastructure/blockchain/services/request_helper.dart';
import 'package:hnotes/infrastructure/blockchain/base_blockchain_repository.dart';


final requestHeaders = {'Content-type': 'application/json'};


class BlockchainRepository {
final client = http.Client();
class BlockchainInfoRepository extends BaseBlockchainRepository {

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

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

return await makeRequest(requestBody)
return await makePostRequest(requestBody)
.then((response) {
NumberResultDto dto = NumberResultDto.fromResponse(response, methodName);
return dto;
Expand All @@ -65,9 +60,9 @@ class BlockchainRepository {

/// For API responses that only contain a text string
Future<TextResultDto> _parseTextResult(String methodName) async {
final String requestBody = formRequestBody(methodName);
final String requestBody = formPostRequestBody(methodName);

return await makeRequest(requestBody)
return await makePostRequest(requestBody)
.then((response) {
TextResultDto dto = TextResultDto.fromResponse(response, methodName);
return dto;
Expand Down
9 changes: 9 additions & 0 deletions lib/infrastructure/blockchain/nft_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'dart:async';

import 'package:hnotes/domain/blockchain/dto_collections.dart';
import 'package:hnotes/infrastructure/blockchain/base_blockchain_repository.dart';


class NftRepository extends BaseBlockchainRepository {

}
50 changes: 0 additions & 50 deletions lib/infrastructure/blockchain/services/request_helper.dart

This file was deleted.

0 comments on commit 42cca90

Please sign in to comment.