pub struct APIRequestBuilder { /* private fields */ }Expand description
Builder for constructing and sending HTTP requests.
This builder provides a fluent API for configuring request options like headers, body, query parameters, and timeout.
§Example
use viewpoint_core::api::{APIRequestContext, APIContextOptions};
let api = APIRequestContext::new(APIContextOptions::new()).await?;
// Simple GET request
let response = api.get("https://api.example.com/users").send().await?;
// POST with JSON body
let user = serde_json::json!({ "name": "John" });
let response = api.post("https://api.example.com/users")
.json(&user)
.header("X-Custom", "value")
.send()
.await?;Implementations§
Source§impl APIRequestBuilder
impl APIRequestBuilder
Sourcepub fn header(self, name: impl Into<String>, value: impl Into<String>) -> Self
pub fn header(self, name: impl Into<String>, value: impl Into<String>) -> Self
Add a header to the request.
§Example
let response = api.get("https://api.example.com/data")
.header("Authorization", "Bearer token")
.header("Accept", "application/json")
.send()
.await?;Sourcepub fn headers(
self,
headers: impl IntoIterator<Item = (String, String)>,
) -> Self
pub fn headers( self, headers: impl IntoIterator<Item = (String, String)>, ) -> Self
Add multiple headers to the request.
Sourcepub fn query<K, V>(self, params: &[(K, V)]) -> Self
pub fn query<K, V>(self, params: &[(K, V)]) -> Self
Add query parameters to the request URL.
§Example
let response = api.get("https://api.example.com/search")
.query(&[("q", "rust"), ("page", "1")])
.send()
.await?;
// Request URL: https://api.example.com/search?q=rust&page=1Sourcepub fn query_param(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn query_param( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
Add a single query parameter.
Sourcepub fn json<T: Serialize>(self, data: &T) -> Self
pub fn json<T: Serialize>(self, data: &T) -> Self
Set the request body as JSON.
This will also set the Content-Type header to application/json.
§Example
let user = serde_json::json!({
"name": "John",
"email": "john@example.com"
});
let response = api.post("https://api.example.com/users")
.json(&user)
.send()
.await?;Sourcepub fn form<K, V>(self, data: &[(K, V)]) -> Self
pub fn form<K, V>(self, data: &[(K, V)]) -> Self
Set the request body as form-urlencoded data.
This will also set the Content-Type header to application/x-www-form-urlencoded.
§Example
let response = api.post("https://api.example.com/login")
.form(&[("username", "john"), ("password", "secret")])
.send()
.await?;Sourcepub fn multipart(self, fields: Vec<MultipartField>) -> Self
pub fn multipart(self, fields: Vec<MultipartField>) -> Self
Set the request body as multipart form data.
This is used for file uploads.
§Example
let file_content = vec![1, 2, 3, 4]; // or std::fs::read("document.pdf")
let response = api.post("https://api.example.com/upload")
.multipart(vec![
MultipartField::text("description", "My document"),
MultipartField::file("file", "document.pdf", file_content)
.content_type("application/pdf"),
])
.send()
.await?;Trait Implementations§
Source§impl Debug for APIRequestBuilder
impl Debug for APIRequestBuilder
Source§impl IntoFuture for APIRequestBuilder
impl IntoFuture for APIRequestBuilder
Source§type Output = Result<APIResponse, APIError>
type Output = Result<APIResponse, APIError>
The output that the future will produce on completion.
Source§type IntoFuture = Pin<Box<dyn Future<Output = <APIRequestBuilder as IntoFuture>::Output> + Send>>
type IntoFuture = Pin<Box<dyn Future<Output = <APIRequestBuilder as IntoFuture>::Output> + Send>>
Which kind of future are we turning this into?
Source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
Creates a future from a value. Read more
Auto Trait Implementations§
impl Freeze for APIRequestBuilder
impl !RefUnwindSafe for APIRequestBuilder
impl Send for APIRequestBuilder
impl Sync for APIRequestBuilder
impl Unpin for APIRequestBuilder
impl !UnwindSafe for APIRequestBuilder
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