start basic patreon client

This commit is contained in:
Cadey Ratio 2020-07-16 08:42:07 -04:00
parent b110623ae8
commit b4ac823611
5 changed files with 178 additions and 0 deletions

15
lib/patreon/Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "patreon"
version = "0.1.0"
authors = ["Christine Dodrill <me@christine.website>"]
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"

View File

@ -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<Utc>,
pub published_at: DateTime<Utc>,
pub pledge_url: String,
pub pledge_sum: i32,
pub patron_count: u32,
pub creation_coult: u32,
pub outstanding_payment_amount_cents: u64,
}

89
lib/patreon/src/lib.rs Normal file
View File

@ -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<T> = std::result::Result<T, Error>;
#[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<Campaign> {
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<Vec<Pledge>> {
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?)
}
}

16
lib/patreon/src/pledge.rs Normal file
View File

@ -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<Utc>,
pub declined_since: DateTime<Utc>,
pub pledge_cap_cents: u32,
pub patron_pays_fees: bool,
pub total_historical_amount_cents: Option<u32>,
pub is_paused: Option<bool>,
pub has_shipping_address: Option<bool>,
pub outstanding_payment_amount_cents: Option<u32>,
}

28
lib/patreon/src/user.rs Normal file
View File

@ -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<Utc>,
pub url: String,
pub discord_id: String,
}