From fa1256d85826f37e08e75e349225ef9ed5763a7d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 19 Apr 2021 22:15:47 +1000 Subject: [PATCH] Updated PATCH request --- lib/api.dart | 87 ++++++++++++++++++++++++++++------------ lib/inventree/model.dart | 29 +++----------- lib/l10n | 2 +- 3 files changed, 67 insertions(+), 51 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index 4cfc5978..011751ce 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -348,9 +348,8 @@ class InvenTreeAPI { // Perform a PATCH request - Future patch(String url, {Map body}) async { + Future patch(String url, {Map body, int expectedStatusCode=200}) async { var _url = makeApiUrl(url); - var _headers = defaultHeaders(); var _body = Map(); // Copy across provided data @@ -358,10 +357,59 @@ class InvenTreeAPI { print("PATCH: " + _url); - return http.patch(_url, - headers: _headers, - body: _body, - ); + var client = createClient(true); + + HttpClientRequest request = await client.patchUrl(Uri.parse(_url)); + + var data = json.encode(body); + + // Set headers + request.headers.set('Accept', 'application/json'); + request.headers.set('Content-type', 'application/json'); + request.headers.set('Content-Length', data.length.toString()); + request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); + + request.add(utf8.encode(data)); + + HttpClientResponse response = await request.close() + .timeout(Duration(seconds: 30)) + .catchError((error) { + print("PATCH 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 (response == null) { + print("null response from PATCH ${_url}"); + return null; + } + + if (response.statusCode != expectedStatusCode) { + showStatusCodeError(response.statusCode); + return null; + } } /* @@ -394,7 +442,6 @@ class InvenTreeAPI { */ Future post(String url, {Map body, int expectedStatusCode=201}) async { var _url = makeApiUrl(url); - var _headers = jsonHeaders(); print("POST: ${_url} -> ${body.toString()}"); @@ -409,13 +456,7 @@ class InvenTreeAPI { request.headers.set('Accept', 'application/json'); request.headers.set('Content-type', 'application/json'); request.headers.set('Content-Length', data.length.toString()); - - if (profile != null) { - request.headers.set( - HttpHeaders.authorizationHeader, - _authorizationHeader(profile.username, profile.password) - ); - } + request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); // Add JSON data to the request request.add(utf8.encode(data)); @@ -509,7 +550,6 @@ class InvenTreeAPI { */ Future get(String url, {Map params, int expectedStatusCode=200}) async { var _url = makeApiUrl(url); - var _headers = defaultHeaders(); print("GET: ${_url}"); @@ -535,12 +575,7 @@ class InvenTreeAPI { // Set headers request.headers.set(HttpHeaders.contentTypeHeader, 'application/json'); - if (profile != null) { - request.headers.set( - HttpHeaders.authorizationHeader, - _authorizationHeader(profile.username, profile.password) - ); - } + request.headers.set(HttpHeaders.authorizationHeader, _authorizationHeader()); HttpClientResponse response = await request.close() .timeout(Duration(seconds: 30)) @@ -604,9 +639,7 @@ class InvenTreeAPI { Map defaultHeaders() { var headers = Map(); - if (profile != null) { - headers[HttpHeaders.authorizationHeader] = _authorizationHeader(profile.username, profile.password); - } + headers[HttpHeaders.authorizationHeader] = _authorizationHeader(); return headers; } @@ -617,11 +650,13 @@ class InvenTreeAPI { return headers; } - String _authorizationHeader(String username, String password) { + String _authorizationHeader() { if (_token.isNotEmpty) { return "Token $_token"; + } else if (profile != null) { + return "Basic " + base64Encode(utf8.encode('${profile.username}:${profile.password}')); } else { - return "Basic " + base64Encode(utf8.encode('${username}:${password}')); + return ""; } } diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index f37c11d0..19204aa7 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -186,34 +186,15 @@ class InvenTreeModel { addr += "/"; } - var response = await api.patch(addr, body: values) - .timeout(Duration(seconds: 10)) - .catchError((e) { - - if (e is SocketException) { - showServerError( - I18N.of(context).connectionRefused, - e.toString() - ); - } else if (e is TimeoutException) { - showTimeoutError(context); - } else { - // Re-throw the error - throw e; - } - - return null; - }); + var response = await api.patch( + addr, + body: values, + expectedStatusCode: 200 + ); if (response == null) return false; - if (response.statusCode != 200) { - showStatusCodeError(response.statusCode); - return false; - } - return true; - } // Return the detail view for the associated pk diff --git a/lib/l10n b/lib/l10n index 2fe34df1..385c15b1 160000 --- a/lib/l10n +++ b/lib/l10n @@ -1 +1 @@ -Subproject commit 2fe34df1ddb2b5512003981dbdaf3f7b3dbf2ee9 +Subproject commit 385c15b1e3275140c1b8a4e59de9513a6241dd9b