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
impl APIResponse
Sourcepub fn status(&self) -> u16
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., 200Sourcepub fn status_code(&self) -> StatusCode
pub fn status_code(&self) -> StatusCode
Get the HTTP status code as a reqwest::StatusCode.
Sourcepub fn ok(&self) -> bool
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!");
}Sourcepub fn status_text(&self) -> &str
pub fn status_text(&self) -> &str
Get the status text (reason phrase).
Sourcepub fn headers(&self) -> &HeaderMap
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);
}Sourcepub fn headers_map(&self) -> HashMap<String, String>
pub fn headers_map(&self) -> HashMap<String, String>
Get response headers as a HashMap.
Sourcepub async fn json<T: DeserializeOwned>(self) -> Result<T, APIError>
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);Sourcepub fn content_length(&self) -> Option<u64>
pub fn content_length(&self) -> Option<u64>
Get the content length if known.
Sourcepub fn is_redirect(&self) -> bool
pub fn is_redirect(&self) -> bool
Check if the response indicates a redirect.
Sourcepub fn is_client_error(&self) -> bool
pub fn is_client_error(&self) -> bool
Check if the response indicates a client error (4xx).
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Check if the response indicates a server error (5xx).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for APIResponse
impl !RefUnwindSafe for APIResponse
impl Send for APIResponse
impl Sync for APIResponse
impl Unpin for APIResponse
impl !UnwindSafe for APIResponse
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