1use bitcoin::Address;
4use reqwest::{Client, Response};
5use url::Url;
6
7use crate::builder::CKPoolClientBuilder;
8use crate::error::Error;
9use crate::response::UserStats;
10
11#[derive(Debug, Clone)]
13pub struct CKPoolClient {
14 url: Url,
15 client: Client,
16}
17
18impl CKPoolClient {
19 #[inline]
31 pub fn new(url: Url) -> Self {
32 Self::from_client(url, Client::new())
33 }
34
35 #[inline]
37 pub fn builder(url: Url) -> CKPoolClientBuilder {
38 CKPoolClientBuilder::new(url)
39 }
40
41 #[inline]
43 pub fn from_client(url: Url, client: Client) -> Self {
44 Self { client, url }
45 }
46
47 pub async fn user_stats(&self, address: &Address) -> Result<UserStats, Error> {
49 let url: Url = self
50 .url
51 .join("/users/")?
52 .join(address.to_string().as_str())?;
53 let response: Response = self.client.get(url).send().await?;
54
55 match response.error_for_status() {
56 Ok(res) => Ok(res.json().await?),
57 Err(err) => match err.status() {
58 Some(reqwest::StatusCode::NOT_FOUND) => Err(Error::UserNotFound),
59 _ => Err(Error::from(err)),
60 },
61 }
62 }
63}