Crate mempool_space_api

Crate mempool_space_api 

Source
Expand description

mempool_space_api

§mempool_space_api

§Getting started

use bytes::Bytes;
use mempool_space_api::AsyncClient;
use mempool_space_api::{Http, HttpMethod};

// Define a HTTP client implementation.

#[derive(Debug)]
struct MyClient {
    inner: reqwest::Client,
}

// Implement `Http` for `MyClient`.

impl Http for MyClient {
    type Body = Bytes;
    type Err = reqwest::Error;

    async fn send<'a>(
        &'a self,
        method: HttpMethod,
        url: &'a str,
        body: impl Into<Self::Body>,
    ) -> Result<Self::Body, Self::Err>
    where
        Self: 'a,
    {
        let resp = match method {
            HttpMethod::GET => self.inner.get(url).send().await?,
            HttpMethod::POST => self.inner.post(url).body(body.into()).send().await?,
        };

        resp.bytes().await
    }
}

// Start making API requests.

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let my_client = MyClient {
        inner: reqwest::Client::new(),
    };
    let client = AsyncClient::new("https://mempool.space/api", &my_client);

    // Get recommended fees.
    let res = client.get_recommended_fees().await?;
    println!("{res:#?}");

    Ok(())
}

§Features

  • bitreq: An async HTTP client that can be used with this library out of the box.

Re-exports§

pub extern crate bitreq;
pub extern crate tokio;

Modules§

api
api.

Structs§

AsyncClient
Async client that is generic over the Http implementation.
BitreqClient
HTTP client implementation.
BitreqClientBuilder
Builder struct for BitreqClient.
HttpMethod
HTTP method.

Enums§

BitreqError
Error for BitreqClient
Error
Errors that can occur in this library.

Traits§

Http
Trait describing the behavior required of the HTTP client.