2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-12 10:15:32 +00:00

Better use of async functions for API

- Use local_preferences for storing data
This commit is contained in:
Oliver Walters
2019-06-26 00:07:18 +10:00
parent 6e807cbed1
commit b71c665453
4 changed files with 141 additions and 31 deletions

View File

@ -8,6 +8,8 @@ import 'package:http/http.dart' as http;
class InvenTreeAPI {
static const _URL_GET_TOKEN = "user/token/";
// Ensure we only ever create a single instance of the API class
static final InvenTreeAPI _api = new InvenTreeAPI._internal();
@ -56,6 +58,11 @@ class InvenTreeAPI {
// Construct an API URL
String _makeUrl(String url) {
if (url.startsWith('/')) {
url = url.substring(1);
}
return path.join(_base_url, url);
}
@ -65,50 +72,113 @@ class InvenTreeAPI {
// Request the raw /api/ endpoing to see if there is an InvenTree server listening
void _tryConnection() async {
final response = get("");
print("Testing connection to server");
final response = await get("").then((http.Response response) {
print("response!");
print(response.body);
}).catchError((error) {
print("Error trying connection");
});
// TODO - Add timeout handler
}
// Request an API token from the server.
// A valid username/password combination must be provided
void _secureToken() async {
if (_token.isNotEmpty) {
print("Discarding old token - " + _token);
}
_token = "";
var _url = _makeUrl("user/token/");
final response = await http.post(_url,
var response = post(_URL_GET_TOKEN,
body: {
"username": _username,
"password": _password
});
"password": _password,
});
if (response.statusCode != 200) {
print("Invalid status code:" + String.fromCharCode(response.statusCode));
} else {
var _json = json.decode(response.body);
response.then((http.Response response) {
if (response.statusCode != 200) {
print("Invalid status code: " + response.statusCode.toString());
} else {
var _json = json.decode(response.body);
if (_json["token"] != null) {
_token = _json["token"];
print("Received token: " + _token);
if (_json["token"] != null) {
_token = _json["token"];
print("Received token: " + _token);
}
}
}
}).catchError((error) {
print("Error retrieving token");
});
}
Future<http.Response> get(String url) async {
var _url = _makeUrl(url);
final response = await http.get(_url,
headers: {
HttpHeaders.authorizationHeader: "Token: " + _token
}
);
// Perform a PATCH request
Future<http.Response> patch(String url, {Map<String, String> body}) async {
print("Making request to " + _url);
print(response.statusCode);
print(response.body);
var _url = _makeUrl(url);
var _headers = _defaultHeaders();
var _body = Map<String, String>();
// Copy across provided data
body.forEach((K, V) => _body[K] = V);
print("PATCH: " + _url);
final response = await http.patch(_url,
headers: _headers,
body: _body,
);
return response;
}
// Perform a POST request
Future<http.Response> post(String url, {Map<String, String> body}) async {
var _url = _makeUrl(url);
var _headers = _defaultHeaders();
var _body = Map<String, String>();
// Copy across provided data
body.forEach((K, V) => _body[K] = V);
print("POST: " + _url);
final response = await http.post(_url,
headers: _headers,
body: _body,
);
return response;
}
// Perform a GET request
Future<http.Response> get(String url) async {
var _url = _makeUrl(url);
var _headers = _defaultHeaders();
print("GET: " + _url);
final response = await http.get(_url,
headers: _headers,
);
return response;
}
Map<String, String> _defaultHeaders() {
var headers = Map<String, String>();
if (_token.isNotEmpty) {
headers[HttpHeaders.authorizationHeader] = "Token " + _token;
}
return headers;
}
}