APIResponse

Struct APIResponse 

Source
pub struct APIResponse { /* private fields */ }
Expand description

Response from an API request.

This struct wraps a reqwest response and provides convenient methods for extracting the response body in various formats.

§Example

use viewpoint_core::api::{APIRequestContext, APIContextOptions};

let api = APIRequestContext::new(APIContextOptions::new()).await?;
let response = api.get("https://api.example.com/users").send().await?;

// Check status
if response.ok() {
    // Parse JSON
    let data: serde_json::Value = response.json().await?;
    println!("Got data: {:?}", data);
}

Implementations§

Source§

impl APIResponse

Source

pub fn status(&self) -> u16

Get the HTTP status code.

§Example
let response = api.get("https://api.example.com/users").send().await?;
println!("Status: {}", response.status()); // e.g., 200
Source

pub fn status_code(&self) -> StatusCode

Get the HTTP status code as a reqwest::StatusCode.

Source

pub fn ok(&self) -> bool

Check if the response was successful (status code 2xx).

§Example
let response = api.get("https://api.example.com/users").send().await?;
if response.ok() {
    println!("Request succeeded!");
}
Source

pub fn status_text(&self) -> &str

Get the status text (reason phrase).

Source

pub fn headers(&self) -> &HeaderMap

Get the response headers.

§Example
let response = api.get("https://api.example.com/users").send().await?;
let headers = response.headers();
if let Some(content_type) = headers.get("content-type") {
    println!("Content-Type: {:?}", content_type);
}
Source

pub fn headers_map(&self) -> HashMap<String, String>

Get response headers as a HashMap.

Source

pub fn header(&self, name: &str) -> Option<&str>

Get a specific header value.

Source

pub fn url(&self) -> &str

Get the final URL after any redirects.

Source

pub async fn json<T: DeserializeOwned>(self) -> Result<T, APIError>

Parse the response body as JSON.

§Errors

Returns an error if the response body cannot be parsed as JSON.

§Example
use serde::Deserialize;

#[derive(Deserialize)]
struct User {
    id: i32,
    name: String,
}

let response = api.get("https://api.example.com/users/1").send().await?;
let user: User = response.json().await?;
println!("User: {} (id={})", user.name, user.id);
Source

pub async fn text(self) -> Result<String, APIError>

Get the response body as text.

§Errors

Returns an error if the response body cannot be read as text.

§Example
let response = api.get("https://example.com").send().await?;
let html = response.text().await?;
println!("HTML length: {} bytes", html.len());
Source

pub async fn body(self) -> Result<Bytes, APIError>

Get the response body as raw bytes.

§Errors

Returns an error if the response body cannot be read.

§Example
let response = api.get("https://example.com/image.png").send().await?;
let bytes = response.body().await?;
std::fs::write("image.png", &bytes).expect("Failed to write file");
Source

pub fn content_length(&self) -> Option<u64>

Get the content length if known.

Source

pub fn is_redirect(&self) -> bool

Check if the response indicates a redirect.

Source

pub fn is_client_error(&self) -> bool

Check if the response indicates a client error (4xx).

Source

pub fn is_server_error(&self) -> bool

Check if the response indicates a server error (5xx).

Trait Implementations§

Source§

impl Debug for APIResponse

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more