pub struct Client<A: AuthenticationState, F: AuthFlow> {
pub auto_refresh: bool,
/* private fields */
}
Expand description
The client which handles the authentication and all the Spotify API requests.
It is recommended to use one of the following: AuthCodeClient
, AuthCodePkceClient
or ClientCredsClient
, depending on the chosen authentication flow.
Fields§
§auto_refresh: bool
Dictates whether or not the client will request a new token when the current one is about the expire.
It will check if the token has expired in every request.
Implementations§
Source§impl Client<Token, UnknownFlow>
impl Client<Token, UnknownFlow>
Sourcepub async fn from_refresh_token(
client_id: impl Into<String>,
client_secret: Option<&str>,
scopes: Option<Scopes>,
auto_refresh: bool,
refresh_token: String,
) -> Result<Self>
pub async fn from_refresh_token( client_id: impl Into<String>, client_secret: Option<&str>, scopes: Option<Scopes>, auto_refresh: bool, refresh_token: String, ) -> Result<Self>
Create a new authenticated and authorised client from a refresh token.
This method will fail if the refresh token is invalid or a new one cannot be obtained.
Source§impl<F: AuthFlow> Client<Token, F>
impl<F: AuthFlow> Client<Token, F>
Sourcepub fn token(&self) -> Arc<RwLock<Token>>
pub fn token(&self) -> Arc<RwLock<Token>>
Get a reference to the client’s token.
Please note that the RwLock used here is not async-aware, and thus the read/write guard should not be held across await points.
Sourcepub fn access_token(&self) -> Result<String>
pub fn access_token(&self) -> Result<String>
Sourcepub fn refresh_token(&self) -> Result<Option<String>>
pub fn refresh_token(&self) -> Result<Option<String>>
Sourcepub async fn exchange_refresh_token(&self) -> Result<()>
pub async fn exchange_refresh_token(&self) -> Result<()>
Exchange the refresh token for a new access token and updates it in the client. Only some auth flows allow for token refreshing.
Source§impl Client<Unauthenticated, AuthCodeFlow>
impl Client<Unauthenticated, AuthCodeFlow>
Sourcepub fn new<S>(
client_id: impl Into<String>,
client_secret: impl Into<String>,
scopes: S,
redirect_uri: RedirectUrl,
auto_refresh: bool,
) -> (Self, Url)where
S: Into<Scopes>,
pub fn new<S>(
client_id: impl Into<String>,
client_secret: impl Into<String>,
scopes: S,
redirect_uri: RedirectUrl,
auto_refresh: bool,
) -> (Self, Url)where
S: Into<Scopes>,
Create a new client and generate an authorisation URL
You must redirect the user to the returned URL, which in turn redirects them to
the redirect_uri
you provided, along with a code
and state
parameter in the URl.
They are required for the next step in the auth process.
Sourcepub async fn authenticate(
self,
auth_code: impl Into<String>,
csrf_state: impl AsRef<str>,
) -> Result<Client<Token, AuthCodeFlow>>
pub async fn authenticate( self, auth_code: impl Into<String>, csrf_state: impl AsRef<str>, ) -> Result<Client<Token, AuthCodeFlow>>
This will exchange the auth_code
for a token which will allow the client
to make requests.
csrf_state
is used for CSRF protection.
Source§impl Client<Unauthenticated, AuthCodePkceFlow>
impl Client<Unauthenticated, AuthCodePkceFlow>
Sourcepub fn new<T, S>(
client_id: T,
scopes: S,
redirect_uri: RedirectUrl,
auto_refresh: bool,
) -> (Self, Url)
pub fn new<T, S>( client_id: T, scopes: S, redirect_uri: RedirectUrl, auto_refresh: bool, ) -> (Self, Url)
Create a new client and generate an authorisation URL
You must redirect the user to the received URL, which in turn redirects them to
the redirect URI you provided, along with a code
and state
parameter in the URl.
They are required for the next step in the auth process.
Sourcepub async fn authenticate(
self,
auth_code: impl Into<String>,
csrf_state: impl AsRef<str>,
) -> Result<Client<Token, AuthCodePkceFlow>>
pub async fn authenticate( self, auth_code: impl Into<String>, csrf_state: impl AsRef<str>, ) -> Result<Client<Token, AuthCodePkceFlow>>
This will exchange the auth_code
for a token which will allow the client
to make requests.
csrf_state
is used for CSRF protection.
Source§impl Client<Unauthenticated, ClientCredsFlow>
impl Client<Unauthenticated, ClientCredsFlow>
Sourcepub async fn authenticate(
client_id: impl Into<String>,
client_secret: impl Into<String>,
) -> Result<ClientCredsClient<Token>>
pub async fn authenticate( client_id: impl Into<String>, client_secret: impl Into<String>, ) -> Result<ClientCredsClient<Token>>
This will exchange the client credentials for an access token used to make requests.
This authentication method doesn’t allow for token refreshing or to access user resources.
Source§impl Client<Token, AuthCodeFlow>
impl Client<Token, AuthCodeFlow>
Sourcepub async fn from_access_token(
client_id: impl Into<String>,
client_secret: impl Into<String>,
auto_refresh: bool,
token: Token,
) -> Result<Self>
pub async fn from_access_token( client_id: impl Into<String>, client_secret: impl Into<String>, auto_refresh: bool, token: Token, ) -> Result<Self>
Create a new authenticated client from an access token. This client will be able to access user data.
This method will fail if the access token is invalid (a request will be sent to check the token).
Source§impl Client<Token, AuthCodePkceFlow>
impl Client<Token, AuthCodePkceFlow>
Sourcepub async fn from_access_token(
client_id: impl Into<String>,
auto_refresh: bool,
token: Token,
) -> Result<Self>
pub async fn from_access_token( client_id: impl Into<String>, auto_refresh: bool, token: Token, ) -> Result<Self>
Create a new authenticated client from an access token. This client will be able to access user data.
This method will fail if the access token is invalid (a request will be sent to check the token).
Source§impl Client<Token, ClientCredsFlow>
impl Client<Token, ClientCredsFlow>
Sourcepub async fn from_access_token(
client_id: impl Into<String>,
client_secret: impl Into<String>,
token: Token,
) -> Result<Self>
pub async fn from_access_token( client_id: impl Into<String>, client_secret: impl Into<String>, token: Token, ) -> Result<Self>
Create a new authenticated client from an access token. This client will not be able to access user data.
This method will fail if the access token is invalid (a request will be sent to check the token).