Skip to content
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 11 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,25 @@ rspotify-macros = { path = "rspotify-macros", version = "0.10.0" }
rspotify-model = { path = "rspotify-model", version = "0.10.0" }
rspotify-http = { path = "rspotify-http", version = "0.10.0", default-features = false }

### Client ###
async-stream = { version = "0.3.0", optional = true }
async-trait = { version = "0.1.48", optional = true }
async-stream = { version = "0.3.2", optional = true }
async-trait = { version = "0.1.51", optional = true }
base64 = "0.13.0"
chrono = { version = "0.4.13", features = ["serde", "rustc-serialize"] }
chrono = { version = "0.4.19", features = ["serde", "rustc-serialize"] }
dotenv = { version = "0.15.0", optional = true }
futures = { version = "0.3.8", optional = true }
futures-util = "0.3.8" # TODO
getrandom = "0.2.0"
log = "0.4.11"
maybe-async = "0.2.1"
serde = { version = "1.0.115", features = ["derive"] }
serde_json = "1.0.57"
thiserror = "1.0.20"
futures = { version = "0.3.17", optional = true }
getrandom = "0.2.3"
log = "0.4.14"
maybe-async = "0.2.6"
serde = { version = "1.0.130", default-features = false }
serde_json = "1.0.67"
thiserror = "1.0.29"
url = "2.2.2"
webbrowser = { version = "0.5.5", optional = true }

### Auth ###
# chrono = { version = "0.4.13", features = ["serde", "rustc-serialize"] }
# log = "0.4.11"
# maybe-async = "0.2.1"
# thiserror = "1.0.20"

[dev-dependencies]
env_logger = "0.9.0"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] }
futures-util = "0.3.8"
env_logger = { version = "0.9.0", default-features = false }
tokio = { version = "1.11.0", features = ["rt-multi-thread", "macros"] }
futures-util = "0.3.17"

[features]
default = ["client-reqwest", "reqwest-default-tls"]
Expand Down
23 changes: 9 additions & 14 deletions rspotify-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@ rspotify-model = { path = "../rspotify-model", version = "0.10.0" }

# Temporary until https://github.com/rust-lang/rfcs/issues/2739, for
# `maybe_async`.
async-trait = { version = "0.1.48", optional = true }
base64 = "0.13.0"
futures = { version = "0.3.8", optional = true }
log = "0.4.11"
maybe-async = "0.2.4"
reqwest = { version = "0.11.0", default-features = false, features = ["json", "socks"], optional = true }
serde_json = "1.0.57"
thiserror = "1.0.20"
ureq = { version = "2.0", default-features = false, features = ["json", "cookies"], optional = true }
url = "2.2.2"
async-trait = { version = "0.1.51", optional = true }
log = "0.4.14"
Copy link
Owner

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 with debug_assert, is it possible to remove this crate?

Copy link
Collaborator Author

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.

maybe-async = "0.2.6"
serde_json = "1.0.67"
thiserror = "1.0.29"

[dev-dependencies]
env_logger = "0.9.0"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] }
# Supported clients
reqwest = { version = "0.11.4", default-features = false, features = ["json", "socks"], optional = true }
ureq = { version = "2.2.0", default-features = false, features = ["json", "cookies"], optional = true }

[features]
default = ["client-reqwest", "reqwest-default-tls"]
Expand All @@ -50,5 +45,5 @@ reqwest-native-tls-vendored = ["reqwest/native-tls-vendored"]
ureq-rustls-tls = ["ureq/tls"]

# Internal features for checking async or sync compilation
__async = ["async-trait", "futures"]
__async = ["async-trait"]
__sync = ["maybe-async/is_sync"]
2 changes: 1 addition & 1 deletion rspotify-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ keywords = ["spotify", "api", "rspotify"]
edition = "2018"

[dev-dependencies]
serde_json = "1.0.57"
serde_json = "1.0.67"
11 changes: 5 additions & 6 deletions rspotify-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ keywords = ["spotify", "api", "rspotify"]
edition = "2018"

[dependencies]
chrono = { version = "0.4.13", features = ["serde", "rustc-serialize"] }
serde = { version = "1.0.115", features = ["derive"] }
strum = { version = "0.21", features = ["derive"] }
thiserror = "1.0.20"
chrono = { version = "0.4.19", features = ["serde", "rustc-serialize"] }
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.67"
strum = { version = "0.21.0", features = ["derive"] }
thiserror = "1.0.29"

[dev-dependencies]
serde_json = "1.0.57"
11 changes: 9 additions & 2 deletions rspotify-model/src/album.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use super::artist::SimplifiedArtist;
use super::image::Image;
use super::page::Page;
use super::track::SimplifiedTrack;
use super::Restriction;
use crate::{AlbumType, Copyright, DatePrecision, Type};
use crate::{AlbumType, Copyright, DatePrecision, RestrictionReason, Type};

/// Simplified Album Object
///
Expand Down Expand Up @@ -88,3 +87,11 @@ pub struct SavedAlbum {
pub added_at: DateTime<Utc>,
pub album: FullAlbum,
}

/// Album restriction object
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#object-albumrestrictionobject)
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Restriction {
pub reason: RestrictionReason,
}
5 changes: 4 additions & 1 deletion rspotify-model/src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! All objects related to audio defined by Spotify API

use crate::{duration_ms, enums::Modality, modality};
use crate::{
custom_serde::{duration_ms, modality},
enums::Modality,
};
use serde::{Deserialize, Serialize};
use std::time::Duration;

Expand Down
84 changes: 84 additions & 0 deletions rspotify-model/src/auth.rs
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())
}
}
3 changes: 2 additions & 1 deletion rspotify-model/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use super::device::Device;
use super::PlayableItem;
use crate::{
millisecond_timestamp, option_duration_ms, CurrentlyPlayingType, DisallowKey, RepeatState, Type,
custom_serde::{millisecond_timestamp, option_duration_ms},
CurrentlyPlayingType, DisallowKey, RepeatState, Type,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize};
Expand Down
Loading