Add documentation and prelude
This commit is contained in:
parent
7cf1c857f2
commit
88af193dca
3 changed files with 63 additions and 22 deletions
|
@ -17,6 +17,23 @@ pub struct Client {
|
|||
}
|
||||
|
||||
impl Client {
|
||||
|
||||
/// Create a new e621 client.
|
||||
///
|
||||
/// ```
|
||||
///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);
|
||||
///}
|
||||
/// ```
|
||||
pub fn new(auth: Authentication, useragent: &str) -> Result<Self> {
|
||||
let mut header_map = HeaderMap::new();
|
||||
header_map.append(USER_AGENT, HeaderValue::from_str(useragent)?);
|
||||
|
@ -37,6 +54,44 @@ impl Client {
|
|||
})
|
||||
}
|
||||
|
||||
/// Query the e621 api for posts
|
||||
///
|
||||
/// ```
|
||||
///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 posts = esix_client.list_posts(None, Some("lucario"), None).await?;
|
||||
///}
|
||||
/// ```
|
||||
pub async fn list_posts(
|
||||
&mut self,
|
||||
limit: Option<u16>,
|
||||
tags: Option<String>,
|
||||
page: Option<u32>,
|
||||
) -> Result<Vec<Post>> {
|
||||
self.request_limiter().await;
|
||||
let res = self.list_posts_raw(limit, tags, page).await?;
|
||||
let text = res.text().await?;
|
||||
|
||||
if cfg!(debug) {
|
||||
let mut debug_file = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open("/tmp/debug.json")?;
|
||||
debug_file.write_all(text.as_bytes())?;
|
||||
}
|
||||
|
||||
Ok(serde_json::from_str::<Posts>(text.as_str())?.into())
|
||||
}
|
||||
|
||||
async fn request_limiter(&mut self) {
|
||||
let wait_time = Instant::now() - self.last_request_time;
|
||||
if Instant::now() - self.last_request_time > Duration::from_secs(1) {
|
||||
|
@ -86,27 +141,6 @@ impl Client {
|
|||
self.request_counter = self.request_counter+1;
|
||||
Ok(self.http_client.get(url.as_str()).send().await?)
|
||||
}
|
||||
|
||||
pub async fn list_posts(
|
||||
&mut self,
|
||||
limit: Option<u16>,
|
||||
tags: Option<String>,
|
||||
page: Option<u32>,
|
||||
) -> Result<Vec<Post>> {
|
||||
self.request_limiter().await;
|
||||
let res = self.list_posts_raw(limit, tags, page).await?;
|
||||
let text = res.text().await?;
|
||||
|
||||
if cfg!(debug) {
|
||||
let mut debug_file = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open("/tmp/debug.json")?;
|
||||
debug_file.write_all(text.as_bytes())?;
|
||||
}
|
||||
|
||||
Ok(serde_json::from_str::<Posts>(text.as_str())?.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
|
|
|
@ -1,3 +1,10 @@
|
|||
pub mod client;
|
||||
mod errors;
|
||||
pub mod errors;
|
||||
pub mod post;
|
||||
pub mod query;
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::client::Client;
|
||||
pub use crate::client::Authentication;
|
||||
pub use crate::post::Post;
|
||||
}
|
||||
|
|
0
src/query.rs
Normal file
0
src/query.rs
Normal file
Loading…
Reference in a new issue