Cleaner API code

This commit is contained in:
poka 2020-12-13 15:37:39 +01:00
parent a9d0f5a7a6
commit 2a24c15f17
3 changed files with 47 additions and 52 deletions

View File

@ -4,23 +4,11 @@ import 'package:gql_dio_link/gql_dio_link.dart';
import 'package:gql_exec/gql_exec.dart'; import 'package:gql_exec/gql_exec.dart';
import "package:gql_link/gql_link.dart"; import "package:gql_link/gql_link.dart";
// Configure node
const graphqlEndpoint = "https://g1.librelois.fr/gva"; const graphqlEndpoint = "https://g1.librelois.fr/gva";
const QcurrentUD = """{ // Build query
currentUd { Future buildQ(query) async {
amount
base
}
}""";
Future getBalance(String pubkey) async {
var qBalance = """{
balance(script: "$pubkey") {
amount
base
}
}""";
final client = dio.Dio(); final client = dio.Dio();
final Link link = DioLink( final Link link = DioLink(
graphqlEndpoint, graphqlEndpoint,
@ -29,17 +17,45 @@ Future getBalance(String pubkey) async {
final res = await link final res = await link
.request(Request( .request(Request(
operation: Operation(document: gqlLang.parseString(qBalance)), operation: Operation(document: gqlLang.parseString(query)),
)) ))
.first; .first;
final balance = res.data["balance"]["amount"] / 100; return res;
return balance;
} }
/* Requests functions */
// Get current UD
Future getUD() async {
const query = """{
currentUd {
amount
base
}
}""";
final result = await buildQ(query);
return result.data["currentUd"]["amount"];
}
// Get balance
Future getBalance(String pubkey) async {
var query = """{
balance(script: "$pubkey") {
amount
base
}
}""";
final result = await buildQ(query);
return result.data["balance"]["amount"] / 100;
}
// Get history
Future getHistory(String pubkey) async { Future getHistory(String pubkey) async {
print(pubkey); print(pubkey);
var qHistory = """{ var query = """{
transactionsHistory(pubkey: "$pubkey") { transactionsHistory(pubkey: "$pubkey") {
received { received {
writtenTime writtenTime
@ -50,18 +66,9 @@ Future getHistory(String pubkey) async {
} }
}"""; }""";
final client = dio.Dio(); final res = await buildQ(query);
final Link link = DioLink(
graphqlEndpoint,
client: client,
);
final res = await link
.request(Request(
operation: Operation(document: gqlLang.parseString(qHistory)),
))
.first;
// Parse history
var result = res.data["transactionsHistory"]["received"]; var result = res.data["transactionsHistory"]["received"];
String outPubkey; String outPubkey;
for (var bloc in result) { for (var bloc in result) {
@ -70,6 +77,5 @@ Future getHistory(String pubkey) async {
print(outPubkey); print(outPubkey);
} }
final history = outPubkey; return outPubkey;
return history;
} }

View File

@ -1,10 +0,0 @@
class TodoFetch {
static String fetchAll = """
query {
currentUd {
amount,
base
}
}
""";
}

View File

@ -18,13 +18,13 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
Uint8List bytes = Uint8List(0); Uint8List bytes = Uint8List(0);
TextEditingController _outputController; TextEditingController _outputPubkey;
TextEditingController _outputAmount; TextEditingController _outputAmount;
@override @override
initState() { initState() {
super.initState(); super.initState();
this._outputController = new TextEditingController(); this._outputPubkey = new TextEditingController();
this._outputAmount = new TextEditingController(); this._outputAmount = new TextEditingController();
} }
@ -43,7 +43,7 @@ class _MyAppState extends State<MyApp> {
children: <Widget>[ children: <Widget>[
SizedBox(height: 20), SizedBox(height: 20),
TextField( TextField(
controller: this._outputController, controller: this._outputPubkey,
maxLines: 2, maxLines: 2,
decoration: InputDecoration( decoration: InputDecoration(
prefixIcon: Icon(Icons.wrap_text), prefixIcon: Icon(Icons.wrap_text),
@ -120,14 +120,13 @@ class _MyAppState extends State<MyApp> {
print('nothing return.'); print('nothing return.');
} else { } else {
print("Debug: " + barcode); print("Debug: " + barcode);
this._outputController.text = ""; this._outputPubkey.text = "";
this._outputAmount.text = ""; this._outputAmount.text = "";
var myAmount = await getHistory(barcode.toString()); // final udValue = await getUD();
this._outputController.text = barcode; final myBalance = await getBalance(barcode.toString());
print(myAmount.toString()); this._outputPubkey.text = barcode;
this._outputAmount.text = myAmount.toString(); print(myBalance.toString());
this._outputAmount.text = myBalance.toString();
// _getAmount(barcode);
} }
} }