HTTP via reqwest (id_effect_platform::http::reqwest)
The id_effect_platform::http::reqwest module integrates reqwest::Client with the
effect environment: the client lives in R as the ReqwestClient capability, and HTTP work
is expressed as Effect values (send, text, bytes, json, …).
Relation to portable HTTP
| Use | Module |
|---|---|
Portable HttpClient trait, swap implementations in tests, minimal request/response model | id_effect_platform::http (HttpClientService, execute, …) |
Rich reqwest surface (RequestBuilder, redirects, pools), JSON + Schema decoding | id_effect_platform::http::reqwest |
Prefer id_effect_platform::http for new application boundaries that should stay
stack-agnostic. Use id_effect_platform::http::reqwest when you depend on RequestBuilder
pipelines, need pooled clients (provide_reqwest_pool, send_pooled), or want
json_schema so parse failures carry field paths (id_effect::schema::ParseError).
Core pieces
ReqwestClient(pub reqwest::Client)— the capability;ReqwestClientEnvis its one-element list.ReqwestClientLive::layer()/layer_with(config)— layers that build the client at the composition root from anHttpClientConfig(timeouts, no redirects, the SSRF resolver check; see client defaults).dangerous_provide_reqwest_client(client)supplies your own client unguarded, and records the opt-out.send,text,bytes,json,json_schema— effects over aFnOnce(&Client) -> RequestBuilder;get/post/put/delete/head/patchtake a URL.- Pools —
provide_reqwest_pool(n, ttl)provides aReqwestPool(id_effect::Poolof clients);send_pooledborrows one per request.
#![allow(unused)] fn main() { use id_effect::Exit; use id_effect_platform::http::reqwest::{ReqwestClientEnv, ReqwestClientLive, text}; use id_effect_tokio::TokioRuntime; use reqwest::Error; // Needs the network, so it is compiled but not run. async fn fetch(url: &'static str) -> Result<String, std::io::Error> { let program = text::<String, Error, ReqwestClientEnv, _>(move |c| c.get(url)) .provide_layer(ReqwestClientLive::layer()); let Exit::Success(body) = TokioRuntime::current()?.run(program).await else { panic!("request failed") }; Ok(body) } }
Import reqwest types (Client, Error, …) from the reqwest crate and the helpers from
id_effect_platform::http::reqwest. Like every live platform effect these need a Tokio runtime
with its IO driver (Tokio bridge).
Further reading
- Examples:
030_layer_default_env,010_wiremock_get_text,020_wiremock_jsonincrates/id_effect_platform/examples - Crate-level docs:
cargo doc --open -p id_effect_platform - Migrating from async for the general
async fn→Effectmove.