APIRequestBuilder

Struct APIRequestBuilder 

Source
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

Source

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?;
Source

pub fn headers( self, headers: impl IntoIterator<Item = (String, String)>, ) -> Self

Add multiple headers to the request.

Source

pub fn query<K, V>(self, params: &[(K, V)]) -> Self
where K: AsRef<str>, V: AsRef<str>,

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=1
Source

pub fn query_param( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

Add a single query parameter.

Source

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?;
Source

pub fn form<K, V>(self, data: &[(K, V)]) -> Self
where K: AsRef<str>, V: AsRef<str>,

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?;
Source

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?;
Source

pub fn body(self, data: Vec<u8>) -> Self

Set the request body as raw bytes.

Source

pub fn text(self, data: impl Into<String>) -> Self

Set the request body as text.

Source

pub fn timeout(self, timeout: Duration) -> Self

Set the request timeout.

This overrides the default timeout set on the API context.

§Example
use std::time::Duration;

let response = api.get("https://slow-api.example.com/data")
    .timeout(Duration::from_secs(60))
    .send()
    .await?;
Source

pub async fn send(self) -> Result<APIResponse, APIError>

Send the request and return the response.

§Errors

Returns an error if:

  • The context has been disposed
  • The URL is invalid
  • The request fails
  • A timeout occurs

Trait Implementations§

Source§

impl Debug for APIRequestBuilder

Source§

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

Formats the value using the given formatter. Read more
Source§

impl IntoFuture for APIRequestBuilder

Source§

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>>

Which kind of future are we turning this into?
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. 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