pub struct Adapter<Srv> { /* private fields */ }
Expand description
Adapter for a specific service.
An Adapter
is very similar to a Session, but is tied to a specific
service, and thus does not require passing a service
argument to all calls.
Implementations§
source§impl<Srv> Adapter<Srv>
impl<Srv> Adapter<Srv>
sourcepub async fn new<Auth: AuthType + 'static>(
auth_type: Auth,
service: Srv
) -> Result<Adapter<Srv>, Error>
pub async fn new<Auth: AuthType + 'static>( auth_type: Auth, service: Srv ) -> Result<Adapter<Srv>, Error>
Create a new adapter with a given authentication plugin.
sourcepub async fn from_config<S: AsRef<str>>(
cloud_name: S,
service: Srv
) -> Result<Adapter<Srv>, Error>
pub async fn from_config<S: AsRef<str>>( cloud_name: S, service: Srv ) -> Result<Adapter<Srv>, Error>
Create a new adapter from a clouds.yaml
configuration file.
See Session::from_config for details.
sourcepub async fn from_env(service: Srv) -> Result<Adapter<Srv>, Error>
pub async fn from_env(service: Srv) -> Result<Adapter<Srv>, Error>
Create a new adapter with information from environment variables.
See Session::from_env for details.
sourcepub fn from_session(session: Session, service: Srv) -> Adapter<Srv>
pub fn from_session(session: Session, service: Srv) -> Adapter<Srv>
Create a new adapter from a Session
.
sourcepub fn endpoint_filters(&self) -> &EndpointFilters
pub fn endpoint_filters(&self) -> &EndpointFilters
Endpoint filters in use.
sourcepub fn endpoint_filters_mut(&mut self) -> &mut EndpointFilters
pub fn endpoint_filters_mut(&mut self) -> &mut EndpointFilters
Modify endpoint filters.
This call clears the cached service information for this Adapter
.
It does not, however, affect clones of this Adapter
.
sourcepub async fn refresh(&mut self) -> Result<(), Error>
pub async fn refresh(&mut self) -> Result<(), Error>
Update the authentication and purges cached endpoint information.
Warning
Authentication will also be updated for clones of this Adapter
and its parent Session
,
since they share the same authentication object.
sourcepub fn set_auth_type<Auth: AuthType + 'static>(&mut self, auth_type: Auth)
pub fn set_auth_type<Auth: AuthType + 'static>(&mut self, auth_type: Auth)
Set a new authentication for this Adapter
.
This call clears the cached service information for this Adapter
.
It does not, however, affect clones of this Adapter
.
sourcepub fn set_endpoint_interface(&mut self, endpoint_interface: InterfaceType)
pub fn set_endpoint_interface(&mut self, endpoint_interface: InterfaceType)
A convenience call to set an endpoint interface.
This call clears the cached service information for this Adapter
.
It does not, however, affect clones of this Adapter
.
sourcepub fn with_auth_type<Auth: AuthType + 'static>(
self,
auth_method: Auth
) -> Adapter<Srv>
pub fn with_auth_type<Auth: AuthType + 'static>( self, auth_method: Auth ) -> Adapter<Srv>
Convert this adapter into one using the given authentication.
sourcepub fn with_endpoint_filters(
self,
endpoint_filters: EndpointFilters
) -> Adapter<Srv>
pub fn with_endpoint_filters( self, endpoint_filters: EndpointFilters ) -> Adapter<Srv>
Convert this adapter into one using the given endpoint filters.
sourcepub fn with_endpoint_interface(
self,
endpoint_interface: InterfaceType
) -> Adapter<Srv>
pub fn with_endpoint_interface( self, endpoint_interface: InterfaceType ) -> Adapter<Srv>
Convert this adapter into one using the given endpoint filters.
source§impl<Srv> Adapter<Srv>where
Srv: VersionedService + Send,
impl<Srv> Adapter<Srv>where Srv: VersionedService + Send,
sourcepub fn default_api_version(&self) -> Option<ApiVersion>
pub fn default_api_version(&self) -> Option<ApiVersion>
Default API version used when no version is specified.
sourcepub fn set_default_api_version(&mut self, api_version: Option<ApiVersion>)
pub fn set_default_api_version(&mut self, api_version: Option<ApiVersion>)
Set the default API version.
This version will be used when no version is specified. No checks are done against this
version inside of this call. If it is not valid, the subsequent request
calls will fail.
sourcepub fn with_default_api_version(
self,
api_version: Option<ApiVersion>
) -> Adapter<Srv>
pub fn with_default_api_version( self, api_version: Option<ApiVersion> ) -> Adapter<Srv>
Convert this adapter into one using the given default API version.
source§impl<Srv: ServiceType + Send + Clone> Adapter<Srv>
impl<Srv: ServiceType + Send + Clone> Adapter<Srv>
sourcepub async fn get_api_versions(
&self
) -> Result<Option<(ApiVersion, ApiVersion)>, Error>
pub async fn get_api_versions( &self ) -> Result<Option<(ApiVersion, ApiVersion)>, Error>
Get minimum/maximum API (micro)version information.
Returns None
if the range cannot be determined, which usually means
that microversioning is not supported.
let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE)
.await
.expect("Failed to create an identity provider from the environment");
let maybe_versions = adapter
.get_api_versions()
.await?;
if let Some((min, max)) = maybe_versions {
println!("The compute service supports versions {} to {}", min, max);
} else {
println!("The compute service does not support microversioning");
}
sourcepub async fn get_endpoint<I>(&self, path: I) -> Result<Url, Error>where
I: IntoIterator + Send,
I::Item: AsRef<str>,
pub async fn get_endpoint<I>(&self, path: I) -> Result<Url, Error>where I: IntoIterator + Send, I::Item: AsRef<str>,
Construct an endpoint from the path;
You won’t need to use this call most of the time, since all request calls can fetch the endpoint automatically.
sourcepub async fn get_major_version(&self) -> Result<Option<ApiVersion>, Error>
pub async fn get_major_version(&self) -> Result<Option<ApiVersion>, Error>
Get the currently used major version from the given service.
Can return None
if the service does not support API version discovery at all.
sourcepub async fn pick_api_version<I>(
&self,
versions: I
) -> Result<Option<ApiVersion>, Error>where
I: IntoIterator<Item = ApiVersion> + Send,
pub async fn pick_api_version<I>( &self, versions: I ) -> Result<Option<ApiVersion>, Error>where I: IntoIterator<Item = ApiVersion> + Send,
Pick the highest API version supported by the service.
Returns None
if none of the requested versions are available.
let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE)
.await
.expect("Failed to create an identity provider from the environment");
let candidates = vec![osauth::ApiVersion(1, 2), osauth::ApiVersion(1, 42)];
let maybe_version = adapter
.pick_api_version(candidates)
.await?;
let request = adapter.get(&["servers"]);
let response = if let Some(version) = maybe_version {
println!("Using version {}", version);
request.api_version(version)
} else {
println!("Using the base version");
request
}.send().await?;
sourcepub async fn supports_api_version(
&self,
version: ApiVersion
) -> Result<bool, Error>
pub async fn supports_api_version( &self, version: ApiVersion ) -> Result<bool, Error>
Check if the service supports the API version.
sourcepub fn request<I>(&self, method: Method, path: I) -> ServiceRequestBuilder<Srv>where
I: IntoIterator,
I::Item: AsRef<str>,
pub fn request<I>(&self, method: Method, path: I) -> ServiceRequestBuilder<Srv>where I: IntoIterator, I::Item: AsRef<str>,
Make an HTTP request.
The path
argument is a URL path without the service endpoint (e.g. /servers/1234
).
The result is a ServiceRequestBuilder
that can be customized further. Error checking and response
parsing can be done using functions from the request module.
let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE)
.await
.expect("Failed to create an identity provider from the environment");
let response = adapter
.request(reqwest::Method::HEAD, &["servers", "1234"])
.send()
.await?;
println!("Response: {:?}", response);
This is the most generic call to make a request. You may prefer to use more specific get
,
post
, put
or delete
calls instead.
sourcepub fn get<I>(&self, path: I) -> ServiceRequestBuilder<Srv>where
I: IntoIterator,
I::Item: AsRef<str>,
pub fn get<I>(&self, path: I) -> ServiceRequestBuilder<Srv>where I: IntoIterator, I::Item: AsRef<str>,
Start a GET request.
See request for an explanation of the parameters.
sourcepub async fn get_json<I, T>(&self, path: I) -> Result<T, Error>where
I: IntoIterator,
I::Item: AsRef<str>,
T: DeserializeOwned + Send,
pub async fn get_json<I, T>(&self, path: I) -> Result<T, Error>where I: IntoIterator, I::Item: AsRef<str>, T: DeserializeOwned + Send,
Fetch a JSON using the GET request.
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Server {
pub id: String,
pub name: String,
}
#[derive(Debug, Deserialize)]
pub struct ServersRoot {
pub servers: Vec<Server>,
}
let adapter = osauth::Adapter::from_env(osauth::services::COMPUTE)
.await
.expect("Failed to create an identity provider from the environment");
let servers: ServersRoot = adapter
.get_json(&["servers"])
.await?;
for srv in servers.servers {
println!("ID = {}, Name = {}", srv.id, srv.name);
}
See request for an explanation of the parameters.
sourcepub fn post<I>(&self, path: I) -> ServiceRequestBuilder<Srv>where
I: IntoIterator,
I::Item: AsRef<str>,
pub fn post<I>(&self, path: I) -> ServiceRequestBuilder<Srv>where I: IntoIterator, I::Item: AsRef<str>,
Start a POST request.
See request for an explanation of the parameters.
sourcepub fn put<I>(&self, path: I) -> ServiceRequestBuilder<Srv>where
I: IntoIterator,
I::Item: AsRef<str>,
pub fn put<I>(&self, path: I) -> ServiceRequestBuilder<Srv>where I: IntoIterator, I::Item: AsRef<str>,
Start a PUT request.
See request for an explanation of the parameters.
sourcepub fn delete<I>(&self, path: I) -> ServiceRequestBuilder<Srv>where
I: IntoIterator,
I::Item: AsRef<str>,
pub fn delete<I>(&self, path: I) -> ServiceRequestBuilder<Srv>where I: IntoIterator, I::Item: AsRef<str>,
Start a DELETE request.
See request for an explanation of the parameters.