From a77bfc6277743a1b065284339d7137298911eb10 Mon Sep 17 00:00:00 2001 From: kilowatt Date: Sat, 24 Oct 2020 00:00:37 +0300 Subject: [PATCH] Refresh CSRF token and retry API call --- .gitignore | 1 + lib/api/deezer.dart | 8 ++++++-- lib/ui/details_screens.dart | 7 ++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ccee263..599cfa6 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ android/local.properties .pub-cache/ .pub/ /build/ +.gradle # Web related lib/generated_plugin_registrant.dart diff --git a/lib/api/deezer.dart b/lib/api/deezer.dart index 00e0730..4054e75 100644 --- a/lib/api/deezer.dart +++ b/lib/api/deezer.dart @@ -47,6 +47,7 @@ class DeezerAPI { }); //Post http.Response res = await http.post(uri, headers: headers, body: jsonEncode(params)); + dynamic body = jsonDecode(res.body); //Grab SID if (method == 'deezer.getUserData') { for (String cookieHeader in res.headers['set-cookie'].split(';')) { @@ -55,8 +56,11 @@ class DeezerAPI { } } } - - return jsonDecode(res.body); + // In case of error "Invalid CSRF token" retrieve new one and retry the same call + if (body['error'].isNotEmpty && body['error'].containsKey('VALID_TOKEN_REQUIRED') && await rawAuthorize()) { + return callApi(method, params: params, gatewayInput: gatewayInput); + } + return body; } Future> callPublicApi(String path) async { diff --git a/lib/ui/details_screens.dart b/lib/ui/details_screens.dart index 3de835d..b85c3b6 100644 --- a/lib/ui/details_screens.dart +++ b/lib/ui/details_screens.dart @@ -752,14 +752,15 @@ class _PlaylistDetailsState extends State { if (playlist.tracks.length == 0) { //Get correct metadata deezerAPI.playlist(playlist.id) - .catchError((e) => setState(() => _error = true)) .then((Playlist p) { - if (p == null) return; setState(() { playlist = p; }); //Load tracks _load(); + }) + .catchError((e) { + setState(() => _error = true); }); } @@ -805,7 +806,7 @@ class _PlaylistDetailsState extends State { ), Container(height: 4.0), Text( - playlist.user.name, + playlist.user.name??'', overflow: TextOverflow.ellipsis, maxLines: 2, textAlign: TextAlign.center,