Skip to main content

RequestOptions

Struct RequestOptions 

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

Extra headers and query parameters, and another base URL, for one call, applied after everything the generated code sets.

A generated operation takes them with .options(&options) (or its last argument without builders); RequestBuilder::options applies them to a request of your own. One value can be reused for many calls.

  • A header replaces every value the request has under the same name: generated headers, authentication and Content-Type included. A name set twice on the options keeps both values (see RequestOptions::headers).
  • Query parameters are appended after the generated ones; a name the request already has is sent twice.
  • A base URL sends the call to another host or path prefix: it replaces the Configuration.base_path the request was built on, and the operation’s own path and query are kept.

They win over a ProviderLayer: it does not replace a header or a query parameter the options set, and it does not move a call with a base URL of its own to the provider (see its docs).

use timvw_rust_runtime::RequestOptions;

let options = RequestOptions::new()
    .header("x-request-source", "batch")?
    .query(&[("trace", "1")])?;
assert_eq!(options.header_map()["x-request-source"], "batch");
assert_eq!(options.query_pairs(), [("trace".to_owned(), "1".to_owned())]);

Implementations§

Source§

impl RequestOptions

Source

pub fn new() -> Self

No extra headers or query parameters.

Source

pub fn header<K, V>(self, name: K, value: V) -> Result<Self, Error>

Sets a header, replacing a value set before under the same name. Fails, with an error whose is_builder() is true, on an invalid name or value.

Source

pub fn headers(self, headers: HeaderMap) -> Self

Adds every header of a map, replacing the values set before under its names. A name with several values in headers keeps them all, and so does the request they are applied to.

Source

pub fn query<T: Serialize + ?Sized>(self, query: &T) -> Result<Self, Error>

Appends query parameters, serialized as application/x-www-form-urlencoded: a sequence of pairs, a map or a struct, as RequestBuilder::query takes them. Fails, with an error whose is_builder() is true, on a value that is not a flat list of pairs.

Source

pub fn base_url(self, base_url: &str) -> Result<Self, Error>

Sends the call to another base URL, such as https://eu.api.example.com/v1: the scheme, host, port and path prefix of the request’s base (a generated client’s Configuration.base_path, declared with RequestBuilder::base) are replaced, and the operation’s path and the query are kept. On a request without a declared base, the whole path is kept and put after this base’s path. Calling it again replaces the base. Fails, with an error whose is_builder() is true, on a URL that is not absolute or that has a query or fragment.

use timvw_rust_runtime::{Client, Method, RequestOptions};

let options = RequestOptions::new().base_url("https://eu.api.example.com/v2")?;
let request = Client::default()
    .request(Method::GET, "https://api.example.com/v1/models?limit=2")
    .base("https://api.example.com/v1")
    .options(&options)
    .build()?;
assert_eq!(request.uri(), "https://eu.api.example.com/v2/models?limit=2");
Source

pub fn base(&self) -> Option<&str>

The base URL set with RequestOptions::base_url, if any.

Source

pub fn header_map(&self) -> &HeaderMap

The headers.

Source

pub fn query_pairs(&self) -> &[(String, String)]

The query parameters, decoded, in order.

Source

pub fn is_empty(&self) -> bool

Whether there are no headers, no query parameters and no base URL.

Trait Implementations§

Source§

impl Clone for RequestOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RequestOptions

Source§

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

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

impl Default for RequestOptions

Source§

fn default() -> Self

Returns the “default value” for a type. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSync for T
where T: Sync + ?Sized,

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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