Skip to content

Commit 1ed8edc

Browse files
committed
add graphql client
1 parent b7fe839 commit 1ed8edc

15 files changed

+699
-1
lines changed

.vscode/settings.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
{
2-
"cSpell.words": ["keypair", "polkadot", "sublist", "tfchain", "TFKV", "twoxx"]
2+
"cSpell.words": [
3+
"Ffarms",
4+
"keypair",
5+
"polkadot",
6+
"sublist",
7+
"tfchain",
8+
"TFKV",
9+
"twoxx"
10+
]
311
}

packages/graphql_client/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/

packages/graphql_client/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial version.

packages/graphql_client/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A sample command-line application with an entrypoint in `bin/`, library code
2+
in `lib/`, and example unit test in `test/`.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import 'package:graphql_client/graphql_client.dart';
2+
import 'package:graphql_client/models/contracts.dart';
3+
import 'package:graphql_client/models/farms.dart';
4+
import 'package:graphql_client/src/contracts.dart';
5+
6+
void main() async {
7+
final graphQLClient = GraphQLClient('https://graphql.dev.grid.tf/graphql');
8+
9+
await graphQLClient.farms
10+
.listFarmsByTwinId(ListFarmsByTwinIdOptions(twinId: 214));
11+
12+
// final contracts = await graphQLClient.contracts
13+
// .listContractsByTwinId(ListContractByTwinIdOptions(twinId: 5110));
14+
// var res = 0.0;
15+
16+
// final nameContracts = contracts.nameContracts;
17+
// print(nameContracts.length);
18+
// for (final x in nameContracts) {
19+
// print(x.contractID);
20+
// final consumption = await graphQLClient.contracts
21+
// .getConsumption(GetConsumptionOptions(contractID: x.contractID));
22+
// res += consumption;
23+
// }
24+
25+
// final nodeContracts = contracts.nodeContracts;
26+
// print(nodeContracts.length);
27+
// for (final x in nodeContracts) {
28+
// print(x.contractID);
29+
// final consumption = await graphQLClient.contracts
30+
// .getConsumption(GetConsumptionOptions(contractID: x.contractID));
31+
// res += consumption;
32+
// }
33+
// print("HEllo");
34+
// print(res * 24 * 30);
35+
// await graphQLClient.contracts
36+
// .getConsumption(GetConsumptionOptions(contractID: "54069"));
37+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library client;
2+
3+
import 'dart:convert';
4+
5+
import 'package:graphql_client/src/contracts.dart';
6+
import 'package:graphql_client/src/farms.dart';
7+
import 'package:http/http.dart' as http;
8+
9+
part 'src/client.dart';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
enum ContractStates {
2+
Created,
3+
Deleted,
4+
OutOfFunds,
5+
GracePeriod,
6+
}
7+
8+
extension ContractStatesExtension on ContractStates {
9+
String get value {
10+
switch (this) {
11+
case ContractStates.Created:
12+
return 'Created';
13+
case ContractStates.Deleted:
14+
return 'Deleted';
15+
case ContractStates.OutOfFunds:
16+
return 'OutOfFunds';
17+
case ContractStates.GracePeriod:
18+
return 'GracePeriod';
19+
default:
20+
throw Exception('Invalid ContractState');
21+
}
22+
}
23+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
enum DiscountLevel {
2+
None,
3+
Default,
4+
Bronze,
5+
Silver,
6+
Gold,
7+
}
8+
9+
DiscountLevel parseDiscountLevel(String level) {
10+
switch (level.toLowerCase()) {
11+
case 'None':
12+
return DiscountLevel.None;
13+
case 'Default':
14+
return DiscountLevel.Default;
15+
case 'Bronze':
16+
return DiscountLevel.Bronze;
17+
case 'Silver':
18+
return DiscountLevel.Silver;
19+
case 'Gold':
20+
return DiscountLevel.Gold;
21+
default:
22+
return DiscountLevel.None;
23+
}
24+
}
25+
26+
class GqlBaseContract {
27+
String? id;
28+
String? gridVersion;
29+
String contractID;
30+
int twinID;
31+
String state;
32+
String createdAt;
33+
int solutionProviderID;
34+
35+
GqlBaseContract({
36+
this.id,
37+
this.gridVersion,
38+
required this.contractID,
39+
required this.twinID,
40+
required this.state,
41+
required this.createdAt,
42+
required this.solutionProviderID,
43+
});
44+
}
45+
46+
class GqlNameContract extends GqlBaseContract {
47+
String name;
48+
49+
GqlNameContract({
50+
super.id,
51+
super.gridVersion,
52+
required super.contractID,
53+
required super.twinID,
54+
required super.state,
55+
required super.createdAt,
56+
required super.solutionProviderID,
57+
required this.name,
58+
});
59+
}
60+
61+
class GqlNodeContract extends GqlBaseContract {
62+
int nodeID;
63+
String deploymentData;
64+
String deploymentHash;
65+
int numberOfPublicIPs;
66+
ContractUsedResources? resourcesUsed;
67+
Map<String, String>? parsedDeploymentData;
68+
69+
GqlNodeContract({
70+
super.id,
71+
super.gridVersion,
72+
required super.contractID,
73+
required super.twinID,
74+
required super.state,
75+
required super.createdAt,
76+
required super.solutionProviderID,
77+
required this.nodeID,
78+
required this.deploymentData,
79+
required this.deploymentHash,
80+
required this.numberOfPublicIPs,
81+
this.resourcesUsed,
82+
this.parsedDeploymentData,
83+
});
84+
}
85+
86+
class GqlRentContract extends GqlBaseContract {
87+
int nodeID;
88+
89+
GqlRentContract({
90+
super.id,
91+
super.gridVersion,
92+
required super.contractID,
93+
required super.twinID,
94+
required super.state,
95+
required super.createdAt,
96+
required super.solutionProviderID,
97+
required this.nodeID,
98+
});
99+
}
100+
101+
class ContractUsedResources {
102+
GqlNodeContract contract;
103+
int hru;
104+
int sru;
105+
int cru;
106+
int mru;
107+
108+
ContractUsedResources({
109+
required this.contract,
110+
required this.hru,
111+
required this.sru,
112+
required this.cru,
113+
required this.mru,
114+
});
115+
}
116+
117+
class GqlContracts {
118+
List<GqlNameContract> nameContracts;
119+
List<GqlNodeContract> nodeContracts;
120+
List<GqlRentContract> rentContracts;
121+
122+
GqlContracts({
123+
required this.nameContracts,
124+
required this.nodeContracts,
125+
required this.rentContracts,
126+
});
127+
}
128+
129+
class ListContractByTwinIdOptions {
130+
int twinId;
131+
List<String>? stateList;
132+
String? type;
133+
String? projectName;
134+
int? nodeId;
135+
136+
ListContractByTwinIdOptions({
137+
required this.twinId,
138+
this.stateList,
139+
this.type,
140+
this.projectName,
141+
this.nodeId,
142+
});
143+
}
144+
145+
class GetConsumptionOptions {
146+
String contractID;
147+
148+
GetConsumptionOptions({
149+
required this.contractID,
150+
});
151+
}
152+
153+
class GqlContractBillReports {
154+
String id;
155+
int contractID;
156+
DiscountLevel discountLevel;
157+
String amountBilled;
158+
String timeStamp;
159+
160+
GqlContractBillReports({
161+
required this.id,
162+
required this.contractID,
163+
required this.discountLevel,
164+
required this.amountBilled,
165+
required this.timeStamp,
166+
});
167+
}
168+
169+
class GqlConsumption {
170+
GqlContracts contracts;
171+
List<GqlContractBillReports> contractBillReports;
172+
173+
GqlConsumption({
174+
required this.contracts,
175+
required this.contractBillReports,
176+
});
177+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class ListFarmsByTwinIdOptions {
2+
int twinId;
3+
4+
ListFarmsByTwinIdOptions({
5+
required this.twinId,
6+
});
7+
}
8+
9+
class FarmInfo {
10+
String? id;
11+
String? gridVersion;
12+
int farmID;
13+
String name;
14+
String certification;
15+
bool dedicatedFarm;
16+
int pricingPolicyID;
17+
String stellarAddress;
18+
int twinID;
19+
20+
FarmInfo({
21+
this.id,
22+
this.gridVersion,
23+
required this.farmID,
24+
required this.name,
25+
required this.certification,
26+
required this.dedicatedFarm,
27+
required this.pricingPolicyID,
28+
required this.stellarAddress,
29+
required this.twinID,
30+
});
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
part of '../graphql_client.dart';
2+
3+
class GraphQLClient {
4+
final String url;
5+
late final TFContracts contracts;
6+
late final TFFarms farms;
7+
8+
GraphQLClient(this.url) {
9+
contracts = TFContracts(this);
10+
farms = TFFarms(this);
11+
}
12+
13+
Future<int> getItemTotalCount(String itemName, [String options = ""]) async {
14+
final queryOptions = options.isNotEmpty ? options : "";
15+
final countBody =
16+
'query { items: ${itemName}Connection$queryOptions { count: totalCount } }';
17+
final response = await http.post(
18+
Uri.parse(url),
19+
headers: {'Content-Type': 'application/json'},
20+
body: json.encode({'query': countBody}),
21+
);
22+
23+
if (response.statusCode == 200) {
24+
final data = json.decode(response.body);
25+
return data['data']['items']['count'] as int;
26+
} else {
27+
throw Exception('Failed to fetch data: ${response.statusCode}');
28+
}
29+
}
30+
31+
Future<Map<String, dynamic>> query(String body,
32+
{Map<String, dynamic> variables = const {}}) async {
33+
final response = await http.post(
34+
Uri.parse(url),
35+
headers: {'Content-Type': 'application/json'},
36+
body: json.encode({'query': body, 'variables': variables}),
37+
);
38+
39+
if (response.statusCode == 200) {
40+
return json.decode(response.body);
41+
} else {
42+
print("ERROR: ${response.body}");
43+
throw Exception('Failed to fetch data: ${response.statusCode}');
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)