Struct osauth::ServiceRequestBuilder
source · pub struct ServiceRequestBuilder<S: ServiceType> { /* private fields */ }
Expand description
A request builder for a service.
Implementations§
source§impl<S> ServiceRequestBuilder<S>where
S: ServiceType,
impl<S> ServiceRequestBuilder<S>where S: ServiceType,
sourcepub fn client(&self) -> &AuthenticatedClient
pub fn client(&self) -> &AuthenticatedClient
Get a reference to the client.
sourcepub fn body<T: Into<Body>>(self, body: T) -> ServiceRequestBuilder<S>
pub fn body<T: Into<Body>>(self, body: T) -> ServiceRequestBuilder<S>
Add a body to the request.
sourcepub fn header<K, V>(self, key: K, value: V) -> ServiceRequestBuilder<S>where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
pub fn header<K, V>(self, key: K, value: V) -> ServiceRequestBuilder<S>where HeaderName: TryFrom<K>, <HeaderName as TryFrom<K>>::Error: Into<HttpError>, HeaderValue: TryFrom<V>, <HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
Add a header to the request.
sourcepub fn headers(self, headers: HeaderMap) -> ServiceRequestBuilder<S>
pub fn headers(self, headers: HeaderMap) -> ServiceRequestBuilder<S>
Add headers to a request.
sourcepub fn json<T: Serialize + ?Sized>(self, json: &T) -> ServiceRequestBuilder<S>
pub fn json<T: Serialize + ?Sized>(self, json: &T) -> ServiceRequestBuilder<S>
Add a JSON body to the request.
sourcepub fn query<T: Serialize + ?Sized>(self, query: &T) -> ServiceRequestBuilder<S>
pub fn query<T: Serialize + ?Sized>(self, query: &T) -> ServiceRequestBuilder<S>
Send a query with the request.
Query objects can be used for type-safe querying without creating a potentially large structure with all possible filters:
use std::borrow::Cow;
#[derive(Debug)]
enum NodeFilter {
Associated(bool),
ResourceClass(String),
Limit(usize),
// ... a lot of items here ...
}
impl osauth::QueryItem for NodeFilter {
fn query_item(&self) -> Result<(&str, Cow<str>), osauth::Error> {
Ok(match self {
NodeFilter::Associated(a) => ("associated", a.to_string().into()),
NodeFilter::ResourceClass(s) => ("resource_class", s.into()),
NodeFilter::Limit(l) => ("limit", l.to_string().into()),
})
}
}
let session = osauth::Session::from_env().await?;
let query = osauth::Query::default()
.with(NodeFilter::Associated(true))
.with(NodeFilter::ResourceClass("x1.large".into()))
.with(NodeFilter::Limit(100));
let request = session
.get(osauth::services::BAREMETAL, &["nodes"])
.query(&query);
sourcepub fn timeout(self, timeout: Duration) -> ServiceRequestBuilder<S>
pub fn timeout(self, timeout: Duration) -> ServiceRequestBuilder<S>
Override the timeout for the request.
sourcepub async fn fetch<T>(self) -> Result<T, Error>where
T: DeserializeOwned + Send,
S: Send,
pub async fn fetch<T>(self) -> Result<T, Error>where T: DeserializeOwned + Send, S: Send,
Send the request and receive JSON in response.
source§impl<S> ServiceRequestBuilder<S>where
S: VersionedService,
impl<S> ServiceRequestBuilder<S>where S: VersionedService,
sourcepub fn api_version<A: Into<ApiVersion>>(
self,
version: A
) -> ServiceRequestBuilder<S>
pub fn api_version<A: Into<ApiVersion>>( self, version: A ) -> ServiceRequestBuilder<S>
Add an API version to this request.
sourcepub fn set_api_version<A: Into<ApiVersion>>(&mut self, version: A)
pub fn set_api_version<A: Into<ApiVersion>>(&mut self, version: A)
Set the API version on the request.
source§impl<S> ServiceRequestBuilder<S>where
S: ServiceType + Clone,
impl<S> ServiceRequestBuilder<S>where S: ServiceType + Clone,
sourcepub async fn fetch_paginated<T>(
self,
limit: Option<usize>,
starting_with: Option<<T as PaginatedResource>::Id>
) -> impl Stream<Item = Result<T, Error>>where
S: Send + Sync,
T: PaginatedResource + Unpin,
<T as PaginatedResource>::Root: Into<Vec<T>> + Send,
pub async fn fetch_paginated<T>( self, limit: Option<usize>, starting_with: Option<<T as PaginatedResource>::Id> ) -> impl Stream<Item = Result<T, Error>>where S: Send + Sync, T: PaginatedResource + Unpin, <T as PaginatedResource>::Root: Into<Vec<T>> + Send,
Send the request and receive JSON in response with pagination.
Note that the actual requests will happen only on iteration over the results.
To use this feature, you need to implement PaginatedResource
for your resource
class. This can be done with derive
:
use futures::pin_mut;
use futures::stream::TryStreamExt;
use serde::Deserialize;
use osauth::PaginatedResource;
#[derive(Debug, Deserialize, PaginatedResource)]
#[collection_name = "servers"]
pub struct Server {
#[resource_id]
pub id: String,
pub name: String,
}
let session = osauth::Session::from_env().await?;
let servers = session
.get(osauth::services::COMPUTE, &["servers"])
.fetch_paginated::<Server>(None, None)
.await;
pin_mut!(servers);
while let Some(srv) = servers.try_next().await? {
println!("ID = {}, Name = {}", srv.id, srv.name);
}
See PaginatedResource
for more information on how to implement it.
Panics
Will panic during iteration if the request builder has a streaming body.
sourcepub fn try_clone(&self) -> Option<ServiceRequestBuilder<S>>
pub fn try_clone(&self) -> Option<ServiceRequestBuilder<S>>
Attempt to clone this request builder.