spotifyrs/lib.rs
1//! spotifyrs aims to be a relatively easy to use wrapper for the Spotify API. It is still currently under development (see [issues and roadmap](https://github.com/TheSharkhead2/spotify.rs#issues-and-roadmap)); however, it can be used for most purposes in its current state.
2//! This wrapper allows you to interact with the Spotify API using simple method calls and consistent objects that give you a consistent way to access the data returned by the API.
3//!
4//! # Authorization codeflow
5//! Note: Currently, the only supported authorization method is the PKCE extension for OAuth2.0, ideal for client-side applications.
6//!
7//! Support exists for both a fully-automated authentication from a localhost port as well as more manual authentication that allows you to use any redirect uri. Likely the more manual authentication will become default, but it is currently gated behind the `"manual_auth"` feature.
8//!
9//! ## Localhost/Fully Automated Authentication
10//! This authentication is ideal for local applications.
11//!
12//! First, create a .env file in the root of your project with the `CLIENT_ID` variable:
13//! ```dotenv
14//! CLIENT_ID=your_client_id
15//! ```
16//! Then, you can simply create a new `Spotify` object with the `authenticate` method, which will be used to make all requests to the API. You need to pass in the localhost port you want to use for the redirect URI and the scope you need:
17//! ```ignore
18//! use spotifyrs::Spotify;
19//!
20//! let spotify = Spotify::new(); // create blank object
21//! spotify.authenticate(String::from("8080"), String::from("user-read-private user-read-email")).unwrap(); // authenticate it
22//! ```
23//! This will open a browser window and prompt the user to authorize your application. Once they do, they will be redirected to an html page confirming the authorization. You can then use the `Spotify` object to make requests to the API.
24//! This can return an error if the user cancels the request. Be warned, this method will also not automatically timeout and will indefinitely hang waiting for the user to authorize if the user closes the browser.
25//!
26//! The Spotify object will handle refreshing your access token when it expires upon making a request. This ensures that you never have to check if your token is expired.
27//!
28//! ## Arbitrary Redirect URI Authentication
29//! This authentication method is best for all situations where the program cannot read requests sent to localhost ports on the machine. For example, a website that is employing the Spotify API.
30//!
31//! As this authentication method does not assume assess to a local machine, it requires you to supply the client ID from within the program. Additionally, you will have to manually call a function to generate a code challenge and a code verifier, which are used by the PKCE for authentication purposes. The full code flow to get the authorization code request url looks like this:
32//! ```
33//! let scope = String::from("user-modify-playback-state");
34//! let redirect_uri = "www.YOUR_URL.com/callback";
35//! let client_id = dotenv::var("CLIENT_ID").unwrap();
36//!
37//! let (code_verifier, code_challenge) = generate_verifier();
38//! let (auth_url, state) = requesturl_authorization_code(
39//! &client_id[..],
40//! redirect_uri,
41//! &scope[..],
42//! &code_challenge[..],
43//! );
44//! ```
45//!
46//! You then should redirect the user to the provided `auth_url`, at which point, once the user has granted you access, they will be redirected to the provided redirect uri with a query containing both the access code (under `code`) and the state value. It is recommended you check this `state` matches that returned in the above function. With these, you should run
47//! ```
48//! let spotify = Spotify::new_from_auth_code(
49//! auth_code,
50//! &client_id[..],
51//! scope,
52//! code_verifier,
53//! redirect_uri,
54//! );
55//! ```
56//! This Spotify object will handle refreshing the access token and is how you will interact with the API.
57//!
58//! # Examples
59//! We can get information on a specific artist:
60//! ```ignore
61//! let artist: Artist = spotify.get_artist("59sBwR0jPSTrbMtuTkRPN5").unwrap();
62//!
63//! assert_eq!(artist.name, "Wild Rivers");
64//! ```
65//!
66//! Alternatively, we can get the tracks in the current user's queue:
67//! ```ignore
68//! let (currently_playing, queue) = spotify.get_users_queue().unwrap();
69//! ```
70//!
71//! See the [Spotify struct](struct.Spotify.html) for a full list of supported endpoints.
72//!
73
74mod albums;
75mod artists;
76mod authorization;
77mod categories;
78mod genres;
79mod markets;
80mod object_formatting;
81mod player;
82mod playlist;
83mod spotify;
84mod srequest;
85mod tracks;
86mod users;
87
88pub use spotify::{
89 Album, AlbumType, AnalysisTrack, Artist, Bar, Beat, Category, DatedAlbum, DatedTrack, Device,
90 ExternalTrackIds, FeatureTrack, Playback, PlaybackActions, PlayedTrack, Playlist,
91 PlaylistTrack, ReleaseDatePrecision, RepeatState, RestrictionReason, Section, Segment, Spotify,
92 SpotifyCollection, SpotifyContext, SpotifyError, SpotifyImage, SpotifyObject, Tatum, TimeRange,
93 Track, User,
94}; // re-export relevant structs and enums
95
96// export if manual authentication feature is active
97#[cfg(feature = "manual_auth")]
98pub use authorization::{generate_verifier, requesturl_authorization_code};