From db34860258a117ad525d8557d278bd8fda03189c Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 16 Jul 2020 09:39:12 -0400 Subject: [PATCH] fix patreon client --- Cargo.lock | 4 ++ Cargo.toml | 1 - lib/patreon/.gitignore | 1 + lib/patreon/Cargo.toml | 6 ++ lib/patreon/examples/campaign.rs | 17 +++++ lib/patreon/src/campaign.rs | 30 --------- lib/patreon/src/lib.rs | 110 +++++++++++++++++++++++++------ lib/patreon/src/pledge.rs | 16 ----- lib/patreon/src/user.rs | 28 -------- 9 files changed, 118 insertions(+), 95 deletions(-) create mode 100644 lib/patreon/.gitignore create mode 100644 lib/patreon/examples/campaign.rs delete mode 100644 lib/patreon/src/campaign.rs delete mode 100644 lib/patreon/src/pledge.rs delete mode 100644 lib/patreon/src/user.rs diff --git a/Cargo.lock b/Cargo.lock index 29464a9..4e8fb41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1269,11 +1269,15 @@ name = "patreon" version = "0.1.0" dependencies = [ "chrono", + "envy", "jsonapi", + "log 0.3.9", + "pretty_env_logger", "reqwest", "serde", "serde_json", "thiserror", + "tokio", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 7424817..00b810b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,6 @@ pretty_env_logger = "0" prometheus = { version = "0.9", default-features = false, features = ["process"] } rand = "0" rss = "1" -ructe = "0.11" serde_dhall = "0.5.3" serde = { version = "1", features = ["derive"] } serde_yaml = "0.8" diff --git a/lib/patreon/.gitignore b/lib/patreon/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/lib/patreon/.gitignore @@ -0,0 +1 @@ +.env diff --git a/lib/patreon/Cargo.toml b/lib/patreon/Cargo.toml index 8ccec8f..1e77c5a 100644 --- a/lib/patreon/Cargo.toml +++ b/lib/patreon/Cargo.toml @@ -13,3 +13,9 @@ reqwest = { version = "0.10", features = ["json"] } serde_json = "1.0" serde = { version = "1", features = ["derive"] } thiserror = "1" +log = "0" + +[dev-dependencies] +tokio = { version = "0.2", features = ["macros"] } +envy = "0.4" +pretty_env_logger = "0" \ No newline at end of file diff --git a/lib/patreon/examples/campaign.rs b/lib/patreon/examples/campaign.rs new file mode 100644 index 0000000..be56f9d --- /dev/null +++ b/lib/patreon/examples/campaign.rs @@ -0,0 +1,17 @@ +use patreon::*; + +#[tokio::main] +async fn main() -> Result<()> { + pretty_env_logger::init(); + let creds: Credentials = envy::prefixed("PATREON_").from_env().unwrap(); + let cli = Client::new(creds); + + let camp = cli.campaign().await?; + println!("{:#?}", camp); + + let id = camp.data[0].id.clone(); + + let pledges = cli.pledges(id).await?; + println!("{:#?}", pledges); + Ok(()) +} diff --git a/lib/patreon/src/campaign.rs b/lib/patreon/src/campaign.rs deleted file mode 100644 index ed12b98..0000000 --- a/lib/patreon/src/campaign.rs +++ /dev/null @@ -1,30 +0,0 @@ -use serde::{Deserialize, Serialize}; -use chrono::prelude::*; - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct Campaign { - pub id: String, - pub summary: String, - pub creation_name: String, - pub display_patron_goals: bool, - pub pay_per_name: String, - pub one_liner: String, - pub main_video_embed: String, - pub main_video_url: String, - pub image_small_url: String, - pub image_url: String, - pub thanks_video_url: String, - pub thanks_embed: String, - pub thanks_msg: String, - pub is_charged_immediately: bool, - pub is_monthly: bool, - pub is_nsfw: bool, - pub is_plural: bool, - pub created_at: DateTime, - pub published_at: DateTime, - pub pledge_url: String, - pub pledge_sum: i32, - pub patron_count: u32, - pub creation_coult: u32, - pub outstanding_payment_amount_cents: u64, -} diff --git a/lib/patreon/src/lib.rs b/lib/patreon/src/lib.rs index 79cd545..1f5df72 100644 --- a/lib/patreon/src/lib.rs +++ b/lib/patreon/src/lib.rs @@ -1,21 +1,68 @@ #[macro_use] extern crate jsonapi; +#[macro_use] +extern crate log; -use jsonapi::api::*; -use jsonapi::model::*; use serde::{Deserialize, Serialize}; use thiserror::Error; +use chrono::prelude::*; -mod campaign; -mod pledge; -mod user; -pub use campaign::Campaign; -pub use pledge::Pledge; -pub use user::User; +pub type Campaigns = Vec>; +pub type Pledges = Vec>; -jsonapi_model!(Campaign; "campaign"); -jsonapi_model!(Pledge; "pledge"); -jsonapi_model!(User; "user"); +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Campaign { + pub summary: String, + pub creation_name: String, + pub display_patron_goals: bool, + pub pay_per_name: String, + pub one_liner: Option, + pub main_video_embed: Option, + pub main_video_url: Option, + pub image_small_url: String, + pub image_url: String, + pub thanks_video_url: Option, + pub thanks_embed: Option, + pub thanks_msg: String, + pub is_charged_immediately: bool, + pub is_monthly: bool, + pub is_nsfw: bool, + pub is_plural: bool, + pub created_at: DateTime, + pub published_at: DateTime, + pub pledge_url: String, + pub pledge_sum: i32, + pub patron_count: u32, + pub creation_count: u32, + pub outstanding_payment_amount_cents: u64, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Pledge { + pub amount_cents: u32, + pub created_at: String, + pub declined_since: Option, + pub pledge_cap_cents: u32, + pub patron_pays_fees: bool, + pub total_historical_amount_cents: Option, + pub is_paused: Option, + pub has_shipping_address: Option, + pub outstanding_payment_amount_cents: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct User { + pub first_name: String, + pub last_name: String, + pub full_name: String, + pub vanity: Option, + pub about: Option, + pub gender: i32, + pub image_url: String, + pub thumb_url: String, + pub created: DateTime, + pub url: String, +} pub type Result = std::result::Result; @@ -41,6 +88,25 @@ pub struct Client { creds: Credentials, } +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Data { + pub data: T, + pub included: Option>, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Object { + pub id: String, + pub attributes: T, + pub r#type: String, + pub links: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Links { + related: String, +} + impl Client { pub fn new(creds: Credentials) -> Self { Self { @@ -50,26 +116,27 @@ impl Client { } } - pub async fn campaign(&self) -> Result { - Ok(self + pub async fn campaign(&self) -> Result>, ()>> { + let data = self .cli .get(&format!( "{}/oauth2/api/current_user/campaigns", self.base_url )) + .query(&[("include", "patron.null"), ("includes", "")]) .header( "Authorization", format!("Bearer {}", self.creds.access_token), ) .send() .await? - .error_for_status()? - .json() - .await?) + .error_for_status()?.text().await?; + log::debug!("campaign response: {}", data); + Ok(serde_json::from_str(&data)?) } - pub async fn pledges(&self, camp_id: u32) -> Result> { - Ok(self + pub async fn pledges(&self, camp_id: String) -> Result>> { + let data = self .cli .get(&format!( "{}/oauth2/api/campaigns/{}/pledges", @@ -83,7 +150,10 @@ impl Client { .send() .await? .error_for_status()? - .json() - .await?) + .text() + .await?; + log::debug!("pledges for {}: {}", camp_id, data); + let data : Data>, Object> = serde_json::from_str(&data)?; + Ok(data.included.unwrap()) } } diff --git a/lib/patreon/src/pledge.rs b/lib/patreon/src/pledge.rs deleted file mode 100644 index dc71d89..0000000 --- a/lib/patreon/src/pledge.rs +++ /dev/null @@ -1,16 +0,0 @@ -use serde::{Deserialize, Serialize}; -use chrono::prelude::*; - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct Pledge { - pub id: String, - pub amount_cents: u32, - pub created_at: DateTime, - pub declined_since: DateTime, - pub pledge_cap_cents: u32, - pub patron_pays_fees: bool, - pub total_historical_amount_cents: Option, - pub is_paused: Option, - pub has_shipping_address: Option, - pub outstanding_payment_amount_cents: Option, -} diff --git a/lib/patreon/src/user.rs b/lib/patreon/src/user.rs deleted file mode 100644 index 598e20b..0000000 --- a/lib/patreon/src/user.rs +++ /dev/null @@ -1,28 +0,0 @@ -use serde::{Deserialize, Serialize}; -use chrono::prelude::*; - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct User { - pub id: String, - pub first_name: String, - pub last_name: String, - pub full_name: String, - pub vanity: String, - pub email: String, - pub about: String, - pub facebook_id: String, - pub gender: i32, - pub has_password: bool, - pub image_url: String, - pub thumb_url: String, - pub youtube: String, - pub twitter: String, - pub facebook: String, - pub is_email_verified: bool, - pub is_suspended: bool, - pub is_deleted: bool, - pub is_nuked: bool, - pub created: DateTime, - pub url: String, - pub discord_id: String, -}