-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup #250
Merged
Merged
Cleanup #250
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bc1c7be
Cleanup Cargo.toml
marioortizmanero 4489430
logging -> debug_assert
marioortizmanero 0e38d95
More cargo cleanup
marioortizmanero 933f116
Formatting
marioortizmanero 6ca5fff
No need for a fancy logger
marioortizmanero 466a9a9
Clean up derives
marioortizmanero da95750
Lots of fixes & improvements
marioortizmanero c337ae8
Upgraded all dependencies with `cargo upgrade`
marioortizmanero bdfc9a7
Fix docs errors
marioortizmanero 1fb3492
Fix clippy errors
marioortizmanero 775a8fb
Fix test errors
marioortizmanero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//! All objects related to the auth flows defined by Spotify API | ||
|
||
use crate::{ | ||
custom_serde::{duration_second, space_separated_scopes}, | ||
ModelResult, | ||
}; | ||
|
||
use std::{ | ||
collections::HashSet, | ||
fs, | ||
io::{Read, Write}, | ||
path::Path, | ||
}; | ||
|
||
use chrono::{DateTime, Duration, Utc}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Spotify access token information | ||
/// | ||
/// [Reference](https://developer.spotify.com/documentation/general/guides/authorization-guide/) | ||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct Token { | ||
/// An access token that can be provided in subsequent calls | ||
pub access_token: String, | ||
/// The time period for which the access token is valid. | ||
#[serde(with = "duration_second")] | ||
pub expires_in: Duration, | ||
/// The valid time for which the access token is available represented | ||
/// in ISO 8601 combined date and time. | ||
pub expires_at: Option<DateTime<Utc>>, | ||
/// A token that can be sent to the Spotify Accounts service | ||
/// in place of an authorization code | ||
pub refresh_token: Option<String>, | ||
/// A list of [scopes](https://developer.spotify.com/documentation/general/guides/scopes/) | ||
/// which have been granted for this `access_token` | ||
/// | ||
/// You may use the `scopes!` macro in | ||
/// [`rspotify-macros`](https://docs.rs/rspotify-macros) to build it at | ||
/// compile time easily. | ||
// The token response from spotify is singular, hence the rename to `scope` | ||
#[serde(default, with = "space_separated_scopes", rename = "scope")] | ||
pub scopes: HashSet<String>, | ||
} | ||
|
||
impl Default for Token { | ||
fn default() -> Self { | ||
Token { | ||
access_token: String::new(), | ||
expires_in: Duration::seconds(0), | ||
expires_at: Some(Utc::now()), | ||
refresh_token: None, | ||
scopes: HashSet::new(), | ||
} | ||
} | ||
} | ||
|
||
impl Token { | ||
/// Tries to initialize the token from a cache file. | ||
pub fn from_cache<T: AsRef<Path>>(path: T) -> ModelResult<Self> { | ||
let mut file = fs::File::open(path)?; | ||
let mut tok_str = String::new(); | ||
file.read_to_string(&mut tok_str)?; | ||
let tok = serde_json::from_str::<Token>(&tok_str)?; | ||
|
||
Ok(tok) | ||
} | ||
|
||
/// Saves the token information into its cache file. | ||
pub fn write_cache<T: AsRef<Path>>(&self, path: T) -> ModelResult<()> { | ||
let token_info = serde_json::to_string(&self)?; | ||
|
||
let mut file = fs::OpenOptions::new().write(true).create(true).open(path)?; | ||
file.set_len(0)?; | ||
file.write_all(token_info.as_bytes())?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Check if the token is expired | ||
pub fn is_expired(&self) -> bool { | ||
self.expires_at | ||
.map_or(true, |x| Utc::now().timestamp() > x.timestamp()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you have replaced
log
withdebug_assert
, is it possible to remove this crate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to remove it but I think it's still useful to have around. I wanted to add some log statements before releasing the new version because we don't even log that much right now. But I wanted to wait for the pending PRs to be merged first.