ramsayleung / rspotify

Spotify Web API SDK implemented on Rust
MIT License
639 stars 123 forks source link

Add token fetch callback #418

Closed ramsayleung closed 1 year ago

ramsayleung commented 1 year ago

Description

Add a user-customized callback function support which is invoked whenever client succeeds to request or refresh a token, so it provides users an opportunity to save token into file, store into database etc.

Motivation and Context

400

Dependencies

None

Type of change

Please delete options that are not relevant.

How has this been tested?

Is this change properly documented?

Please make sure you've properly documented the changes you're making.

Don't forget to add an entry to the CHANGELOG if necessary (new features, breaking changes, relevant internal improvements).

PaulOlteanu commented 1 year ago

I have some tests written as well https://github.com/PaulOlteanu/rspotify/commit/2d38341478bfa950225a2fbd8e6e5d6ddcb4c5ac if you'd like to add those

ramsayleung commented 1 year ago

Thanks for your contribution, I would like to add your test, but I refine the callback function based your code.

The callback function is a field of Config struct instead of SpotifyClient, so it's unnecessary to use Arc<Mutex> to sync between different threads.

TokenCallback should only be invoked when a token is gained successfully, so it's unnecessary to use Fn(Option<Token>, Fn(Token) is more appropriate.

Furthermore, the callback function might throw an error, so the return value of the callback function should be Result

PaulOlteanu commented 1 year ago

Yeah I had them written up for my original implementation - I just added them in case you wanted to modify them to fit this pr

ramsayleung commented 1 year ago

Sure, thanks for your contribution in advance.