Skip to content

Commit 25672a0

Browse files
authored
hamed/add_authorize_class (deriv-com#36)
* hamed/add_authorize_class - add authorize class * hamed/add_base_model - add base model * hamed/minor_refactor - minor refactor * hamed/add_unit_test_to_authorize_class - add unit test to authorize class * hamed/add_date_time_helper - add date time helper * hamed/minor_refactor - minor refactor * hamed/minor_refactor - minor refactor * hamed/minor_refactor - minor refactor * hamed/minor_refactor - minor refactor * hamed/sort_imports - sort imports * hamed/fix_imports - fix imports * hamed/minor_refactor - minor refactor
1 parent 59a74fd commit 25672a0

12 files changed

+411
-4
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ lib/basic_api/generated/*.json
1616
# The .vscode folder contains launch configuration and tasks you configure in
1717
# VS Code which you may wish to be included in version control, so this line
1818
# is commented out by default.
19-
#.vscode/
19+
.vscode/
2020

2121
# Flutter/Dart/Pub related
2222
**/doc/api/

lib/api/authorize/account.dart

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'package:flutter_deriv_api/api/models/account_model.dart';
2+
import 'package:flutter_deriv_api/utils/helpers.dart';
3+
4+
/// Account class
5+
class Account extends AccountModel {
6+
/// Class constructor
7+
Account({
8+
String currency,
9+
DateTime excludedUntil,
10+
bool isDisabled,
11+
bool isVirtual,
12+
String landingCompanyName,
13+
String loginid,
14+
}) : super(
15+
currency: currency,
16+
excludedUntil: excludedUntil,
17+
isDisabled: isDisabled,
18+
isVirtual: isVirtual,
19+
landingCompanyName: landingCompanyName,
20+
loginid: loginid,
21+
);
22+
23+
/// Generate instance from json
24+
factory Account.fromJson(Map<String, dynamic> json) => Account(
25+
currency: json['currency'],
26+
excludedUntil: getDateTime(json['excluded_until']),
27+
isDisabled: getBool(json['is_disabled']),
28+
isVirtual: getBool(json['is_virtual']),
29+
landingCompanyName: json['landing_company_name'],
30+
loginid: json['loginid'],
31+
);
32+
33+
/// Generate copy of instance with given parameters
34+
Account copyWith({
35+
String currency,
36+
int excludedUntil,
37+
bool isDisabled,
38+
bool isVirtual,
39+
String landingCompanyName,
40+
String loginid,
41+
}) =>
42+
Account(
43+
currency: currency ?? this.currency,
44+
excludedUntil: excludedUntil ?? this.excludedUntil,
45+
isDisabled: isDisabled ?? this.isDisabled,
46+
isVirtual: isVirtual ?? this.isVirtual,
47+
landingCompanyName: landingCompanyName ?? this.landingCompanyName,
48+
loginid: loginid ?? this.loginid,
49+
);
50+
}

lib/api/authorize/authorize.dart

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import 'package:flutter_deriv_api/api/authorize/account.dart';
2+
import 'package:flutter_deriv_api/api/models/authorize_model.dart';
3+
import 'package:flutter_deriv_api/api/models/local_currency_model.dart';
4+
5+
/// Authorize class
6+
class Authorize extends AuthorizeModel {
7+
/// Class constructor
8+
Authorize({
9+
List<Account> accountList,
10+
double balance,
11+
String country,
12+
String currency,
13+
String email,
14+
String fullname,
15+
bool isVirtual,
16+
String landingCompanyFullname,
17+
String landingCompanyName,
18+
List<LocalCurrencyModel> localCurrencies,
19+
String loginid,
20+
List<String> scopes,
21+
List<String> upgradeableLandingCompanies,
22+
int userId,
23+
}) : super(
24+
accountList: accountList,
25+
balance: balance,
26+
country: country,
27+
currency: currency,
28+
email: email,
29+
fullname: fullname,
30+
isVirtual: isVirtual,
31+
landingCompanyFullname: landingCompanyFullname,
32+
landingCompanyName: landingCompanyName,
33+
localCurrencies: localCurrencies,
34+
loginid: loginid,
35+
scopes: scopes,
36+
upgradeableLandingCompanies: upgradeableLandingCompanies,
37+
userId: userId,
38+
);
39+
40+
/// Generate instance from json
41+
factory Authorize.fromJson(Map<String, dynamic> json) => Authorize(
42+
accountList: json['account_list'] == null
43+
? null
44+
: json['account_list']
45+
.map<Account>((dynamic item) => Account.fromJson(item))
46+
.toList(),
47+
balance: json['balance'].toDouble(),
48+
country: json['country'],
49+
currency: json['currency'],
50+
email: json['email'],
51+
fullname: json['fullname'],
52+
isVirtual: json['is_virtual'] == 1,
53+
landingCompanyFullname: json['landing_company_fullname'],
54+
landingCompanyName: json['landing_company_name'],
55+
localCurrencies: json['local_currencies'] == null
56+
? null
57+
: json['local_currencies']
58+
.entries
59+
.map<LocalCurrencyModel>(
60+
(dynamic entry) => LocalCurrencyModel.fromJson(
61+
<String, dynamic>{
62+
'key': entry.key,
63+
'values': entry.value,
64+
},
65+
),
66+
)
67+
.toList(),
68+
loginid: json['loginid'],
69+
scopes: json['scopes'] == null
70+
? null
71+
: json['scopes']
72+
.map<String>((dynamic item) => item.toString())
73+
.toList(),
74+
upgradeableLandingCompanies:
75+
json['upgradeable_landing_companies'] == null
76+
? null
77+
: json['upgradeable_landing_companies']
78+
.map<String>((dynamic item) => item.toString())
79+
.toList(),
80+
userId: json['user_id'],
81+
);
82+
83+
/// Generate copy of instance with given parameters
84+
Authorize copyWith({
85+
List<Account> accountList,
86+
double balance,
87+
String country,
88+
String currency,
89+
String email,
90+
String fullname,
91+
bool isVirtual,
92+
String landingCompanyFullname,
93+
String landingCompanyName,
94+
List<LocalCurrencyModel> localCurrencies,
95+
String loginid,
96+
List<String> scopes,
97+
List<String> upgradeableLandingCompanies,
98+
int userId,
99+
}) =>
100+
Authorize(
101+
accountList: accountList ?? this.accountList,
102+
balance: balance ?? this.balance,
103+
country: country ?? this.country,
104+
currency: currency ?? this.currency,
105+
email: email ?? this.email,
106+
fullname: fullname ?? this.fullname,
107+
isVirtual: isVirtual ?? this.isVirtual,
108+
landingCompanyFullname:
109+
landingCompanyFullname ?? this.landingCompanyFullname,
110+
landingCompanyName: landingCompanyName ?? this.landingCompanyName,
111+
localCurrencies: localCurrencies ?? this.localCurrencies,
112+
loginid: loginid ?? this.loginid,
113+
scopes: scopes ?? this.scopes,
114+
upgradeableLandingCompanies:
115+
upgradeableLandingCompanies ?? this.upgradeableLandingCompanies,
116+
userId: userId ?? this.userId,
117+
);
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:flutter_deriv_api/api/exceptions/api_base_exception.dart';
2+
3+
/// Authorize exception class
4+
class AuthorizeException extends APIBaseException {
5+
/// Class constructor
6+
AuthorizeException({String message}) : super(message: message);
7+
}

lib/api/exceptions/api_base_exception.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ class APIBaseException implements Exception {
77
final String message;
88

99
@override
10-
String toString() => '$runtimeType Exception: $message';
10+
String toString() => '$runtimeType: $message';
1111
}

lib/api/models/account_model.dart

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:flutter_deriv_api/api/models/base_model.dart';
2+
3+
/// Account model class
4+
abstract class AccountModel extends BaseModel {
5+
/// Class constructor
6+
AccountModel({
7+
this.currency,
8+
this.excludedUntil,
9+
this.isDisabled,
10+
this.isVirtual,
11+
this.landingCompanyName,
12+
this.loginid,
13+
});
14+
15+
/// Currency of specified account.
16+
final String currency;
17+
18+
/// Epoch of date till client has excluded him/herself from the website, only present if client is self excluded.
19+
final DateTime excludedUntil;
20+
21+
/// Boolean value: true or false, indicating whether the account is marked as disabled or not.
22+
final bool isDisabled;
23+
24+
/// Boolean value: true or false, indicating whether the account is a virtual-money account.
25+
final bool isVirtual;
26+
27+
/// Landing company shortcode the account belongs to.
28+
final String landingCompanyName;
29+
30+
/// The account ID of specified account.
31+
final String loginid;
32+
}

lib/api/models/authorize_model.dart

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import 'package:flutter_deriv_api/api/models/account_model.dart';
2+
import 'package:flutter_deriv_api/api/models/base_model.dart';
3+
import 'package:flutter_deriv_api/api/models/local_currency_model.dart';
4+
5+
/// Authorize model class
6+
abstract class AuthorizeModel extends BaseModel {
7+
/// Class constructor
8+
AuthorizeModel({
9+
this.accountList,
10+
this.balance,
11+
this.country,
12+
this.currency,
13+
this.email,
14+
this.fullname,
15+
this.isVirtual,
16+
this.landingCompanyFullname,
17+
this.landingCompanyName,
18+
this.localCurrencies,
19+
this.loginid,
20+
this.scopes,
21+
this.upgradeableLandingCompanies,
22+
this.userId,
23+
});
24+
25+
/// List of accounts for current user.
26+
final List<AccountModel> accountList;
27+
28+
/// Cash balance of the account.
29+
final double balance;
30+
31+
/// 2-letter country code (ISO standard).
32+
final String country;
33+
34+
/// Currency of the account.
35+
final String currency;
36+
37+
/// User email.
38+
final String email;
39+
40+
/// User's full name. Will be empty for virtual accounts.
41+
final String fullname;
42+
43+
/// Boolean value: 1 or 0, indicating whether the account is a virtual-money account.
44+
final bool isVirtual;
45+
46+
/// Landing company name the account belongs to.
47+
final String landingCompanyFullname;
48+
49+
/// Landing company shortcode the account belongs to.
50+
final String landingCompanyName;
51+
52+
/// Currencies in client's residence country
53+
final List<LocalCurrencyModel> localCurrencies;
54+
55+
/// The account ID that the token was issued for.
56+
final String loginid;
57+
58+
/// Scopes available to the token.
59+
final List<String> scopes;
60+
61+
/// List of landing company shortcodes the account can upgrade to.
62+
final List<String> upgradeableLandingCompanies;
63+
64+
/// The internal user ID for this account.
65+
final int userId;
66+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'package:flutter_deriv_api/api/models/base_model.dart';
2+
3+
/// Local currency model class
4+
class LocalCurrencyModel extends BaseModel {
5+
/// Class constructor
6+
LocalCurrencyModel({
7+
this.currencyCode,
8+
this.fractionalDigits,
9+
});
10+
11+
/// Generate instance from json
12+
factory LocalCurrencyModel.fromJson(Map<String, dynamic> json) =>
13+
LocalCurrencyModel(
14+
currencyCode: json['key'],
15+
fractionalDigits: json['values']['fractional_digits'],
16+
);
17+
18+
/// Currency code
19+
final String currencyCode;
20+
21+
/// Number of fractional digits.
22+
final int fractionalDigits;
23+
}

lib/state/connection/connection_bloc.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class ConnectionBloc extends Bloc<ConnectionEvent, ConnectionState> {
7474
} else if (event is Reconnect) {
7575
print('Reconnecting ws connection!');
7676

77-
// api.close should be always invoked before changing the state otherwise the onDone function which is passed to the run funciton will be invoked one more time.
77+
// api.close should be always invoked before changing the state otherwise the onDone function which is passed to the run function will be invoked one more time.
7878
if (state is Connected) {
7979
final Connected currentState = state;
8080
await currentState.api.close();

lib/utils/helpers.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'dart:math';
66
import 'package:flutter/material.dart';
77

88
/// Parses the [url] and gets the endpoint out of it
9-
String parseWSUrl(String url, {bool isAuthUrl = false}) {
9+
String parseWebSocketUrl(String url, {bool isAuthUrl = false}) {
1010
if (url == null) {
1111
return null;
1212
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const String authorizeModelMockData = '''
2+
{
3+
"authorize": {
4+
"account_list": [
5+
{
6+
"currency": "USD",
7+
"excluded_until": 1587486726,
8+
"is_disabled": 0,
9+
"is_virtual": 0,
10+
"landing_company_name": "svg",
11+
"loginid": "CR90000028"
12+
},
13+
{
14+
"currency": "USD",
15+
"excluded_until": 1587486726,
16+
"is_disabled": 0,
17+
"is_virtual": 1,
18+
"landing_company_name": "virtual",
19+
"loginid": "VRTC90000028"
20+
}
21+
],
22+
"balance": 10000,
23+
"country": "za",
24+
"currency": "USD",
25+
"email": "[email protected]",
26+
"fullname": "Ms QA script hamedSTX",
27+
"is_virtual": 0,
28+
"landing_company_fullname": "Binary (SVG) Ltd.",
29+
"landing_company_name": "svg",
30+
"local_currencies": {
31+
"ZAR": {
32+
"fractional_digits": 2
33+
},
34+
"USD": {
35+
"fractional_digits": 3
36+
}
37+
},
38+
"loginid": "CR90000028",
39+
"scopes": [
40+
"read",
41+
"trade",
42+
"payments",
43+
"admin"
44+
],
45+
"upgradeable_landing_companies": [
46+
"svg"
47+
],
48+
"user_id": 29
49+
},
50+
"echo_req": {
51+
"authorize": "<not shown>"
52+
},
53+
"msg_type": "authorize"
54+
}
55+
''';

0 commit comments

Comments
 (0)