Skip to main content

ResponseExt

Trait ResponseExt 

Source
pub trait ResponseExt {
    // Required methods
    fn rate_limit_info(&self) -> Option<RateLimitInfo>;
    fn next_page_url(&self) -> Option<String>;
    fn problem_json_or_json<T: DeserializeOwned>(
        self,
    ) -> impl Future<Output = Result<T, ApiError>> + Send;
}
Expand description

Extension methods for reqwest::Response.

Required Methods§

Source

fn rate_limit_info(&self) -> Option<RateLimitInfo>

Parse X-RateLimit-* headers into a RateLimitInfo.

Returns None if the required rate-limit headers are absent or unparseable.

§Example
use api_bones_reqwest::ResponseExt;

let resp = reqwest::get("https://api.example.com/").await?;
if let Some(rl) = resp.rate_limit_info() {
    println!("remaining: {}", rl.remaining);
}
Source

fn next_page_url(&self) -> Option<String>

Parse the RFC 5988 Link: <url>; rel="next" header and return the URL for the next page, if present.

§Example
use api_bones_reqwest::ResponseExt;

let resp = reqwest::get("https://api.example.com/items").await?;
if let Some(next) = resp.next_page_url() {
    println!("next: {next}");
}
Source

fn problem_json_or_json<T: DeserializeOwned>( self, ) -> impl Future<Output = Result<T, ApiError>> + Send

Consume the response, returning the deserialized body.

  • If the status code indicates an error (>= 400) and the Content-Type contains application/problem+json, the body is parsed as an ApiError and returned as Err.
  • Otherwise the body JSON is deserialized into T and returned as Ok.
§Errors

Returns Err(ApiError) for:

  • Problem+JSON error responses (>= 400 with correct content type).
  • Non-Problem+JSON error responses (>= 400).
  • JSON deserialization failures.
  • Network / transport errors from reqwest.
§Example
use api_bones_reqwest::{RequestBuilderExt, ResponseExt};

let resp = reqwest::Client::new()
    .get("https://api.example.com/items")
    .send()
    .await
    .map_err(|e| api_bones::ApiError::bad_request(e.to_string()))?;

let body: serde_json::Value = resp.problem_json_or_json().await?;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ResponseExt for Response

Implementors§