diff --git a/lib/patreon/Cargo.toml b/lib/patreon/Cargo.toml new file mode 100644 index 0000000..8ccec8f --- /dev/null +++ b/lib/patreon/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "patreon" +version = "0.1.0" +authors = ["Christine Dodrill "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +chrono = { version = "0.4", features = ["serde"] } +jsonapi = "0.6.6" +reqwest = { version = "0.10", features = ["json"] } +serde_json = "1.0" +serde = { version = "1", features = ["derive"] } +thiserror = "1" diff --git a/lib/patreon/src/campaign.rs b/lib/patreon/src/campaign.rs new file mode 100644 index 0000000..ed12b98 --- /dev/null +++ b/lib/patreon/src/campaign.rs @@ -0,0 +1,30 @@ +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 new file mode 100644 index 0000000..79cd545 --- /dev/null +++ b/lib/patreon/src/lib.rs @@ -0,0 +1,89 @@ +#[macro_use] +extern crate jsonapi; + +use jsonapi::api::*; +use jsonapi::model::*; +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +mod campaign; +mod pledge; +mod user; +pub use campaign::Campaign; +pub use pledge::Pledge; +pub use user::User; + +jsonapi_model!(Campaign; "campaign"); +jsonapi_model!(Pledge; "pledge"); +jsonapi_model!(User; "user"); + +pub type Result = std::result::Result; + +#[derive(Error, Debug)] +pub enum Error { + #[error("json error: {0:?}")] + Json(#[from] serde_json::Error), + #[error("request error: {0:?}")] + Request(#[from] reqwest::Error), +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Credentials { + pub client_id: String, + pub client_secret: String, + pub access_token: String, + pub refresh_token: String, +} + +pub struct Client { + cli: reqwest::Client, + base_url: String, + creds: Credentials, +} + +impl Client { + pub fn new(creds: Credentials) -> Self { + Self { + cli: reqwest::Client::new(), + base_url: "https://api.patreon.com".into(), + creds: creds, + } + } + + pub async fn campaign(&self) -> Result { + Ok(self + .cli + .get(&format!( + "{}/oauth2/api/current_user/campaigns", + self.base_url + )) + .header( + "Authorization", + format!("Bearer {}", self.creds.access_token), + ) + .send() + .await? + .error_for_status()? + .json() + .await?) + } + + pub async fn pledges(&self, camp_id: u32) -> Result> { + Ok(self + .cli + .get(&format!( + "{}/oauth2/api/campaigns/{}/pledges", + self.base_url, camp_id + )) + .query(&[("include", "patron.null")]) + .header( + "Authorization", + format!("Bearer {}", self.creds.access_token), + ) + .send() + .await? + .error_for_status()? + .json() + .await?) + } +} diff --git a/lib/patreon/src/pledge.rs b/lib/patreon/src/pledge.rs new file mode 100644 index 0000000..dc71d89 --- /dev/null +++ b/lib/patreon/src/pledge.rs @@ -0,0 +1,16 @@ +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 new file mode 100644 index 0000000..598e20b --- /dev/null +++ b/lib/patreon/src/user.rs @@ -0,0 +1,28 @@ +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, +}