Code Monkey home page Code Monkey logo

Comments (10)

hrkfdn avatar hrkfdn commented on August 11, 2024

Mhh, or is passing None equivalent to from_token?

from rspotify.

ramsayleung avatar ramsayleung commented on August 11, 2024

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

from rspotify.

hrkfdn avatar hrkfdn commented on August 11, 2024

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.

from rspotify.

ramsayleung avatar ramsayleung commented on August 11, 2024

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 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

from rspotify.

hrkfdn avatar hrkfdn commented on August 11, 2024

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.

from rspotify.

Qluxzz avatar Qluxzz commented on August 11, 2024

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?

from rspotify.

Qluxzz avatar Qluxzz commented on August 11, 2024

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.

from rspotify.

marioortizmanero avatar marioortizmanero commented on August 11, 2024

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.

from rspotify.

Qluxzz avatar Qluxzz commented on August 11, 2024

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.

rspotify/src/client.rs

Lines 329 to 332 in 18f2396

params.insert(
"country".to_owned(),
country.into().unwrap_or(Country::UnitedStates).to_string(),
);

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

from rspotify.

marioortizmanero avatar marioortizmanero commented on August 11, 2024

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?

from rspotify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.