From 316391d5fbd7390ff6d50f70a9b761ff6fd53ef6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 22 Jun 2021 21:24:14 +1000 Subject: [PATCH 1/2] Bump minimum required API version --- lib/api.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api.dart b/lib/api.dart index 6c097f6c..67f3b059 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -62,7 +62,7 @@ class InvenTreeFileService extends FileService { class InvenTreeAPI { // Minimum required API version for server - static const _minApiVersion = 3; + static const _minApiVersion = 5; // Endpoint for requesting an API token static const _URL_GET_TOKEN = "user/token/"; From 9e1305b1f0b9ca8304dd91fa5a6a5a409f33c7c3 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 23 Jun 2021 08:51:41 +1000 Subject: [PATCH 2/2] Allow upload of part images - Requires API v6 --- lib/api.dart | 15 ++++++++------- lib/inventree/part.dart | 22 ++++++++++++++++++++-- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index 67f3b059..1d062680 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -62,11 +62,10 @@ class InvenTreeFileService extends FileService { class InvenTreeAPI { // Minimum required API version for server - static const _minApiVersion = 5; + static const _minApiVersion = 6; // Endpoint for requesting an API token static const _URL_GET_TOKEN = "user/token/"; - static const _URL_GET_VERSION = ""; static const _URL_GET_ROLES = "user/roles/"; @@ -518,16 +517,18 @@ class InvenTreeAPI { * Upload a file to the given URL */ Future uploadFile(String url, File f, - {String name = "attachment", Map fields}) async { + {String name = "attachment", String method="POST", Map fields}) async { var _url = makeApiUrl(url); - var request = http.MultipartRequest('POST', Uri.parse(_url)); + var request = http.MultipartRequest(method, Uri.parse(_url)); request.headers.addAll(defaultHeaders()); - fields.forEach((String key, String value) { - request.fields[key] = value; - }); + if (fields != null) { + fields.forEach((String key, String value) { + request.fields[key] = value; + }); + } var _file = await http.MultipartFile.fromPath(name, f.path); diff --git a/lib/inventree/part.dart b/lib/inventree/part.dart index ea71051f..82c44a8b 100644 --- a/lib/inventree/part.dart +++ b/lib/inventree/part.dart @@ -301,8 +301,26 @@ class InvenTreePart extends InvenTreeModel { return img.isNotEmpty ? img : InvenTreeAPI.staticThumb; } - void uploadImage(File image) async { - // TODO + Future uploadImage(File image) async { + // Upload file against this part + final http.StreamedResponse response = await InvenTreeAPI().uploadFile( + url, + image, + method: 'PATCH', + name: 'image', + ); + + if (response == null) { + print("uploadImage returned null at '${url}'"); + return false; + } + + if (response.statusCode != 200) { + print("uploadImage returned ${response.statusCode} at '${url}'"); + return false; + } + + return true; } // Return the "starred" status of this part