From 0ce2b6efcab160c2f7b002bf245abdfca87fd319 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 9 Mar 2021 11:42:44 +0100 Subject: [PATCH] Move URI generation to helper --- lib/providers/auth.dart | 13 ++++--- lib/providers/base_provider.dart | 33 ++++------------ lib/providers/helpers.dart | 40 ++++++++++++++++++++ pubspec.lock | 65 ++++++++------------------------ pubspec.yaml | 11 +++--- test/base_provider_test.dart | 14 +++---- test/weight_provider_test.dart | 6 +-- 7 files changed, 86 insertions(+), 96 deletions(-) create mode 100644 lib/providers/helpers.dart diff --git a/lib/providers/auth.dart b/lib/providers/auth.dart index 9902bd46..27e936cd 100644 --- a/lib/providers/auth.dart +++ b/lib/providers/auth.dart @@ -30,6 +30,8 @@ import 'package:package_info/package_info.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:wger/models/http_exception.dart'; +import 'helpers.dart'; + class Auth with ChangeNotifier { String token; String serverUrl; @@ -62,8 +64,7 @@ class Auth with ChangeNotifier { /// Server application version Future setServerVersion() async { - var url = '$serverUrl/api/v2/version/'; - final response = await http.get(url); + final response = await http.get(makeUri(serverUrl, 'version')); final responseData = json.decode(response.body); serverVersion = responseData; } @@ -76,7 +77,7 @@ class Auth with ChangeNotifier { /// Registers a new user Future register({String username, String password, String email, String serverUrl}) async { - var url = '$serverUrl/api/v2/register/'; + final uri = Uri.http(serverUrl, '/api/v2/register/'); Map metadata = Map(); // Read the api key from the manifest file @@ -93,7 +94,7 @@ class Auth with ChangeNotifier { data['email'] = email; } final response = await http.post( - url, + uri, headers: { HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8', HttpHeaders.authorizationHeader: "Token ${metadata['wger.api_key']}" @@ -114,12 +115,12 @@ class Auth with ChangeNotifier { /// Authenticates a user Future login(String username, String password, String serverUrl) async { - var url = '$serverUrl/api/v2/login/'; + final uri = Uri.http(serverUrl, '/api/v2/login/'); await logout(); try { final response = await http.post( - url, + uri, headers: { HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8', }, diff --git a/lib/providers/base_provider.dart b/lib/providers/base_provider.dart index 5c291b4e..e6f9c03e 100644 --- a/lib/providers/base_provider.dart +++ b/lib/providers/base_provider.dart @@ -23,6 +23,7 @@ import 'package:http/http.dart' as http; import 'package:http/http.dart'; import 'package:wger/models/http_exception.dart'; import 'package:wger/providers/auth.dart'; +import 'package:wger/providers/helpers.dart'; /// Base provider class. /// @@ -38,32 +39,14 @@ class WgerBaseProvider { /// Helper function to make a URL. makeUrl(String path, {int id, String objectMethod, Map query}) { - Uri uriServer = Uri.parse(auth.serverUrl); - - var pathList = ['api', 'v2', path]; - if (id != null) { - pathList.add(id.toString()); - } - if (objectMethod != null) { - pathList.add(objectMethod); - } - - final uri = Uri( - scheme: uriServer.scheme, - host: uriServer.host, - port: uriServer.port, - path: pathList.join('/') + '/', - queryParameters: query, - ); - - return uri.toString(); + return makeUri(auth.serverUrl, path, id, objectMethod, query); } /// Fetch and retrieve the overview list of objects, returns the JSON parsed response - Future> fetch([String urlPath]) async { + Future> fetch([Uri uri]) async { // Send the request final response = await client.get( - urlPath, + uri, headers: { HttpHeaders.authorizationHeader: 'Token ${auth.token}', HttpHeaders.userAgentHeader: 'wger Workout Manager App', @@ -81,9 +64,9 @@ class WgerBaseProvider { } /// POSTs a new object - Future> post(Map data, String urlPath) async { + Future> post(Map data, Uri uri) async { final response = await client.post( - urlPath, + uri, headers: { HttpHeaders.authorizationHeader: 'Token ${auth.token}', HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8', @@ -101,9 +84,9 @@ class WgerBaseProvider { } /// PATCHEs an existing object - Future> patch(Map data, String urlPath) async { + Future> patch(Map data, Uri uri) async { final response = await client.patch( - urlPath, + uri, headers: { HttpHeaders.authorizationHeader: 'Token ${auth.token}', HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8', diff --git a/lib/providers/helpers.dart b/lib/providers/helpers.dart new file mode 100644 index 00000000..362da4f8 --- /dev/null +++ b/lib/providers/helpers.dart @@ -0,0 +1,40 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (C) 2020 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wger Workout Manager is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/// Helper function to make a URL. +makeUri(String serverUrl, String path, [int id, String objectMethod, Map query]) { + Uri uriServer = Uri.parse(serverUrl); + + var pathList = ['api', 'v2', path]; + if (id != null) { + pathList.add(id.toString()); + } + if (objectMethod != null) { + pathList.add(objectMethod); + } + + final uri = Uri( + scheme: uriServer.scheme, + host: uriServer.host, + port: uriServer.port, + path: pathList.join('/') + '/', + queryParameters: query, + ); + + return uri; +} diff --git a/pubspec.lock b/pubspec.lock index 220c1343..7d7cb840 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,14 +7,14 @@ packages: name: _fe_analyzer_shared url: "https://pub.dartlang.org" source: hosted - version: "7.0.0" + version: "14.0.0" analyzer: dependency: transitive description: name: analyzer url: "https://pub.dartlang.org" source: hosted - version: "0.39.17" + version: "0.41.2" android_metadata: dependency: "direct main" description: @@ -56,14 +56,14 @@ packages: name: build url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.6.2" build_config: dependency: transitive description: name: build_config url: "https://pub.dartlang.org" source: hosted - version: "0.4.2" + version: "0.4.6" build_daemon: dependency: transitive description: @@ -77,21 +77,21 @@ packages: name: build_resolvers url: "https://pub.dartlang.org" source: hosted - version: "1.3.11" + version: "1.5.3" build_runner: dependency: "direct dev" description: name: build_runner url: "https://pub.dartlang.org" source: hosted - version: "1.10.2" + version: "1.11.5" build_runner_core: dependency: transitive description: name: build_runner_core url: "https://pub.dartlang.org" source: hosted - version: "6.0.1" + version: "6.1.10" built_collection: dependency: transitive description: @@ -183,13 +183,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.5" - csslib: - dependency: transitive - description: - name: csslib - url: "https://pub.dartlang.org" - source: hosted - version: "0.16.2" cupertino_icons: dependency: "direct main" description: @@ -203,7 +196,7 @@ packages: name: dart_style url: "https://pub.dartlang.org" source: hosted - version: "1.3.6" + version: "1.3.12" fake_async: dependency: transitive description: @@ -300,7 +293,7 @@ packages: name: glob url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "2.0.0" graphs: dependency: transitive description: @@ -308,20 +301,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.2.0" - html: - dependency: transitive - description: - name: html - url: "https://pub.dartlang.org" - source: hosted - version: "0.14.0+4" http: dependency: "direct main" description: name: http url: "https://pub.dartlang.org" source: hosted - version: "0.12.2" + version: "0.13.0" http_multi_server: dependency: transitive description: @@ -335,7 +321,7 @@ packages: name: http_parser url: "https://pub.dartlang.org" source: hosted - version: "3.1.4" + version: "4.0.0" image: dependency: transitive description: @@ -350,13 +336,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.17.0" - intl_translation: - dependency: "direct dev" - description: - name: intl_translation - url: "https://pub.dartlang.org" - source: hosted - version: "0.17.10+1" io: dependency: transitive description: @@ -372,14 +351,14 @@ packages: source: hosted version: "0.6.3" json_annotation: - dependency: transitive + dependency: "direct main" description: name: json_annotation url: "https://pub.dartlang.org" source: hosted version: "3.1.1" json_serializable: - dependency: "direct main" + dependency: "direct dev" description: name: json_serializable url: "https://pub.dartlang.org" @@ -427,20 +406,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.0" - node_interop: - dependency: transitive - description: - name: node_interop - url: "https://pub.dartlang.org" - source: hosted - version: "1.2.1" - node_io: - dependency: transitive - description: - name: node_io - url: "https://pub.dartlang.org" - source: hosted - version: "1.1.1" package_config: dependency: transitive description: @@ -594,7 +559,7 @@ packages: name: shelf url: "https://pub.dartlang.org" source: hosted - version: "0.7.9" + version: "1.0.0" shelf_web_socket: dependency: transitive description: @@ -620,7 +585,7 @@ packages: name: source_gen url: "https://pub.dartlang.org" source: hosted - version: "0.9.7+1" + version: "0.9.10+3" source_span: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 58f72c56..ed620555 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,7 +18,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 0.1.0+5 environment: - sdk: ">=2.7.0 <3.0.0" + sdk: ">=2.11.0 <3.0.0" dependencies: flutter: @@ -28,11 +28,11 @@ dependencies: provider: ^5.0.0 intl: ^0.17.0 - http: ^0.12.2 + http: ^0.13.0 shared_preferences: ^2.0.3 flutter_calendar_carousel: ^1.5.3 cupertino_icons: ^1.0.0 - json_serializable: ^3.5.1 + json_annotation: ^3.1.1 url_launcher: ^6.0.2 charts_flutter: ^0.9.0 flutter_typeahead: ^2.0.0 @@ -44,12 +44,13 @@ dev_dependencies: flutter_test: sdk: flutter flutter_launcher_icons: ^0.8.1 - intl_translation: ^0.17.10+1 + #intl_translation: ^0.17.10+1 mockito: ^4.1.3 + json_serializable: ^3.5.1 # Note that there are incompatibilities when using a higher version. Both build # runner and intl_translation depend on different versions of analyzer - build_runner: ^1.10.2 + build_runner: ^1.11.5 dependency_overrides: intl: ^0.17.0 diff --git a/test/base_provider_test.dart b/test/base_provider_test.dart index 65fdda15..ec3842a5 100644 --- a/test/base_provider_test.dart +++ b/test/base_provider_test.dart @@ -27,31 +27,31 @@ void main() { final WgerBaseProvider provider = WgerBaseProvider(testAuth); expect( - 'https://localhost/api/v2/endpoint/', + Uri.https('localhost', '/api/v2/endpoint/'), provider.makeUrl('endpoint'), ); expect( - 'https://localhost/api/v2/endpoint/5/', + Uri.https('localhost', '/api/v2/endpoint/5/'), provider.makeUrl('endpoint', id: 5), ); expect( - 'https://localhost/api/v2/endpoint/5/log_data/', + Uri.https('localhost', '/api/v2/endpoint/5/log_data/'), provider.makeUrl('endpoint', id: 5, objectMethod: 'log_data'), ); expect( - 'https://localhost/api/v2/endpoint/?a=2&b=c', + Uri.https('localhost', '/api/v2/endpoint/', {'a': '2', 'b': 'c'}), provider.makeUrl('endpoint', query: {'a': '2', 'b': 'c'}), ); expect( - 'https://localhost/api/v2/endpoint/log_data/?a=2&b=c', + Uri.https('localhost', '/api/v2/endpoint/log_data/', {'a': '2', 'b': 'c'}), provider.makeUrl('endpoint', objectMethod: 'log_data', query: {'a': '2', 'b': 'c'}), ); expect( - 'https://localhost/api/v2/endpoint/42/?a=2&b=c', + Uri.https('localhost', '/api/v2/endpoint/42/', {'a': '2', 'b': 'c'}), provider.makeUrl('endpoint', id: 42, query: {'a': '2', 'b': 'c'}), ); expect( - 'https://localhost/api/v2/endpoint/42/log_data/?a=2&b=c', + Uri.https('localhost', '/api/v2/endpoint/42/log_data/', {'a': '2', 'b': 'c'}), provider.makeUrl('endpoint', id: 42, objectMethod: 'log_data', query: {'a': '2', 'b': 'c'}), ); }); diff --git a/test/weight_provider_test.dart b/test/weight_provider_test.dart index 9d3cfca3..ea890050 100644 --- a/test/weight_provider_test.dart +++ b/test/weight_provider_test.dart @@ -33,7 +33,7 @@ void main() { // Mock the server response when(client.get( - 'https://localhost/api/v2/weightentry/?ordering=-date', + Uri.https('localhost', 'api/v2/weightentry/', {'ordering': '-date'}), headers: { HttpHeaders.authorizationHeader: 'Token ${testAuth.token}', HttpHeaders.userAgentHeader: 'wger Workout Manager App', @@ -57,7 +57,7 @@ void main() { final client = MockClient(); // Mock the server response - when(client.post('https://localhost/api/v2/weightentry/', + when(client.post(Uri.https('localhost', 'api/v2/weightentry/'), headers: { HttpHeaders.authorizationHeader: 'Token ${testAuth.token}', HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8', @@ -83,7 +83,7 @@ void main() { // Mock the server response when(client.delete( - 'https://localhost/api/v2/weightentry/4/', + Uri.https('localhost', 'api/v2/weightentry/4/'), headers: { HttpHeaders.authorizationHeader: 'Token ${testAuth.token}', HttpHeaders.userAgentHeader: 'wger Workout Manager App',