fix patreon client
This commit is contained in:
parent
0956fa9a48
commit
db34860258
|
@ -1269,11 +1269,15 @@ name = "patreon"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
|
"envy",
|
||||||
"jsonapi",
|
"jsonapi",
|
||||||
|
"log 0.3.9",
|
||||||
|
"pretty_env_logger",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -23,7 +23,6 @@ pretty_env_logger = "0"
|
||||||
prometheus = { version = "0.9", default-features = false, features = ["process"] }
|
prometheus = { version = "0.9", default-features = false, features = ["process"] }
|
||||||
rand = "0"
|
rand = "0"
|
||||||
rss = "1"
|
rss = "1"
|
||||||
ructe = "0.11"
|
|
||||||
serde_dhall = "0.5.3"
|
serde_dhall = "0.5.3"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_yaml = "0.8"
|
serde_yaml = "0.8"
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
.env
|
|
@ -13,3 +13,9 @@ reqwest = { version = "0.10", features = ["json"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
|
log = "0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tokio = { version = "0.2", features = ["macros"] }
|
||||||
|
envy = "0.4"
|
||||||
|
pretty_env_logger = "0"
|
|
@ -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(())
|
||||||
|
}
|
|
@ -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<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,
|
|
||||||
}
|
|
|
@ -1,21 +1,68 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate jsonapi;
|
extern crate jsonapi;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
|
|
||||||
use jsonapi::api::*;
|
|
||||||
use jsonapi::model::*;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
use chrono::prelude::*;
|
||||||
|
|
||||||
mod campaign;
|
pub type Campaigns = Vec<Object<Campaign>>;
|
||||||
mod pledge;
|
pub type Pledges = Vec<Object<Pledge>>;
|
||||||
mod user;
|
|
||||||
pub use campaign::Campaign;
|
|
||||||
pub use pledge::Pledge;
|
|
||||||
pub use user::User;
|
|
||||||
|
|
||||||
jsonapi_model!(Campaign; "campaign");
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
jsonapi_model!(Pledge; "pledge");
|
pub struct Campaign {
|
||||||
jsonapi_model!(User; "user");
|
pub summary: String,
|
||||||
|
pub creation_name: String,
|
||||||
|
pub display_patron_goals: bool,
|
||||||
|
pub pay_per_name: String,
|
||||||
|
pub one_liner: Option<String>,
|
||||||
|
pub main_video_embed: Option<String>,
|
||||||
|
pub main_video_url: Option<String>,
|
||||||
|
pub image_small_url: String,
|
||||||
|
pub image_url: String,
|
||||||
|
pub thanks_video_url: Option<String>,
|
||||||
|
pub thanks_embed: Option<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_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<String>,
|
||||||
|
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>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct User {
|
||||||
|
pub first_name: String,
|
||||||
|
pub last_name: String,
|
||||||
|
pub full_name: String,
|
||||||
|
pub vanity: Option<String>,
|
||||||
|
pub about: Option<String>,
|
||||||
|
pub gender: i32,
|
||||||
|
pub image_url: String,
|
||||||
|
pub thumb_url: String,
|
||||||
|
pub created: DateTime<Utc>,
|
||||||
|
pub url: String,
|
||||||
|
}
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
@ -41,6 +88,25 @@ pub struct Client {
|
||||||
creds: Credentials,
|
creds: Credentials,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct Data<T, U> {
|
||||||
|
pub data: T,
|
||||||
|
pub included: Option<Vec<U>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct Object<T> {
|
||||||
|
pub id: String,
|
||||||
|
pub attributes: T,
|
||||||
|
pub r#type: String,
|
||||||
|
pub links: Option<Links>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct Links {
|
||||||
|
related: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
pub fn new(creds: Credentials) -> Self {
|
pub fn new(creds: Credentials) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -50,26 +116,27 @@ impl Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn campaign(&self) -> Result<Campaign> {
|
pub async fn campaign(&self) -> Result<Data<Vec<Object<Campaign>>, ()>> {
|
||||||
Ok(self
|
let data = self
|
||||||
.cli
|
.cli
|
||||||
.get(&format!(
|
.get(&format!(
|
||||||
"{}/oauth2/api/current_user/campaigns",
|
"{}/oauth2/api/current_user/campaigns",
|
||||||
self.base_url
|
self.base_url
|
||||||
))
|
))
|
||||||
|
.query(&[("include", "patron.null"), ("includes", "")])
|
||||||
.header(
|
.header(
|
||||||
"Authorization",
|
"Authorization",
|
||||||
format!("Bearer {}", self.creds.access_token),
|
format!("Bearer {}", self.creds.access_token),
|
||||||
)
|
)
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
.error_for_status()?
|
.error_for_status()?.text().await?;
|
||||||
.json()
|
log::debug!("campaign response: {}", data);
|
||||||
.await?)
|
Ok(serde_json::from_str(&data)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn pledges(&self, camp_id: u32) -> Result<Vec<Pledge>> {
|
pub async fn pledges(&self, camp_id: String) -> Result<Vec<Object<User>>> {
|
||||||
Ok(self
|
let data = self
|
||||||
.cli
|
.cli
|
||||||
.get(&format!(
|
.get(&format!(
|
||||||
"{}/oauth2/api/campaigns/{}/pledges",
|
"{}/oauth2/api/campaigns/{}/pledges",
|
||||||
|
@ -83,7 +150,10 @@ impl Client {
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
.error_for_status()?
|
.error_for_status()?
|
||||||
.json()
|
.text()
|
||||||
.await?)
|
.await?;
|
||||||
|
log::debug!("pledges for {}: {}", camp_id, data);
|
||||||
|
let data : Data<Vec<Object<Pledge>>, Object<User>> = serde_json::from_str(&data)?;
|
||||||
|
Ok(data.included.unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<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>,
|
|
||||||
}
|
|
|
@ -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<Utc>,
|
|
||||||
pub url: String,
|
|
||||||
pub discord_id: String,
|
|
||||||
}
|
|
Loading…
Reference in New Issue