Add function for singular post
This commit is contained in:
parent
88af193dca
commit
9653b8ac53
2 changed files with 47 additions and 4 deletions
|
@ -1,5 +1,5 @@
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use crate::post::{Post, Posts};
|
use crate::post::{Id, Post, RawPost, RawPosts};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use base64::{engine::GeneralPurpose, Engine};
|
use base64::{engine::GeneralPurpose, Engine};
|
||||||
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, USER_AGENT};
|
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, USER_AGENT};
|
||||||
|
@ -89,7 +89,38 @@ impl Client {
|
||||||
debug_file.write_all(text.as_bytes())?;
|
debug_file.write_all(text.as_bytes())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(serde_json::from_str::<Posts>(text.as_str())?.into())
|
Ok(serde_json::from_str::<RawPosts>(text.as_str())?.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a specific post
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
///use r621::prelude::*;
|
||||||
|
///use std::error::Error;
|
||||||
|
///
|
||||||
|
///#[tokio::main]
|
||||||
|
///async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
/// let user_agent = "MyProject/1.0 (by username on e621)";
|
||||||
|
/// let auth = Authentication::Authorized {
|
||||||
|
/// username: "hexerade",
|
||||||
|
/// apikey: "1nHrmzmsvJf26EhU1F7CjnjC"
|
||||||
|
/// };
|
||||||
|
/// let mut esix_client = Client::new(auth, user_agent);
|
||||||
|
/// let post = esix_client.get_post(1337).await?;
|
||||||
|
///}
|
||||||
|
/// ```
|
||||||
|
async fn get_post(&mut self, post_id: Id) -> Result<Post> {
|
||||||
|
self.request_limiter().await;
|
||||||
|
let res = self.get_post_raw(post_id).await?;
|
||||||
|
let text = res.text().await?;
|
||||||
|
|
||||||
|
Ok(serde_json::from_str::<RawPost>(text.as_str())?.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_post_raw(&mut self, post_id: Id) -> Result<Response> {
|
||||||
|
let url = url::Url::parse(format!("{}/posts/{}.json", self.host, post_id).as_str())?;
|
||||||
|
self.request_counter = self.request_counter+1;
|
||||||
|
Ok(self.http_client.get(url.as_str()).send().await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn request_limiter(&mut self) {
|
async fn request_limiter(&mut self) {
|
||||||
|
|
16
src/post.rs
16
src/post.rs
|
@ -3,12 +3,24 @@ pub type FileSize = u64;
|
||||||
pub type ImageDimension = u64;
|
pub type ImageDimension = u64;
|
||||||
|
|
||||||
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
|
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
|
||||||
pub struct Posts {
|
pub struct RawPost {
|
||||||
|
#[serde(alias = "post")]
|
||||||
|
inner: Post,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<Post> for RawPost {
|
||||||
|
fn into(self) -> Post {
|
||||||
|
self.inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
|
||||||
|
pub struct RawPosts {
|
||||||
#[serde(alias = "posts")]
|
#[serde(alias = "posts")]
|
||||||
inner: Vec<Post>,
|
inner: Vec<Post>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<Vec<Post>> for Posts {
|
impl Into<Vec<Post>> for RawPosts {
|
||||||
fn into(self) -> Vec<Post> {
|
fn into(self) -> Vec<Post> {
|
||||||
self.inner
|
self.inner
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue