Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

Async client for the 4chan JSON API. Cheap to clone, internal config is Arc-shared. Construct with Client::new for sensible defaults, or Client::with_client to plug in your own reqwest::Client (matching backgrounds/rchan ergonomics).

Implementations§

Source§

impl Client

Source

pub fn new() -> Self

New client with a fresh reqwest::Client and a chan-rs/<ver> User-Agent.

Examples found in repository?
examples/list_boards.rs (line 6)
5async fn main() -> chan::Result<()> {
6    let client = chan::Client::new();
7    let boards = client.get_boards().await?;
8    for b in &boards {
9        let ws = if b.is_ws() { "ws" } else { "nsfw" };
10        println!("/{:>3}/ [{}] {}", b.board, ws, b.title);
11    }
12    println!("\n{} boards.", boards.len());
13    Ok(())
14}
More examples
Hide additional examples
examples/catalog_images.rs (line 9)
7async fn main() -> chan::Result<()> {
8    let board = env::args().nth(1).unwrap_or_else(|| "po".to_string());
9    let client = chan::Client::new();
10    let catalog = client.get_board_catalog(&board).await?;
11
12    let mut count = 0usize;
13    for thread in catalog.threads() {
14        if let Some(att) = &thread.op.attachment {
15            if !att.is_animated() {
16                println!("{}", att.url(&board));
17                count += 1;
18            }
19        }
20        for reply in &thread.last_replies {
21            if let Some(att) = &reply.attachment {
22                if !att.is_animated() {
23                    println!("{}", att.url(&board));
24                    count += 1;
25                }
26            }
27        }
28    }
29    eprintln!("/{}/: {} still-image URLs", board, count);
30    Ok(())
31}
examples/poll_thread.rs (line 23)
15async fn main() -> chan::Result<()> {
16    let mut args = env::args().skip(1);
17    let board = args.next().unwrap_or_else(|| "po".to_string());
18    let thread_no: u64 = args
19        .next()
20        .and_then(|s| s.parse().ok())
21        .expect("usage: poll_thread <board> <thread_no>");
22
23    let client = Client::new();
24    let mut seen: HashSet<u64> = HashSet::new();
25    let mut since: Option<String> = None;
26
27    loop {
28        match client
29            .get_full_thread_if_modified(&board, thread_no, since.as_deref())
30            .await
31        {
32            Ok(None) => {
33                eprintln!("304 Not Modified");
34            }
35            Ok(Some(cond)) => {
36                since = cond.last_modified;
37                for post in &cond.value.posts {
38                    if seen.insert(post.no) {
39                        print_post(&board, post);
40                    }
41                }
42                if cond.value.op().archived || cond.value.op().closed {
43                    eprintln!("Thread archived or closed. Exiting.");
44                    return Ok(());
45                }
46            }
47            Err(chan::Error::NotFound(_)) => {
48                eprintln!("Thread 404'd. Exiting.");
49                return Ok(());
50            }
51            Err(e) => return Err(e),
52        }
53        tokio::time::sleep(Duration::from_secs(30)).await;
54    }
55}
Source

pub fn with_client(http: Client) -> Self

New client reusing an existing reqwest::Client. Lets callers share connection pools and proxy/TLS config across multiple APIs.

Source

pub fn with_api_host(self, host: impl Into<String>) -> Self

Override the API host. Useful for tests or proxies.

Source

pub async fn get_boards(&self) -> Result<Vec<Board>>

GET /boards.json, return every board the API exposes.

Examples found in repository?
examples/list_boards.rs (line 7)
5async fn main() -> chan::Result<()> {
6    let client = chan::Client::new();
7    let boards = client.get_boards().await?;
8    for b in &boards {
9        let ws = if b.is_ws() { "ws" } else { "nsfw" };
10        println!("/{:>3}/ [{}] {}", b.board, ws, b.title);
11    }
12    println!("\n{} boards.", boards.len());
13    Ok(())
14}
Source

pub async fn get_board_catalog(&self, board: &str) -> Result<Catalog>

GET /{board}/catalog.json, every thread on the board, with up to 5 reply previews per thread.

Examples found in repository?
examples/catalog_images.rs (line 10)
7async fn main() -> chan::Result<()> {
8    let board = env::args().nth(1).unwrap_or_else(|| "po".to_string());
9    let client = chan::Client::new();
10    let catalog = client.get_board_catalog(&board).await?;
11
12    let mut count = 0usize;
13    for thread in catalog.threads() {
14        if let Some(att) = &thread.op.attachment {
15            if !att.is_animated() {
16                println!("{}", att.url(&board));
17                count += 1;
18            }
19        }
20        for reply in &thread.last_replies {
21            if let Some(att) = &reply.attachment {
22                if !att.is_animated() {
23                    println!("{}", att.url(&board));
24                    count += 1;
25                }
26            }
27        }
28    }
29    eprintln!("/{}/: {} still-image URLs", board, count);
30    Ok(())
31}
Source

pub async fn get_threads(&self, board: &str) -> Result<ThreadList>

GET /{board}/threads.json, a lightweight per-thread last_modified summary

Source

pub async fn get_archive(&self, board: &str) -> Result<Archive>

GET /{board}/archive.json, archived OP numbers, oldest first. Errors with Error::NotFound on boards that have no archive.

Source

pub async fn get_index_page(&self, board: &str, page: u8) -> Result<IndexPage>

GET /{board}/{page}.json, one index page (1.=15) with full thread previews.

Source

pub async fn get_full_thread( &self, board: &str, thread_no: u64, ) -> Result<Thread>

GET /{board}/thread/{no}.json, every post in a thread.

Source

pub async fn get_threads_if_modified( &self, board: &str, since: Option<&str>, ) -> Result<Option<Conditional<ThreadList>>>

Source

pub async fn get_catalog_if_modified( &self, board: &str, since: Option<&str>, ) -> Result<Option<Conditional<Catalog>>>

Source

pub async fn get_full_thread_if_modified( &self, board: &str, thread_no: u64, since: Option<&str>, ) -> Result<Option<Conditional<Thread>>>

Examples found in repository?
examples/poll_thread.rs (line 29)
15async fn main() -> chan::Result<()> {
16    let mut args = env::args().skip(1);
17    let board = args.next().unwrap_or_else(|| "po".to_string());
18    let thread_no: u64 = args
19        .next()
20        .and_then(|s| s.parse().ok())
21        .expect("usage: poll_thread <board> <thread_no>");
22
23    let client = Client::new();
24    let mut seen: HashSet<u64> = HashSet::new();
25    let mut since: Option<String> = None;
26
27    loop {
28        match client
29            .get_full_thread_if_modified(&board, thread_no, since.as_deref())
30            .await
31        {
32            Ok(None) => {
33                eprintln!("304 Not Modified");
34            }
35            Ok(Some(cond)) => {
36                since = cond.last_modified;
37                for post in &cond.value.posts {
38                    if seen.insert(post.no) {
39                        print_post(&board, post);
40                    }
41                }
42                if cond.value.op().archived || cond.value.op().closed {
43                    eprintln!("Thread archived or closed. Exiting.");
44                    return Ok(());
45                }
46            }
47            Err(chan::Error::NotFound(_)) => {
48                eprintln!("Thread 404'd. Exiting.");
49                return Ok(());
50            }
51            Err(e) => return Err(e),
52        }
53        tokio::time::sleep(Duration::from_secs(30)).await;
54    }
55}

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Client

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more