Struct Client

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

Source

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>

Source

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.

Source

pub fn access_token(&self) -> Result<String>

Get the access token secret as an owned (cloned) string. If you only need a reference, you can use token yourself and get a reference from the returned RwLock.

This method will fail if the RwLock that holds the token has been poisoned.

Source

pub fn refresh_token(&self) -> Result<Option<String>>

Get the refresh token secret as an owned (cloned) string. If you only need a reference, you can use token yourself and get a reference from the returned RwLock.

This method will fail if the RwLock that holds the token has been poisoned.

Source

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>

Source

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.

Source

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>

Source

pub fn new<T, S>( client_id: T, scopes: S, redirect_uri: RedirectUrl, auto_refresh: bool, ) -> (Self, Url)
where T: Into<String>, S: Into<Scopes>,

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.

Source

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>

Source

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>

Source

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>

Source

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>

Source

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

Trait Implementations§

Source§

impl<A: Clone + AuthenticationState, F: Clone + AuthFlow> Clone for Client<A, F>

Source§

fn clone(&self) -> Client<A, F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: Debug + AuthenticationState, F: Debug + AuthFlow> Debug for Client<A, F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A, F> Freeze for Client<A, F>
where F: Freeze,

§

impl<A, F> !RefUnwindSafe for Client<A, F>

§

impl<A, F> Send for Client<A, F>
where F: Send, A: Send + Sync,

§

impl<A, F> Sync for Client<A, F>
where F: Sync, A: Send + Sync,

§

impl<A, F> Unpin for Client<A, F>
where F: Unpin,

§

impl<A, F> !UnwindSafe for Client<A, F>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,