Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/packages/service/dfx/dfx_bank_account_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:realunit_wallet/packages/config/api_config.dart';
import 'package:realunit_wallet/packages/service/app_store.dart';
import 'package:realunit_wallet/packages/service/dfx/exceptions/api_exception.dart';
import 'package:realunit_wallet/packages/service/dfx/models/bank_account/dto/bank_account_dto.dart';

class DfxBankAccountService {
Expand All @@ -26,7 +27,8 @@ class DfxBankAccountService {
);

if (response.statusCode != 200 && response.statusCode != 201) {
throw Exception('Failed to add bank account: ${response.body}');
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

final List<dynamic> jsonList = jsonDecode(response.body);
Expand All @@ -51,7 +53,8 @@ class DfxBankAccountService {
);

if (response.statusCode != 200 && response.statusCode != 201) {
throw Exception('Failed to add bank account: ${response.body}');
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

return BankAccountDto.fromJson(jsonDecode(response.body));
Expand Down Expand Up @@ -80,7 +83,8 @@ class DfxBankAccountService {
);

if (response.statusCode != 200 && response.statusCode != 201) {
throw Exception('Failed to update bank account: ${response.body}');
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

return BankAccountDto.fromJson(jsonDecode(response.body));
Expand Down
4 changes: 3 additions & 1 deletion lib/packages/service/dfx/dfx_blockchain_api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:realunit_wallet/packages/config/api_config.dart';
import 'package:realunit_wallet/packages/service/app_store.dart';
import 'package:realunit_wallet/packages/service/dfx/exceptions/api_exception.dart';

class DfxBlockchainApiService {
static const _balancesPath = 'v1/blockchain/balances';
Expand Down Expand Up @@ -31,7 +32,8 @@ class DfxBlockchainApiService {
);

if (response.statusCode != 200 && response.statusCode != 201) {
throw Exception('Failed to get balances: ${response.statusCode}');
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

final json = jsonDecode(response.body) as Map<String, dynamic>;
Expand Down
7 changes: 5 additions & 2 deletions lib/packages/service/dfx/dfx_brokerbot_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:realunit_wallet/packages/config/api_config.dart';
import 'package:realunit_wallet/packages/service/app_store.dart';
import 'package:realunit_wallet/packages/service/dfx/exceptions/api_exception.dart';
import 'package:realunit_wallet/packages/service/dfx/models/brokerbot/dfx_buy_price_dto.dart';
import 'package:realunit_wallet/packages/service/dfx/models/brokerbot/dfx_buy_shares_dto.dart';
import 'package:realunit_wallet/packages/service/dfx/models/brokerbot/dfx_sell_price_dto.dart';
Expand Down Expand Up @@ -78,7 +79,8 @@ class DfxBrokerbotService {
);

if (res.statusCode != 200) {
throw Exception('SellPrice request failed: ${res.body}');
final errorJson = jsonDecode(res.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: res.statusCode);
}

return BrokerbotSellPriceDto.fromJson(jsonDecode(res.body));
Expand All @@ -102,7 +104,8 @@ class DfxBrokerbotService {
);

if (res.statusCode != 200) {
throw Exception('SellShares request failed: ${res.body}');
final errorJson = jsonDecode(res.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: res.statusCode);
}

return BrokerbotSellSharesDto.fromJson(jsonDecode(res.body));
Expand Down
5 changes: 3 additions & 2 deletions lib/packages/service/dfx/dfx_faucet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:realunit_wallet/packages/config/api_config.dart';
import 'package:realunit_wallet/packages/service/app_store.dart';
import 'package:realunit_wallet/packages/service/dfx/exceptions/api_exception.dart';
import 'package:realunit_wallet/packages/service/dfx/models/faucet/faucet_response_dto.dart';

class DfxFaucetService {
Expand All @@ -28,7 +29,7 @@ class DfxFaucetService {
return FaucetResponseDto.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
}

final body = jsonDecode(response.body) as Map<String, dynamic>;
throw Exception(body['message'] ?? 'Faucet request failed');
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}
}
20 changes: 10 additions & 10 deletions lib/packages/service/dfx/dfx_kyc_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

final json = jsonDecode(response.body) as Map<String, dynamic>;
Expand All @@ -73,7 +73,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

final json = jsonDecode(response.body);
Expand All @@ -96,7 +96,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

final json = jsonDecode(response.body);
Expand All @@ -120,7 +120,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

final json = jsonDecode(response.body);
Expand All @@ -142,7 +142,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}
}

Expand All @@ -164,7 +164,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}
return;
}
Expand All @@ -185,7 +185,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}
return;
}
Expand All @@ -207,7 +207,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}
return;
}
Expand All @@ -231,7 +231,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

final json = jsonDecode(response.body) as Map<String, dynamic>;
Expand All @@ -257,7 +257,7 @@ class DfxKycService extends DFXAuthService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}
}
}
41 changes: 23 additions & 18 deletions lib/packages/service/dfx/dfx_support_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:realunit_wallet/packages/config/api_config.dart';
import 'package:realunit_wallet/packages/service/dfx/dfx_auth_service.dart';
import 'package:realunit_wallet/packages/service/dfx/exceptions/api_exception.dart';
import 'package:realunit_wallet/packages/service/dfx/models/support/dto/support_issue_dto.dart';
import 'package:realunit_wallet/packages/service/dfx/models/support/support_issue_reason.dart';
import 'package:realunit_wallet/packages/service/dfx/models/support/support_issue_type.dart';
Expand Down Expand Up @@ -29,12 +30,13 @@ class DfxSupportService extends DFXAuthService {
},
);

