-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.dart
More file actions
54 lines (47 loc) · 1.57 KB
/
client.dart
File metadata and controls
54 lines (47 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
part of '../gridproxy_client.dart';
class GridProxyClient {
final String baseUrl;
late final Nodes nodes;
late final Contracts contracts;
late final Farms farms;
late final Stats stats;
late final Twins twins;
GridProxyClient(this.baseUrl) {
nodes = Nodes(this);
contracts = Contracts(this);
farms = Farms(this);
stats = Stats(this);
twins = Twins(this);
}
Future<dynamic> getRequest(
String path, Map<String, dynamic>? queryParameters) async {
try {
final Map<String, String> encodedQueryParams = {};
queryParameters?.forEach((key, value) {
if (value is List) {
encodedQueryParams[key] = value.map((e) => e.toString()).join(',');
} else if (value != null) {
encodedQueryParams[key] = value.toString();
}
});
final uri = Uri.parse(baseUrl);
final scheme = uri.scheme;
final host = uri.host;
final requestUri = encodedQueryParams.isNotEmpty
? (scheme == 'https'
? Uri.https(host, path, encodedQueryParams)
: Uri.http(host, path, encodedQueryParams))
: (scheme == 'https' ? Uri.https(host, path) : Uri.http(host, path));
final response = await http.get(requestUri);
if (response.statusCode == 200) {
final jsonData = json.decode(response.body);
return jsonData;
} else {
throw Exception(
'Failed to load data. Status: ${response.statusCode}, Body: ${response.body}');
}
} catch (e) {
throw Exception('Error during GET request to $path: $e');
}
}
}