[GH-ISSUE #28] "from_token" is currently not a valid parameter #8

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

Originally created by @hrkfdn on GitHub (Nov 11, 2018).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/28

Hey,

thanks for your work on the library. I'm currently trying to search for tracks using rspotify. According to the documentation, from_token can be passed as a market parameter to search in the market tied to the token.

However, the search_* functions seem to require an Option parameter. There seems to be no Country enum representing from_token, unless I am missing something?

Originally created by @hrkfdn on GitHub (Nov 11, 2018). Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/28 Hey, thanks for your work on the library. I'm currently trying to search for tracks using rspotify. According to the documentation, `from_token` can be passed as a `market` parameter to search in the market tied to the token. However, the `search_*` functions seem to require an Option<Country> parameter. There seems to be no Country enum representing `from_token`, unless I am missing something?
kerem closed this issue 2026-02-27 20:22:35 +03:00
Author
Owner

@hrkfdn commented on GitHub (Nov 11, 2018):

Mhh, or is passing None equivalent to from_token?

<!-- gh-comment-id:437671276 --> @hrkfdn commented on GitHub (Nov 11, 2018): Mhh, or is passing `None` equivalent to `from_token`?
Author
Owner

@ramsayleung commented on GitHub (Nov 12, 2018):

Could you show which search_* function you are using to search for tracks, I am not sure which from_token you are talking about ?

<!-- gh-comment-id:437916152 --> @ramsayleung commented on GitHub (Nov 12, 2018): Could you show which `search_*` function you are using to search for tracks, I am not sure which `from_token` you are talking about ?
Author
Owner

@hrkfdn commented on GitHub (Nov 12, 2018):

For instance, search_track (https://github.com/samrayleung/rspotify/blob/master/src/spotify/client.rs#L420):
The documentation states the following for market:
market - An ISO 3166-1 alpha-2 country code or the string from_token.

Maybe I misread it? The way I interpreted it, is that market either gets an alpha-2 country code or the literal "from_token", but in the signature the type is declared as Option<Country>. So for me, it was unclear how to set the parameter to derive the market from the token.

<!-- gh-comment-id:437919457 --> @hrkfdn commented on GitHub (Nov 12, 2018): For instance, `search_track` (https://github.com/samrayleung/rspotify/blob/master/src/spotify/client.rs#L420): The documentation states the following for market: `market - An ISO 3166-1 alpha-2 country code or the string from_token.` Maybe I misread it? The way I interpreted it, is that `market` either gets an alpha-2 country code or the literal "from_token", but in the signature the type is declared as `Option<Country>`. So for me, it was unclear how to set the parameter to derive the market from the token.
Author
Owner

@ramsayleung commented on GitHub (Nov 13, 2018):

Perhaps you could check this example search_track, every function should have a relative example to demonstrate how to use them. If you have time to check senum.rs, you could find each country are define as a enum field github.com/samrayleung/rspotify@f8463a302f/src/spotify/senum.rs (L126), and they all have a relative representation(you call them as An ISO 3166-1 alpha-2 country code)
github.com/samrayleung/rspotify@f8463a302f/src/spotify/senum.rs (L615)

<!-- gh-comment-id:438100214 --> @ramsayleung commented on GitHub (Nov 13, 2018): Perhaps you could check this example [search_track](https://github.com/samrayleung/rspotify/blob/master/examples/search_track.rs), every function should have a relative [example](https://github.com/samrayleung/rspotify/tree/master/examples) to demonstrate how to use them. If you have time to check `senum.rs`, you could find each country are define as a enum field https://github.com/samrayleung/rspotify/blob/f8463a302f1df9393cf4e05a27252ffd2b3ff2c3/src/spotify/senum.rs#L126, and they all have a relative representation(you call them as `An ISO 3166-1 alpha-2 country code`) https://github.com/samrayleung/rspotify/blob/f8463a302f1df9393cf4e05a27252ffd2b3ff2c3/src/spotify/senum.rs#L615
Author
Owner

@hrkfdn commented on GitHub (Nov 15, 2018):

Yes, I know how to pass a Country enum, but it's not clear how to pass "from_token". The way it is described in the function documentation is not possible, as the "from_token" literal is not a valid parameter for Option.

<!-- gh-comment-id:439038639 --> @hrkfdn commented on GitHub (Nov 15, 2018): Yes, I know how to pass a Country enum, but it's not clear how to pass "from_token". The way it is described in the function documentation is not possible, as the "from_token" literal is not a valid parameter for Option<Country>.
Author
Owner

@Qluxzz commented on GitHub (Jan 8, 2021):

What would be the best course of action if we want to enable using the user tokens country instead of having to supply a country ourselves?

A solution would be a type of Country | FromToken using an enum maybe?

enum Market {
    Country(Country),
    FromToken
}

And then using it like this?

if let Some(market) = market {
    params.insert("country".to_owned(), match market {
        Market::Country(c) => c.to_string(),
        Market::FromToken => "from_token".to_string()
    });
}

As @hrkfdn mentioned, country defaults to US if no country was supplied and country is required, would not "from_token" be a better default here?

<!-- gh-comment-id:756846431 --> @Qluxzz commented on GitHub (Jan 8, 2021): What would be the best course of action if we want to enable using the user tokens country instead of having to supply a country ourselves? A solution would be a type of Country | FromToken using an enum maybe? ```Rust enum Market { Country(Country), FromToken } ``` And then using it like this? ```Rust if let Some(market) = market { params.insert("country".to_owned(), match market { Market::Country(c) => c.to_string(), Market::FromToken => "from_token".to_string() }); } ``` As @hrkfdn mentioned, country defaults to US if no country was supplied and country is required, would not "from_token" be a better default here?
Author
Owner

@Qluxzz commented on GitHub (Jan 24, 2021):

Any thoughts on this? I'm willing to implement this myself and send a PR but I would need to know which way you would prefer.

<!-- gh-comment-id:766415101 --> @Qluxzz commented on GitHub (Jan 24, 2021): Any thoughts on this? I'm willing to implement this myself and send a PR but I would need to know which way you would prefer.
Author
Owner

@marioortizmanero commented on GitHub (Jan 24, 2021):

Sorry about that. Yes, I think that's the best solution to keep it type-safe. Go ahead and make a PR if you want :)

The API doesn't specify a default value so I'm not sure what you're referring to with defaulting to US. If anything I would just not send that parameter.

<!-- gh-comment-id:766421779 --> @marioortizmanero commented on GitHub (Jan 24, 2021): Sorry about that. Yes, I think that's the best solution to keep it type-safe. Go ahead and make a PR if you want :) The API doesn't specify a default value so I'm not sure what you're referring to with _defaulting to US_. If anything I would just not send that parameter.
Author
Owner

@Qluxzz commented on GitHub (Jan 24, 2021):

The API doesn't specify a default value so I'm not sure what you're referring to with defaulting to US. If anything I would just not send that parameter.

Sorry, seems like it was only one function that defaulted it to the US while it is optional everywhere else.

github.com/ramsayleung/rspotify@18f2396026/src/client.rs (L329-L332)

I feel like defaulting to "from_token" is the better option here.

<!-- gh-comment-id:766425643 --> @Qluxzz commented on GitHub (Jan 24, 2021): > The API doesn't specify a default value so I'm not sure what you're referring to with _defaulting to US_. If anything I would just not send that parameter. Sorry, seems like it was only one function that defaulted it to the US while it is optional everywhere else. https://github.com/ramsayleung/rspotify/blob/18f2396026b598edb9c54616ac82ea62715f3f06/src/client.rs#L329-L332 I feel like defaulting to "from_token" is the better option here.
Author
Owner

@marioortizmanero commented on GitHub (Jan 24, 2021):

Yes, I would consider that code snippet wrong; it shouldn't default to the US.

In that case it is a required field for the endpoint, so I just wouldn't use an Option at all. Why not just keep it as close as possible to the real API?

<!-- gh-comment-id:766429852 --> @marioortizmanero commented on GitHub (Jan 24, 2021): Yes, I would consider that code snippet wrong; it shouldn't default to the US. In that case it is a required field for the endpoint, so I just wouldn't use an `Option` at all. Why not just keep it as close as possible to the real API?
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#8
No description provided.