mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 05:26:47 +00:00
Better handling of HttpClientRequest
This commit is contained in:
parent
fa58cca333
commit
f5e1f25dd0
@ -5,6 +5,7 @@
|
||||
---
|
||||
|
||||
- Fixes certificate issues connecting to HTTPs server
|
||||
- Fixes some app crash bugs
|
||||
- Bug fixes for various API calls
|
||||
- UI cleanup
|
||||
|
||||
|
117
lib/api.dart
117
lib/api.dart
@ -209,9 +209,6 @@ class InvenTreeAPI {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
print("Response from server: ${response}");
|
||||
|
||||
// We expect certain response from the server
|
||||
if (!response.containsKey("server") || !response.containsKey("version") || !response.containsKey("instance")) {
|
||||
|
||||
@ -395,7 +392,37 @@ class InvenTreeAPI {
|
||||
|
||||
var client = createClient(true);
|
||||
|
||||
HttpClientRequest request = await client.patchUrl(Uri.parse(_url));
|
||||
// Open a connection to the server
|
||||
HttpClientRequest request = await client.patchUrl(Uri.parse(_url))
|
||||
.timeout(Duration(seconds: 10))
|
||||
.catchError((error) {
|
||||
print("PATCH request return error");
|
||||
print("URL: ${_url}");
|
||||
print("Error: ${error.toString()}");
|
||||
|
||||
var ctx = OneContext().context;
|
||||
|
||||
if (error is SocketException) {
|
||||
showServerError(
|
||||
I18N.of(ctx).connectionRefused,
|
||||
error.toString(),
|
||||
);
|
||||
} else if (error is TimeoutException) {
|
||||
showTimeoutError(ctx);
|
||||
} else {
|
||||
showServerError(
|
||||
I18N.of(ctx).serverError,
|
||||
error.toString()
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
// Request could not be made
|
||||
if (request = null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var data = json.encode(body);
|
||||
|
||||
@ -485,7 +512,36 @@ class InvenTreeAPI {
|
||||
|
||||
var client = createClient(true);
|
||||
|
||||
HttpClientRequest request = await client.postUrl(Uri.parse(_url));
|
||||
// Open a connection to the server
|
||||
HttpClientRequest request = await client.postUrl(Uri.parse(_url))
|
||||
.timeout(Duration(seconds: 10))
|
||||
.catchError((error) {
|
||||
print("POST request returned error");
|
||||
print("URL: ${_url}");
|
||||
print("Error: ${error.toString()}");
|
||||
|
||||
var ctx = OneContext().context;
|
||||
|
||||
if (error is SocketException) {
|
||||
showServerError(
|
||||
I18N.of(ctx).connectionRefused,
|
||||
error.toString()
|
||||
);
|
||||
} else if (error is TimeoutException) {
|
||||
showTimeoutError(ctx);
|
||||
} else {
|
||||
showServerError(
|
||||
I18N.of(ctx).serverError,
|
||||
error.toString()
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var data = json.encode(body);
|
||||
|
||||
@ -607,18 +663,11 @@ class InvenTreeAPI {
|
||||
_url = _url.substring(0, _url.length - 1);
|
||||
}
|
||||
|
||||
print("GET: " + _url);
|
||||
|
||||
var client = createClient(true);
|
||||
|
||||
HttpClientRequest request = await client.getUrl(Uri.parse(_url));
|
||||
|
||||
// Set headers
|
||||
request.headers.set(HttpHeaders.contentTypeHeader, 'application/json');
|
||||
request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader());
|
||||
|
||||
HttpClientResponse response = await request.close()
|
||||
.timeout(Duration(seconds: 30))
|
||||
// Open a connection
|
||||
HttpClientRequest request = await client.getUrl(Uri.parse(_url))
|
||||
.timeout(Duration(seconds: 10))
|
||||
.catchError((error) {
|
||||
print("GET request returned error");
|
||||
print("URL: ${_url}");
|
||||
@ -643,6 +692,44 @@ class InvenTreeAPI {
|
||||
return null;
|
||||
});
|
||||
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Set connection headers
|
||||
request.headers.set(HttpHeaders.contentTypeHeader, 'application/json');
|
||||
request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader());
|
||||
|
||||
print("Attempting connection");
|
||||
|
||||
HttpClientResponse response = await request.close()
|
||||
.timeout(Duration(seconds: 10))
|
||||
.catchError((error) {
|
||||
print("GET request returned error");
|
||||
print("URL: ${_url}");
|
||||
print("Error: ${error.toString()}");
|
||||
|
||||
var ctx = OneContext().context;
|
||||
|
||||
if (error is SocketException) {
|
||||
showServerError(
|
||||
I18N.of(ctx).connectionRefused,
|
||||
error.toString()
|
||||
);
|
||||
} else if (error is TimeoutException) {
|
||||
showTimeoutError(ctx);
|
||||
} else {
|
||||
showServerError(
|
||||
I18N.of(ctx).serverError,
|
||||
error.toString()
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
print("got here");
|
||||
|
||||
// A null response means something has gone wrong...
|
||||
if (response == null) {
|
||||
print("null response from GET ${_url}");
|
||||
|
@ -177,19 +177,24 @@ Future<void> showStatusCodeError(int status, {int expected = 200}) async {
|
||||
|
||||
Future<void> showTimeoutError(BuildContext context) async {
|
||||
|
||||
await showServerError(I18N.of(context).timeout, I18N.of(context).noResponse);
|
||||
// Use OneContext as "sometimes" context is null here?
|
||||
var ctx = OneContext().context;
|
||||
|
||||
await showServerError(I18N.of(ctx).timeout, I18N.of(ctx).noResponse);
|
||||
}
|
||||
|
||||
void showFormDialog(String title, {String acceptText, String cancelText, GlobalKey<FormState> key, List<Widget> fields, List<Widget> actions, Function callback}) {
|
||||
|
||||
BuildContext dialogContext;
|
||||
|
||||
var ctx = OneContext().context;
|
||||
|
||||
if (acceptText == null) {
|
||||
acceptText = I18N.of(OneContext().context).save;
|
||||
acceptText = I18N.of(ctx).save;
|
||||
}
|
||||
|
||||
if (cancelText == null) {
|
||||
cancelText = I18N.of(OneContext().context).cancel;
|
||||
cancelText = I18N.of(ctx).cancel;
|
||||
}
|
||||
|
||||
// Undefined actions = OK + Cancel
|
||||
|
@ -7,7 +7,7 @@ description: InvenTree stock management
|
||||
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
|
||||
# Read more about iOS versioning at
|
||||
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
|
||||
version: 0.1.3+7
|
||||
version: 0.1.4+8
|
||||
|
||||
environment:
|
||||
sdk: ">=2.1.0 <3.0.0"
|
||||
|
Loading…
x
Reference in New Issue
Block a user