Improved api usage, organised my code a bit

This commit is contained in:
Felix 2019-07-08 11:53:10 +01:00
parent b986b3e574
commit 6c9eb1c115
2 changed files with 59 additions and 37 deletions

View file

@ -0,0 +1,56 @@
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:local_spend/common/functions/get_token.dart';
class Organisation {
var id = 0;
var name = "";
var postcode = "";
var street_name = "";
var town = "";
}
List<Organisation> jsonToOrganisations(String json) {
Map decoded = jsonDecode(json);
print(decoded);
List<dynamic> validated = decoded['validated'];
// Map organisation = validated[0];
List<Map> organisations = new List<Map>();
validated.forEach((element) => organisations.add(element));
// print(organisations);
}
Future<List<Organisation>> findOrganisations(String search) async {
final url = "https://dev.peartrade.org/api/search";
var token;
await getToken().then((result) {
token = result;
});
Map<String, String> body = {
"search_name":search,
"session_key":token,
};
final response = await http.post (
url,
body: json.encode(body),
);
print(response.body);
if (response.statusCode == 200) {
//request successful
return jsonToOrganisations(response.body);
} else {
// not successful
return null;
}
}

View file

@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@ -10,9 +9,8 @@ import 'package:local_spend/common/widgets/basic_drawer.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:intl/intl.dart';
import 'package:http/http.dart' as http;
import 'package:local_spend/common/functions/get_token.dart';
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
import 'package:local_spend/common/apifunctions/find_organisations.dart';
const URL = "https://flutter.io/";
const demonstration = false;
@ -123,39 +121,6 @@ class ReceiptPageState extends State<ReceiptPage> {
return "false";
}
Future<String> findOrganisations(String search) async {
final url = "https://dev.peartrade.org/api/search";
var token;
await getToken().then((result) {
token = result;
});
Map<String, String> body = {
"search_name":search,
"session_key":token,
};
debugPrint(token.toString() + " " + json.encode(body).toString());
final response = await http.post (
url,
//{"search_name":"bo","session_key":"1C2AB5F0-A163-11E9-BE68-241789E69626"}
body: json.encode(body),
);
debugPrint(response.body);
debugPrint(response.statusCode.toString());
if (response.statusCode == 200) {
//request successful
return response.body;
} else {
return null;
}
}
String formatDate(String date) {
// return "";
// should be in format:
@ -334,6 +299,7 @@ class ReceiptPageState extends State<ReceiptPage> {
onSubmitted: (_) {
submitReceipt(_amountController.text,
_timeController.text, _orgController.text);
// TODO: make sure organisation is valid
// TODO: Add 'find organisation' button which displays a dialog to, well, find the organisation's address or manual entry
},
),