[GH-ISSUE #324] Update webapp example #103

Closed
opened 2026-02-27 20:23:08 +03:00 by kerem · 10 comments
Owner

Originally created by @marioortizmanero on GitHub (Jun 11, 2022).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/324

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.

Originally created by @marioortizmanero on GitHub (Jun 11, 2022). Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/324 **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.
Author
Owner

@ramsayleung commented on GitHub (Jun 13, 2022):

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.

<!-- gh-comment-id:1153379860 --> @ramsayleung commented on GitHub (Jun 13, 2022): 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.
Author
Owner

@marioortizmanero commented on GitHub (Jun 13, 2022):

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?

<!-- gh-comment-id:1153875033 --> @marioortizmanero commented on GitHub (Jun 13, 2022): > 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?
Author
Owner

@ramsayleung commented on GitHub (Jun 13, 2022):

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.

<!-- gh-comment-id:1154017372 --> @ramsayleung commented on GitHub (Jun 13, 2022): > 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.
Author
Owner

@graves501 commented on GitHub (Sep 15, 2022):

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?

<!-- gh-comment-id:1248645367 --> @graves501 commented on GitHub (Sep 15, 2022): 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... ```rust #[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?
Author
Owner

@ramsayleung commented on GitHub (Sep 16, 2022):

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/

<!-- gh-comment-id:1249414291 --> @ramsayleung commented on GitHub (Sep 16, 2022): 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/
Author
Owner

@graves501 commented on GitHub (Sep 19, 2022):

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.

<!-- gh-comment-id:1251436164 --> @graves501 commented on GitHub (Sep 19, 2022): 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.
Author
Owner

@ramsayleung commented on GitHub (Sep 20, 2022):

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.

<!-- gh-comment-id:1252526210 --> @ramsayleung commented on GitHub (Sep 20, 2022): 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: ```rust // 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.
Author
Owner

@graves501 commented on GitHub (Sep 21, 2022):

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

<!-- gh-comment-id:1253538067 --> @graves501 commented on GitHub (Sep 21, 2022): Thanks for pointing out the scope issue! It works for me now!
Author
Owner

@marioortizmanero commented on GitHub (Nov 8, 2022):

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.

<!-- gh-comment-id:1307812863 --> @marioortizmanero commented on GitHub (Nov 8, 2022): 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.
Author
Owner

@github-actions[bot] commented on GitHub (Jun 26, 2023):

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

<!-- gh-comment-id:1606513368 --> @github-actions[bot] commented on GitHub (Jun 26, 2023): Message to comment on stale issues. If none provided, will not mark issues stale
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/rspotify#103
No description provided.