pub struct Hooks { /* private fields */ }Expand description
Lifecycle hooks for the HTTP client.
Implementations§
Source§impl Hooks
impl Hooks
Sourcepub fn on_request<F, Fut>(self, f: F) -> Hookswhere
F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<RequestContext, Error>> + Send + 'static,
pub fn on_request<F, Fut>(self, f: F) -> Hookswhere
F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<RequestContext, Error>> + Send + 'static,
Runs before the transport call. Return Err(Error::hook("…")) to cancel the request.
§Examples
use better_fetch::{ClientBuilder, Error, Hooks, Result};
let hooks = Hooks::new().on_request(|ctx| async move {
if ctx.url.path().contains("blocked") {
return Err(Error::hook("path not allowed"));
}
Ok(ctx)
});
let client = ClientBuilder::new()
.base_url("https://api.example.com")?
.hooks(hooks)
.build()?;Sourcepub fn on_response<F, Fut>(self, f: F) -> Hooks
pub fn on_response<F, Fut>(self, f: F) -> Hooks
Runs after the transport returns. Return Err(Error::hook("…")) to fail the request.
Sourcepub fn on_success<F, Fut>(self, f: F) -> Hooks
pub fn on_success<F, Fut>(self, f: F) -> Hooks
Runs after a successful (2xx) response; cannot abort the pipeline.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Hooks
impl !RefUnwindSafe for Hooks
impl Send for Hooks
impl Sync for Hooks
impl Unpin for Hooks
impl UnsafeUnpin for Hooks
impl !UnwindSafe for Hooks
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more