if (response.statusCode == 200) {
final List<dynamic> jsonList = jsonDecode(response.body) as List<dynamic>;
return jsonList.map((e) => SupportIssueDto.fromJson(e as Map<String, dynamic>)).toList();
} else {
throw Exception('Failed to load tickets: ${response.statusCode}');
if (response.statusCode != 200) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

final List<dynamic> jsonList = jsonDecode(response.body) as List<dynamic>;
return jsonList.map((e) => SupportIssueDto.fromJson(e as Map<String, dynamic>)).toList();
}

Future<SupportIssueDto> getTicket(String uid) async {
Expand All @@ -49,13 +51,14 @@ class DfxSupportService extends DFXAuthService {
},
);

if (response.statusCode == 200) {
return SupportIssueDto.fromJson(
jsonDecode(response.body) as Map<String, dynamic>,
);
} else {
throw Exception('Failed to load ticket: ${response.statusCode}');
if (response.statusCode != 200) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

return SupportIssueDto.fromJson(
jsonDecode(response.body) as Map<String, dynamic>,
);
}

Future<SupportIssueDto> createTicket({
Expand Down Expand Up @@ -83,13 +86,14 @@ class DfxSupportService extends DFXAuthService {
body: body,
);

if (response.statusCode == 201) {
return SupportIssueDto.fromJson(
jsonDecode(response.body) as Map<String, dynamic>,
);
} else {
throw Exception('Failed to create ticket: ${response.statusCode}');
if (response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

return SupportIssueDto.fromJson(
jsonDecode(response.body) as Map<String, dynamic>,
);
}

Future<void> sendMessage(String ticketUid, String message) async {
Expand All @@ -108,7 +112,8 @@ class DfxSupportService extends DFXAuthService {
);

if (response.statusCode != 201) {
throw Exception('Failed to send message: ${response.statusCode}');
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}
}
}
8 changes: 4 additions & 4 deletions lib/packages/service/dfx/exceptions/api_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ class ApiException implements Exception {
required this.message,
});

factory ApiException.fromJson(Map<String, dynamic> json) {
factory ApiException.fromJson(Map<String, dynamic> json, {int? httpStatusCode}) {
final code = json['code'] as String?;

switch (code) {
case 'KYC_LEVEL_REQUIRED':
return KycLevelRequiredException.fromJson(json);
return KycLevelRequiredException.fromJson(json, httpStatusCode: httpStatusCode);
case 'REGISTRATION_REQUIRED':
return RegistrationRequiredException.fromJson(json);
return RegistrationRequiredException.fromJson(json, httpStatusCode: httpStatusCode);
default:
final message = json['message'];
return ApiException(
statusCode: json['statusCode'] as int,
statusCode: json['statusCode'] as int? ?? httpStatusCode,
code: code ?? 'UNKNOWN',
message: message is List ? message.join(', ') : message?.toString() ?? 'Unknown error',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import 'package:realunit_wallet/packages/service/dfx/exceptions/api_exception.da

class RegistrationRequiredException extends ApiException {
const RegistrationRequiredException({
super.statusCode,
required super.code,
required super.message,
});

factory RegistrationRequiredException.fromJson(Map<String, dynamic> json) {
factory RegistrationRequiredException.fromJson(Map<String, dynamic> json, {int? httpStatusCode}) {
return RegistrationRequiredException(
statusCode: json['statusCode'] as int? ?? httpStatusCode,
code: json['code'] as String,
message: json['message'] as String,
);
Expand All @@ -22,14 +24,16 @@ class KycLevelRequiredException extends ApiException {
final int currentLevel;

const KycLevelRequiredException({
super.statusCode,
required super.code,
required super.message,
required this.requiredLevel,
required this.currentLevel,
});

factory KycLevelRequiredException.fromJson(Map<String, dynamic> json) {
factory KycLevelRequiredException.fromJson(Map<String, dynamic> json, {int? httpStatusCode}) {
return KycLevelRequiredException(
statusCode: json['statusCode'] as int? ?? httpStatusCode,
code: json['code'] as String,
message: json['message'] as String,
requiredLevel: json['requiredLevel'] as int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ class RealUnitBuyPaymentInfoService {
);
} else if (response.statusCode == 403) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
} else {
throw Exception('Unexpected status code: ${response.statusCode}');
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}
}

Expand All @@ -72,7 +73,7 @@ class RealUnitBuyPaymentInfoService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

final json = jsonDecode(response.body) as Map<String, dynamic>;
Expand Down
6 changes: 3 additions & 3 deletions lib/packages/service/dfx/real_unit_pdf_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RealUnitPdfService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

return PdfDto.fromJson(jsonDecode(response.body));
Expand All @@ -71,7 +71,7 @@ class RealUnitPdfService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

return PdfDto.fromJson(jsonDecode(response.body));
Expand All @@ -92,7 +92,7 @@ class RealUnitPdfService {

if (response.statusCode != 200 && response.statusCode != 201) {
final errorJson = jsonDecode(response.body) as Map<String, dynamic>;
throw ApiException.fromJson(errorJson);
throw ApiException.fromJson(errorJson, httpStatusCode: response.statusCode);
}

return PdfDto.fromJson(jsonDecode(response.body));
Expand Down
Loading
Loading