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-Typeincluded. A name set twice on the options keeps both values (seeRequestOptions::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_paththe 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
impl RequestOptions
Sourcepub fn header<K, V>(self, name: K, value: V) -> Result<Self, Error>where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<Error>,
pub fn header<K, V>(self, name: K, value: V) -> Result<Self, Error>where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<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.
Sourcepub fn headers(self, headers: HeaderMap) -> Self
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.
Sourcepub fn query<T: Serialize + ?Sized>(self, query: &T) -> Result<Self, Error>
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.
Sourcepub fn base_url(self, base_url: &str) -> Result<Self, Error>
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");Sourcepub fn base(&self) -> Option<&str>
pub fn base(&self) -> Option<&str>
The base URL set with RequestOptions::base_url, if any.
Sourcepub fn header_map(&self) -> &HeaderMap
pub fn header_map(&self) -> &HeaderMap
The headers.
Sourcepub fn query_pairs(&self) -> &[(String, String)]
pub fn query_pairs(&self) -> &[(String, String)]
The query parameters, decoded, in order.