From 59a2c004d98e7557fd24bfa3583709ce8ffe4aae Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 1 Mar 2021 12:43:38 +1100 Subject: [PATCH] API now requires minimum API version, not minimum server version - A lot simpler to implement and undestand! --- lib/api.dart | 56 ++++++++++++++-------------------------------------- pubspec.lock | 14 +++++++++++++ pubspec.yaml | 1 + 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index c3a28479..10964f21 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -26,42 +26,8 @@ import 'package:one_context/one_context.dart'; class InvenTreeAPI { - // Minimum supported InvenTree server version is - static const List MIN_SUPPORTED_VERSION = [0, 1, 5]; - - String get _requiredVersionString => "${MIN_SUPPORTED_VERSION[0]}.${MIN_SUPPORTED_VERSION[1]}.${MIN_SUPPORTED_VERSION[2]}"; - - bool _checkServerVersion(String version) { - - // Provided version string should be of the format "x.y.z [...]" - List versionSplit = version.split(' ').first.split('.'); - - // Extract the version number .. from the string - if (versionSplit.length != 3) { - return false; - } - - // Cast the server version to an explicit integer - int server_version_code = 0; - - print("server version: ${version}"); - - server_version_code += (int.tryParse(versionSplit[0]) ?? 0) * 100 * 100; - server_version_code += (int.tryParse(versionSplit[1]) ?? 0) * 100; - server_version_code += (int.tryParse(versionSplit[2])); - - print("server version code: ${server_version_code}"); - - int required_version_code = 0; - - required_version_code += MIN_SUPPORTED_VERSION[0] * 100 * 100; - required_version_code += MIN_SUPPORTED_VERSION[1] * 100; - required_version_code += MIN_SUPPORTED_VERSION[2]; - - print("required version code: ${required_version_code}"); - - return server_version_code >= required_version_code; - } + // Minimum required API version for server + static const _minApiVersion = 2; // Endpoint for requesting an API token static const _URL_GET_TOKEN = "user/token/"; @@ -252,11 +218,19 @@ class InvenTreeAPI { // Default API version is 1 if not provided _apiVersion = data['apiVersion'] as int ?? 1; - // Check that the remote server version is *new* enough - if (!_checkServerVersion(_version)) { + + if (_apiVersion < _minApiVersion) { + + BuildContext ctx = OneContext().context; + + String message = I18N.of(ctx).serverApiVersion + ": ${_apiVersion}"; + + message += "\n"; + message += I18N.of(ctx).serverApiRequired + ": ${_minApiVersion}"; + showServerError( I18N.of(OneContext().context).serverOld, - "\n\nServer Version: ${_version}\n\nRequired version: ${_requiredVersionString}" + message ); return false; @@ -384,6 +358,7 @@ class InvenTreeAPI { try { var data = json.decode(response.body); + if (data.containsKey('roles')) { // Save a local copy of the user roles roles = data['roles']; @@ -396,8 +371,7 @@ class InvenTreeAPI { } }); } - - + bool checkPermission(String role, String permission) { /* * Check if the user has the given role.permission assigned diff --git a/pubspec.lock b/pubspec.lock index 95d4f0dd..f9b801d4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -273,6 +273,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.6" + infinite_scroll_pagination: + dependency: "direct main" + description: + name: infinite_scroll_pagination + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0" intl: dependency: "direct main" description: @@ -488,6 +495,13 @@ packages: description: flutter source: sdk version: "0.0.99" + sliver_tools: + dependency: transitive + description: + name: sliver_tools + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.10+1" source_span: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 5ebee8db..ad44522e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: path_provider: ^1.6.27 # Local file storage sembast: ^2.4.9 # NoSQL data storage one_context: ^0.5.0 # Dialogs without requiring context + infinite_scroll_pagination: ^2.3.0 # Let the server do all the work! path: dev_dependencies: