rspotify icon indicating copy to clipboard operation
rspotify copied to clipboard

Update `webapp` example

Open marioortizmanero opened this issue 3 years ago • 8 comments

Is your feature request related to a problem? Please describe.

The webapp example could definitely be improved.

Describe the solution you'd like

  • Add token refreshing
  • Use the new API properly (at some point we do spotify.token = ..., we can surely avoid that).
  • Proper error handling? It has too many unwrap in my opinion.
  • Add string interpolation where possible
  • The Rocket framework isn't that active, unfortunately. It would surely be more helpful if it used Actix instead.
  • Include it in the CI somehow so that this doesn't happen anymore.

Describe alternatives you've considered

I've fixed a couple things in #305, but it was out of scope.

marioortizmanero avatar Jun 11 '22 14:06 marioortizmanero

I totally agree with your point, the webapp example is a little stale, we should refactor this example with the new API, to make it polished and robust.

Except for this point:

  • The Rocket framework isn't that active, unfortunately. It would surely be more helpful if it used Actix instead.

I am not sure if we could be beneficial from switching to Actix. Comparing the recent PR history of Rocket with Actix, I think there are roughly active.

And we should start to refactor webapp example after #305 merged, and honestly speaking, #305 goes a little far so that I need times to get involved in.

ramsayleung avatar Jun 13 '22 02:06 ramsayleung

The Rocket framework isn't that active, unfortunately. It would surely be more helpful if it used Actix instead.

Fair enough. I was worried because v0.5.0 hasn't been released yet, but it does seem like the repo is active.

And we should start to refactor webapp example after https://github.com/ramsayleung/rspotify/pull/305 merged, and honestly speaking, https://github.com/ramsayleung/rspotify/pull/305 goes a little far so that I need times to get involved in.

Sorry, I didn't quite understand that. What do you mean?

marioortizmanero avatar Jun 13 '22 12:06 marioortizmanero

Sorry, I didn't quite understand that. What do you mean?

Never mind, I just think the work of refactoring the webapp example should wait until #305 get merged.

ramsayleung avatar Jun 13 '22 14:06 ramsayleung

Since #305 already got merged, I guess the webapp is ready for some polish?

I tried out the webapp example and I noticed that I am not able to fetch the top artists...

#[get("/topartists")]
fn top_artists(cookies: Cookies) -> AppResponse {
    let mut spotify = init_spotify(&cookies);
    if !spotify.config.cache_path.exists() {
        return AppResponse::Redirect(Redirect::to("/"));
    }

    let token = spotify.read_token_cache(false).unwrap();
    spotify.token = Arc::new(Mutex::new(token));

    let top_artists = spotify
        .current_user_top_artists(Some(&TimeRange::LongTerm))
        .take(10)
        .filter_map(Result::ok)
        .collect::<Vec<_>>();

    if top_artists.is_empty() {
        return AppResponse::Redirect(Redirect::to("/"));
    }

    AppResponse::Json(json!(top_artists))
}

top_artists is always empty. I'm still new to Rust and especially new to this library. So I guess I'm missing something? Or is that a general issue?

graves501 avatar Sep 15 '22 21:09 graves501

Probably you could reproduce this problem with Spotify's Web Console, I guess it's something wrong with your data, e.g. your top artists.

https://developer.spotify.com/console/get-current-user-top-artists-and-tracks/

ramsayleung avatar Sep 16 '22 14:09 ramsayleung

Thanks for the hint! I successfully tried out the developer console, so my data seems to be fine.

I also tried spotify.current_user_top_artists(Some(&TimeRange::MediumTerm)) but the top_artists would still be empty. Another thing I tried is to use release version 0.11.0 of rspotify, but that also didn't salvage the problem.

graves501 avatar Sep 19 '22 19:09 graves501

I can reproduce this issue, the problem is that the default scopes in example are "user-read-currently-playing", "playlist-modify-private", the required scopes to get user's top artists are user-top-read.

https://github.com/ramsayleung/rspotify/blob/master/examples/webapp/src/main.rs#L90

You should add user-top-read to the scopes:

    // Please notice that protocol of redirect_uri, make sure it's http
    // (or https). It will fail if you mix them up.
    let oauth = OAuth {
        scopes: scopes!(
            "user-read-currently-playing",
            "playlist-modify-private",
            "user-top-read"
        ),
        redirect_uri: "http://localhost:8000/callback".to_owned(),
        ..Default::default()
    };

it works for me now.

ramsayleung avatar Sep 20 '22 15:09 ramsayleung

Thanks for pointing out the scope issue! It works for me now!

graves501 avatar Sep 21 '22 10:09 graves501

Yeah, I guess that @graves501 ran into that issue because we:

  1. Turn the Result into Option with Result::ok
  2. Filter out the None entries with filter_map

This is something we could improve as well; otherwise we are ignoring possibly important errors.

marioortizmanero avatar Nov 08 '22 20:11 marioortizmanero

Message to comment on stale issues. If none provided, will not mark issues stale

github-actions[bot] avatar Jun 26 '23 02:06 github-actions[bot]