WIP client submission
This commit is contained in:
parent
265ef7425e
commit
400e1d2465
4 changed files with 33 additions and 20 deletions
|
@ -1,16 +1,18 @@
|
|||
use std::collections::HashMap;
|
||||
use reqwest::header::HeaderMap;
|
||||
use crate::models::{CookieRequest, Submission, SubmissionRequest};
|
||||
use crate::models::{Submission, SubmissionRequest};
|
||||
use anyhow::bail;
|
||||
|
||||
const FA_API_BASE_ORIGIN: &str = "https://furaffinity-api.herokuapp.com";
|
||||
|
||||
pub struct Client {
|
||||
request_client: reqwest::Client,
|
||||
cookies: HashMap<String, String>
|
||||
cookies: HashMap<String, String>,
|
||||
bbcode: bool
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(cookies: HashMap<String, String>) -> anyhow::Result<Self> {
|
||||
pub fn new(cookies: Option<HashMap<String, String>>, bbcode: Option<bool>) -> anyhow::Result<Self> {
|
||||
let headers = HeaderMap::from_iter(vec![
|
||||
(reqwest::header::ACCEPT, "application/json".try_into()?),
|
||||
(reqwest::header::CONTENT_TYPE, "application/json".try_into()?)
|
||||
|
@ -21,22 +23,29 @@ impl Client {
|
|||
|
||||
Ok(Self {
|
||||
request_client,
|
||||
cookies
|
||||
cookies: cookies.unwrap_or_default(),
|
||||
bbcode: bbcode.unwrap_or_default()
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_submission<S>(&mut self, submission_id: S) -> anyhow::Result<Submission> where S: Into<String> {
|
||||
let url = format!("{FA_API_BASE_ORIGIN}/submission/{}", submission_id.into());
|
||||
let url = format!("{FA_API_BASE_ORIGIN}/submission/{}/", submission_id.into());
|
||||
let cookies = self.cookies.clone()
|
||||
.into_iter()
|
||||
.map(|cookie| cookie.into())
|
||||
.collect();
|
||||
|
||||
let res = self.request_client
|
||||
.post(url)
|
||||
.json(&SubmissionRequest {
|
||||
cookies: vec![
|
||||
],
|
||||
bbcode: false
|
||||
cookies,
|
||||
bbcode: self.bbcode.clone()
|
||||
})
|
||||
.send().await?;
|
||||
|
||||
println!("{:?}", res.status());
|
||||
if res.status() != reqwest::StatusCode::OK {
|
||||
bail!(res.status());
|
||||
};
|
||||
//File::create("./out.json")?.write(res.text().await?.as_bytes())?;
|
||||
let submission: Submission = res.json().await?;
|
||||
Ok(submission)
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
use rustyfox::client::Client;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let mut client = Client::new()?;
|
||||
let submission = client.get_submission("59630962").await?;
|
||||
println!("Submission: {:?}", submission);
|
||||
Ok(())
|
||||
}
|
|
@ -12,6 +12,12 @@ impl CookieRequest {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<(String, String)> for CookieRequest {
|
||||
fn from(value: (String, String)) -> Self {
|
||||
Self { name: value.0, value: value.1 }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct SubmissionRequest {
|
||||
pub cookies: Vec<CookieRequest>,
|
||||
|
@ -94,3 +100,10 @@ pub struct Submission {
|
|||
pub favorite: bool,
|
||||
pub favorite_toggle_link: String,
|
||||
}
|
||||
|
||||
/*#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||
#[serde(untagged, rename_all = "lowercase")]
|
||||
pub enum SubmissionType {
|
||||
#[serde(untagged)]
|
||||
Image
|
||||
}*/
|
Loading…
Reference in a new issue