From 58ae2711697e81cc41a403b9b0f9742cb3fa622a Mon Sep 17 00:00:00 2001 From: Arthi-chaud Date: Thu, 3 Mar 2022 21:17:03 +0100 Subject: [PATCH] Mobile Client: fix breaking change --- api/services/anilist.json | 27 +++++++++-- api/services/spotify.json | 4 +- mobile/lib/src/aeris_api.dart | 46 ++++++++++++------- mobile/lib/src/models/action.dart | 16 +++---- mobile/lib/src/models/pipeline.dart | 3 +- mobile/lib/src/models/reaction.dart | 10 ++-- mobile/lib/src/models/service.dart | 4 +- mobile/lib/src/models/trigger.dart | 9 ++-- .../providers/action_catalogue_provider.dart | 9 +++- 9 files changed, 79 insertions(+), 49 deletions(-) diff --git a/api/services/anilist.json b/api/services/anilist.json index f76256b..099050b 100644 --- a/api/services/anilist.json +++ b/api/services/anilist.json @@ -3,20 +3,37 @@ "actions": [], "reactions": [ { - "name": "UpdateAbout", - "description": "Update the about you section.", + "name": "Anilist_UpdateAbout", + "label": { + "en": "Update 'About You' section", + "fr": "Changer la bio" + }, + "description": { + "en": "Update 'About You' section", + "fr": "Changer la bio" + }, "params": [ { "name": "about", "type": "string", - "description": "The description on your profile." + "description": { + "en": "The description of your profile", + "fr": "Le contenu de la bio" + } } ], "returns": [] }, { - "name": "ToggleFavourite", - "description": "Add or remove an anime fro your favorite.", + "name": "Anilist_ToggleFavourite", + "label": { + "en": "(Un)Favorite an anime", + "fr": "(Dé)Liker un animé" + }, + "description": { + "en": "Add or remove an anime from your favorite.", + "fr": "Ajouter ou modifier un animé des favoris" + }, "params": [ { "name": "animeID", diff --git a/api/services/spotify.json b/api/services/spotify.json index 99a4855..f1c8393 100644 --- a/api/services/spotify.json +++ b/api/services/spotify.json @@ -40,7 +40,7 @@ ], "reactions": [ { - "name": "PlayTrack", + "name": "Spotify_PlayTrack", "description": "Play a track", "params": [ { @@ -76,7 +76,7 @@ "returns": [] }, { - "name": "AddTrackToLibrary", + "name": "Spotify_AddTrackToLibrary", "description": "Add a track to library", "params": [ { diff --git a/mobile/lib/src/aeris_api.dart b/mobile/lib/src/aeris_api.dart index c9bf796..044e7ce 100644 --- a/mobile/lib/src/aeris_api.dart +++ b/mobile/lib/src/aeris_api.dart @@ -29,7 +29,7 @@ enum AerisAPIRequestType { get, post, put, delete } /// Call to interact with Aeris' Back end class AerisAPI { /// Get Connection state - bool _connected = false; //TODO Will be false later + bool _connected = false; bool get isConnected => _connected; /// JWT token used to request API @@ -37,8 +37,8 @@ class AerisAPI { late final String deepLinkRoute; - String _baseRoute = GetIt.I().getString('api') - ?? "http://10.0.2.2:8080"; + String _baseRoute = + GetIt.I().getString('api') ?? "http://10.0.2.2:8080"; String get baseRoute => _baseRoute; set baseRoute(value) => _baseRoute = value; @@ -141,8 +141,6 @@ class AerisAPI { Future editPipeline(Pipeline updatedPipeline) async { var res = await _requestAPI('/workflow/${updatedPipeline.id}', AerisAPIRequestType.put, updatedPipeline.toJSON()); - print(res.body); - print(res.statusCode); return res.ok; } @@ -150,7 +148,6 @@ class AerisAPI { Future> getPipelines() async { var res = await _requestAPI('/workflows', AerisAPIRequestType.get, null); if (res.ok == false) return []; - print(res.body); final List body = jsonDecode(res.body); return body.map((e) => Pipeline.fromJSON(Map.from(e))).toList(); @@ -162,7 +159,8 @@ class AerisAPI { await _requestAPI('/auth/services', AerisAPIRequestType.get, null); if (!res.ok) return []; return (jsonDecode(res.body) as List) - .map((e) => Service.factory(e.toString())).toList(); + .map((e) => Service.factory(e.toString())) + .toList(); } /// Disconnects the user from the service @@ -182,7 +180,8 @@ class AerisAPI { } List getActionsFor(Service service, aeris.Action action) { - final catalogue = Aeris.materialKey.currentContext?.read(); + final catalogue = + Aeris.materialKey.currentContext?.read(); if (action is Trigger) { return catalogue!.triggerTemplates[service]!; } @@ -198,7 +197,7 @@ class AerisAPI { Future _requestAPI( String route, AerisAPIRequestType requestType, Object? body) async { final Map header = { - 'Content-type' : 'application/json', + 'Content-type': 'application/json', 'Accept': 'application/json', }; if (_connected) { @@ -208,27 +207,40 @@ class AerisAPI { try { switch (requestType) { case AerisAPIRequestType.delete: - return await http.delete(_encoreUri(route), - body: jsonEncode(body), headers: header).timeout(duration, + return await http + .delete(_encoreUri(route), + body: jsonEncode(body), headers: header) + .timeout( + duration, onTimeout: () { return http.Response('Error', 408); - },); + }, + ); case AerisAPIRequestType.get: return await http.get(_encoreUri(route), headers: header).timeout( duration, onTimeout: () { return http.Response('Error', 408); - },); + }, + ); case AerisAPIRequestType.post: - return await http.post(_encoreUri(route), body: jsonEncode(body), headers: header).timeout(duration, + return await http + .post(_encoreUri(route), body: jsonEncode(body), headers: header) + .timeout( + duration, onTimeout: () { return http.Response('Error', 408); - },); + }, + ); case AerisAPIRequestType.put: - return await http.put(_encoreUri(route), body: jsonEncode(body), headers: header).timeout(duration, + return await http + .put(_encoreUri(route), body: jsonEncode(body), headers: header) + .timeout( + duration, onTimeout: () { return http.Response('Error', 408); - },); + }, + ); } } catch (e) { return http.Response('{}', 400); diff --git a/mobile/lib/src/models/action.dart b/mobile/lib/src/models/action.dart index 3789633..0b90e15 100644 --- a/mobile/lib/src/models/action.dart +++ b/mobile/lib/src/models/action.dart @@ -2,7 +2,6 @@ import 'package:aeris/src/models/action_parameter.dart'; import 'package:flutter/widgets.dart'; import 'package:aeris/src/models/service.dart'; import 'package:recase/recase.dart'; -import 'package:tuple/tuple.dart'; ///Base class for reactions and trigger abstract class Action { @@ -25,16 +24,15 @@ abstract class Action { this.description, this.parameters = const []}); - static Tuple2 parseServiceAndName(String rType) { - var snake = ReCase(rType).snakeCase.split('_'); + static Service parseServiceInName(String rType) { + var snake = rType.split('_'); var service = snake.removeAt(0); - return Tuple2(Service.factory(service), - ReCase(snake.join('_')).titleCase); + return Service.factory(service); } - static String getType(Service service, String aName) { - String serviceName = ReCase(service.name).pascalCase; - String actionName = ReCase(aName).pascalCase; - return "$serviceName$actionName"; + String displayName() { + var words = name.split('_'); + words.removeAt(0); + return ReCase(words.join()).titleCase; } } diff --git a/mobile/lib/src/models/pipeline.dart b/mobile/lib/src/models/pipeline.dart index 2706fde..8783726 100644 --- a/mobile/lib/src/models/pipeline.dart +++ b/mobile/lib/src/models/pipeline.dart @@ -1,4 +1,3 @@ -import 'package:aeris/src/models/action.dart' as aeris_action; import 'package:flutter/material.dart'; import 'package:aeris/src/models/reaction.dart'; import 'package:aeris/src/models/trigger.dart'; @@ -51,7 +50,7 @@ class Pipeline { "action": { "id": id, "name": name, - "pType": aeris_action.Action.getType(trigger.service, trigger.name), + "pType": trigger.name, "pParams": { for (var e in trigger.parameters) e.name : e.value }, ///Serialize "enabled": enabled, "lastTrigger": trigger.last?.toIso8601String(), diff --git a/mobile/lib/src/models/reaction.dart b/mobile/lib/src/models/reaction.dart index 949a930..5351609 100644 --- a/mobile/lib/src/models/reaction.dart +++ b/mobile/lib/src/models/reaction.dart @@ -4,7 +4,6 @@ import 'package:aeris/src/models/action.dart' as aeris_action; import 'package:aeris/src/models/action_parameter.dart'; import 'package:flutter/widgets.dart'; import 'package:aeris/src/models/service.dart'; -import 'package:tuple/tuple.dart'; ///Object representation of a reaction class Reaction extends aeris_action.Action { @@ -21,17 +20,16 @@ class Reaction extends aeris_action.Action { static Reaction fromJSON(Object reaction) { var reactionJSON = reaction as Map; - Tuple2 service = aeris_action.Action.parseServiceAndName( - reactionJSON['rType'] as String); + var service = aeris_action.Action.parseServiceInName(reactionJSON['rType'] as String); return Reaction( - service: service.item1, - name: service.item2, + service: service, + name: reactionJSON['rType'] as String, parameters: ActionParameter.fromJSON((reactionJSON['rParams'] as Map))); } /// Serialize Reaction to JSON Object toJSON() => { - "rType": aeris_action.Action.getType(service, name), + "rType": name, "rParams": { for (var e in parameters) e.name : e.value } }; diff --git a/mobile/lib/src/models/service.dart b/mobile/lib/src/models/service.dart index eb0383f..b3b0041 100644 --- a/mobile/lib/src/models/service.dart +++ b/mobile/lib/src/models/service.dart @@ -35,7 +35,7 @@ class Service { logoUrl = "https://www.presse-citron.net/app/uploads/2020/06/spotify-une-.jpg"; const Service.anilist() - : name = "AniList", + : name = "Anilist", url = "https://anilist.co", logoUrl = "https://anilist.co/img/icons/android-chrome-512x512.png"; @@ -50,7 +50,7 @@ class Service { logoUrl = "https://f.hellowork.com/blogdumoderateur/2019/11/twitter-logo-1200x1200.jpg"; const Service.github() - : name = "GitHub", + : name = "Github", url = "https://github.com/", logoUrl = "https://avatars.githubusercontent.com/u/9919?s=280&v=4"; const Service.youtube() diff --git a/mobile/lib/src/models/trigger.dart b/mobile/lib/src/models/trigger.dart index 6d990e8..5c2ef16 100644 --- a/mobile/lib/src/models/trigger.dart +++ b/mobile/lib/src/models/trigger.dart @@ -6,7 +6,6 @@ import 'package:aeris/main.dart'; import 'package:aeris/src/models/service.dart'; import 'package:aeris/src/models/action.dart' as aeris_action; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -import 'package:tuple/tuple.dart'; ///Object representation of a pipeline trigger class Trigger extends aeris_action.Action { @@ -23,16 +22,16 @@ class Trigger extends aeris_action.Action { /// Unserialize static Trigger fromJSON(Object action) { var triggerJSON = action as Map; - Tuple2 service = - aeris_action.Action.parseServiceAndName(triggerJSON['pType'] as String); + Service service = + aeris_action.Action.parseServiceInName(triggerJSON['pType'] as String); var lastTriggerField = action['lastTrigger']; DateTime? last = lastTriggerField == null ? null : DateTime.parse(lastTriggerField as String); return Trigger( - service: service.item1, - name: service.item2, + service: service, + name: triggerJSON['pType'] as String, last: last, parameters: ActionParameter.fromJSON((triggerJSON['pParams'] as Map)) ); diff --git a/mobile/lib/src/providers/action_catalogue_provider.dart b/mobile/lib/src/providers/action_catalogue_provider.dart index fb7d53a..5290d6d 100644 --- a/mobile/lib/src/providers/action_catalogue_provider.dart +++ b/mobile/lib/src/providers/action_catalogue_provider.dart @@ -14,12 +14,19 @@ class ActionCatalogueProvider extends ChangeNotifier { Map> get reactionTemplates => _reactionTemplates; + String removeServiceFromAName(String aName) { + var words = aName.split('_'); + words.removeAt(0); + return words.join(); + } + void reloadCatalogue() { + _triggerTemplates.clear(); + _reactionTemplates.clear(); Service.all().forEach((element) { _triggerTemplates.putIfAbsent(element, () => []); _reactionTemplates.putIfAbsent(element, () => []); }); - notifyListeners(); GetIt.I().getAbout().then((about) { if (about.isEmpty || about == null) return; final services = (about['server'] as Map)['services'] as List;