Refactor makeUrl

The object ID is an integer and there is now an objectMethod for special object
endpoints.
This commit is contained in:
Roland Geider
2021-01-22 23:11:52 +01:00
parent 12632955df
commit 62926ae549
4 changed files with 32 additions and 12 deletions

View File

@@ -37,13 +37,17 @@ class WgerBaseProvider {
}
/// Helper function to make a URL.
makeUrl(String path, {String id, Map<String, dynamic> query}) {
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);
pathList.add(id.toString());
}
if (objectMethod != null) {
pathList.add(objectMethod);
}
final uri = Uri(
scheme: uriServer.scheme,
host: uriServer.host,
@@ -118,7 +122,7 @@ class WgerBaseProvider {
/// DELETEs an existing object
Future<Response> deleteRequest(String url, int id) async {
final deleteUrl = makeUrl(url, id: id.toString());
final deleteUrl = makeUrl(url, id: id);
final response = await client.delete(
deleteUrl,

View File

@@ -89,7 +89,7 @@ class Nutrition extends WgerBaseProvider with ChangeNotifier {
Future<NutritionalPlan> fetchAndSetPlan(int planId) async {
//fetchAndSet
final data = await fetch(makeUrl(_nutritionalPlansInfoPath, id: planId.toString()));
final data = await fetch(makeUrl(_nutritionalPlansInfoPath, id: planId));
final plan = NutritionalPlan.fromJson(data);
await fetchAndSetLogs(plan);
@@ -103,7 +103,7 @@ class Nutrition extends WgerBaseProvider with ChangeNotifier {
}
Future<void> patchPlan(NutritionalPlan plan) async {
final data = await patch(plan.toJson(), makeUrl(_nutritionalPlansPath, id: plan.id.toString()));
final data = await patch(plan.toJson(), makeUrl(_nutritionalPlansPath, id: plan.id));
notifyListeners();
}
@@ -196,7 +196,7 @@ class Nutrition extends WgerBaseProvider with ChangeNotifier {
}
// Get ingredient from the server and save to cache
final data = await fetch(makeUrl(_ingredientPath, id: ingredientId.toString()));
final data = await fetch(makeUrl(_ingredientPath, id: ingredientId));
ingredient = Ingredient.fromJson(data);
_ingredients.add(ingredient);

View File

@@ -87,8 +87,11 @@ class WorkoutPlans extends WgerBaseProvider with ChangeNotifier {
}
Future<WorkoutPlan> fetchAndSetFullWorkout(int workoutId) async {
final data =
await fetch(makeUrl(_workoutPlansUrlPath, id: '$workoutId/canonical_representation'));
final data = await fetch(makeUrl(
_workoutPlansUrlPath,
id: workoutId,
objectMethod: 'canonical_representation',
));
try {
WorkoutPlan workout = _workoutPlans.firstWhere((element) => element.id == workoutId);
@@ -200,7 +203,8 @@ class WorkoutPlans extends WgerBaseProvider with ChangeNotifier {
final data = await fetch(
makeUrl(
_workoutPlansUrlPath,
id: '${workout.id.toString()}/log_data',
id: workout.id,
objectMethod: 'log_data',
query: {'id': exercise.id.toString()},
),
);

View File

@@ -31,16 +31,28 @@ void main() {
provider.makeUrl('endpoint'),
);
expect(
'https://localhost/api/v2/endpoint/id/',
provider.makeUrl('endpoint', id: 'id'),
'https://localhost/api/v2/endpoint/5/',
provider.makeUrl('endpoint', id: 5),
);
expect(
'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',
provider.makeUrl('endpoint', query: {'a': '2', 'b': 'c'}),
);
expect(
'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',
provider.makeUrl('endpoint', id: '42', query: {'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',
provider.makeUrl('endpoint', id: 42, objectMethod: 'log_data', query: {'a': '2', 'b': 'c'}),
);
});
});