2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-07-11 08:04:14 +00:00

Connection now works properly! yay!

Nested asyncs are hard ya'll
This commit is contained in:
Oliver Walters
2020-03-31 23:35:23 +11:00
parent 83fc4b407d
commit 0ee1dedd45

View File

@ -20,6 +20,22 @@ class InvenTreeAPI {
static const _URL_GET_TOKEN = "user/token/"; static const _URL_GET_TOKEN = "user/token/";
static const _URL_GET_VERSION = ""; static const _URL_GET_VERSION = "";
// Base URL for InvenTree API
String _base_url = "";
String _username = "";
String _password = "";
// Authentication token (initially empty, must be requested)
String _token = "";
// Connection status flag - set once connection has been validated
bool _connected = false;
bool get connected {
return _connected && _base_url.isNotEmpty && _token.isNotEmpty;
}
// Ensure we only ever create a single instance of the API class // Ensure we only ever create a single instance of the API class
static final InvenTreeAPI _api = new InvenTreeAPI._internal(); static final InvenTreeAPI _api = new InvenTreeAPI._internal();
@ -27,11 +43,23 @@ class InvenTreeAPI {
InvenTreeAPI._internal(); InvenTreeAPI._internal();
void connect(String address, String username, String password) async { void disconnected() {
_connected = false;
// Clear token
_token = "";
}
Future<bool> connect(String address, String username, String password) async {
/* Address is the base address for the InvenTree server,
* e.g. http://127.0.0.1:8000
*/
address = address.trim(); address = address.trim();
username = username.trim(); username = username.trim();
// Ensure we are pointing to the correct endpoint
if (!address.endsWith("api/") || !address.endsWith("api")) { if (!address.endsWith("api/") || !address.endsWith("api")) {
address = path.join(address, "api"); address = path.join(address, "api");
} }
@ -55,83 +83,46 @@ class InvenTreeAPI {
print("Connecting to " + address + " -> " + username + ":" + password); print("Connecting to " + address + " -> " + username + ":" + password);
await _testConnection(); // TODO - Add connection timeout
}
bool _connected = false;
// Base URL for InvenTree API
String _base_url = "http://127.0.0.1:8000/api/";
String _username = "";
String _password = "";
// Authentication token (initially empty, must be requested)
String _token = "";
// Construct an API URL
String _makeUrl(String url) {
if (url.startsWith('/')) {
url = url.substring(1);
}
return path.join(_base_url, url); var response = await get("").catchError((error) {
} print("Error connecting to server:");
print(error);
return false;
});
bool _hasToken() { if (response.statusCode != 200) {
return _token.isNotEmpty; print("Invalid status code: " + response.statusCode.toString());
return false;
} }
// Request the raw /api/ endpoint to see if there is an InvenTree server listening var data = json.decode(response.body);
Future<void> _testConnection() async {
print("Testing connection to server");
await get("").then((http.Response response) {
final data = json.decode(response.body); print("Response from server: $data");
// We expect certain response from the server // We expect certain response from the server
if (!data.containsKey("server") || !data.containsKey("version")) { if (!data.containsKey("server") || !data.containsKey("version")) {
print("Incorrect keys in server response"); print("Incorrect keys in server response");
return false;
} }
print("Server: " + data["server"]); print("Server: " + data["server"]);
print("Version: " + data["version"]); print("Version: " + data["version"]);
// Ok, server is good. Request token! // Request token from the server
_getToken();
}).catchError((error) {
print("Error trying connection");
print(error);
});
}
// Request an API token from the server.
// A valid username/password combination must be provided
Future<String> _getToken() async {
print("Requesting API token from server");
if (_token.isNotEmpty) { if (_token.isNotEmpty) {
print("Discarding old token - " + _token); print("Discarding old token - $_token");
} }
// Clear out the token
_token = ""; _token = "";
var response = post(_URL_GET_TOKEN, response = await post(_URL_GET_TOKEN, body: {"username": _username, "password": _password}).catchError((error) {
body: { print("Error requesting token:");
"username": _username, print(error);
"password": _password, return false;
}); });
response.then((http.Response response) {
if (response.statusCode != 200) { if (response.statusCode != 200) {
print("Invalid status code: " + response.statusCode.toString()); print("Invalid status code: " + response.statusCode.toString());
return false; return false;
@ -140,20 +131,29 @@ class InvenTreeAPI {
if (!data.containsKey("token")) { if (!data.containsKey("token")) {
print("No token provided in response"); print("No token provided in response");
return ""; return false;
} }
// Return the received token // Return the received token
String token = data["token"]; _token = data["token"];
print("Received token: " + token); print("Received token - $_token");
return token; _connected = true;
return true;
};
} }
}).catchError((error) {
print("Error retrieving token:"); // Construct an API URL
print(error); String _makeUrl(String url) {
return "";
}); if (url.startsWith('/')) {
url = url.substring(1);
}
return path.join(_base_url, url);
} }
// Perform a PATCH request // Perform a PATCH request
@ -188,12 +188,10 @@ class InvenTreeAPI {
print("POST: " + _url); print("POST: " + _url);
final response = await http.post(_url, return http.post(_url,
headers: _headers, headers: _headers,
body: _body, body: _body,
); );
return response;
} }
// Perform a GET request // Perform a GET request