[][src]Crate spirit_reqwest

This helps with configuring the reqwest Client.

This is part of the spirit system.

There are two levels of support. The first one is just letting the Spirit to load the ReqwestClient configuration fragment and calling one of its methods to create the Client or others.

The other, more convenient way, is pairing an extractor function with the AtomicClient and letting Spirit keep an up to date version of Client in there at all times.

The split and features

The ReqwestClient lives at the top of the crate. However, reqwest provides both blocking and async flavours of the HTTP client. For that reason, this crate provides two submodules, each with the relevant support (note that the name of the async one is futures, because async is a keyword). The pipeline is configured with the relevant IntoClient transformation and installed into the relevant AtomicClient.

Features enable parts of the functionality here and correspond to some of the features of reqwest. In particular:

Porting from the 0.3 version

Examples

use serde::Deserialize;
use spirit::{Empty, Pipeline, Spirit};
use spirit::prelude::*;
use spirit_reqwest::ReqwestClient;
// Here we choose if we want blocking or async (futures module)
use spirit_reqwest::blocking::{AtomicClient, IntoClient};

#[derive(Debug, Default, Deserialize)]
struct Cfg {
    #[serde(default)]
    client: ReqwestClient,
}

impl Cfg {
    fn client(&self) -> ReqwestClient {
        self.client.clone()
    }
}

fn main() {
    let client = AtomicClient::unconfigured(); // Get a default config before we get configured
    Spirit::<Empty, Cfg>::new()
        .with(
            Pipeline::new("http client")
                .extract_cfg(Cfg::client)
                // Choose if you want blocking or async client
                // (eg. spirit_reqwest::blocking::IntoClient or
                // spirit_reqwest::futures::IntoClient)
                .transform(IntoClient)
                // Choose where to store it
                .install(client.clone())
        )
        .run(move |_| {
            let page = client
                .get("https://www.rust-lang.org")
                .send()?
                .error_for_status()?
                .text()?;
            println!("{}", page);
            Ok(())
        });
}

Modules

blocking

The support for blocking clients.

futures

The support for async clients.

Structs

ReqwestClient

A configuration fragment to configure the reqwest Client