mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
Move URI generation to helper
This commit is contained in:
@@ -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<void> 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<void> 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<String, String> 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<void> 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: <String, String>{
|
||||
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
|
||||
},
|
||||
|
||||
@@ -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<String, dynamic> 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<Map<String, dynamic>> fetch([String urlPath]) async {
|
||||
Future<Map<String, dynamic>> 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<Map<String, dynamic>> post(Map<String, dynamic> data, String urlPath) async {
|
||||
Future<Map<String, dynamic>> post(Map<String, dynamic> 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<Map<String, dynamic>> patch(Map<String, dynamic> data, String urlPath) async {
|
||||
Future<Map<String, dynamic>> patch(Map<String, dynamic> data, Uri uri) async {
|
||||
final response = await client.patch(
|
||||
urlPath,
|
||||
uri,
|
||||
headers: {
|
||||
HttpHeaders.authorizationHeader: 'Token ${auth.token}',
|
||||
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
|
||||
|
||||
40
lib/providers/helpers.dart
Normal file
40
lib/providers/helpers.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/// Helper function to make a URL.
|
||||
makeUri(String serverUrl, String path, [int id, String objectMethod, Map<String, dynamic> 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;
|
||||
}
|
||||
65
pubspec.lock
65
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:
|
||||
|
||||
11
pubspec.yaml
11
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
|
||||
|
||||
@@ -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'}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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: <String, String>{
|
||||
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',
|
||||
|
||||
Reference in New Issue
Block a user