Struct osauth::Adapter

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

source

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.

source

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.

source

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.

source

pub fn from_session(session: Session, service: Srv) -> Adapter<Srv>

Create a new adapter from a Session.

source

pub fn auth_type(&self) -> &dyn AuthType

Get a reference to the authentication type in use.

source

pub fn endpoint_filters(&self) -> &EndpointFilters

Endpoint filters in use.

source

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.

source

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.

source

pub fn session(&self) -> &Session

Session used for this adapter.

source

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.

source

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.

source

pub fn with_auth_type<Auth: AuthType + 'static>( self, auth_method: Auth ) -> Adapter<Srv>

Convert this adapter into one using the given authentication.

source

pub fn with_endpoint_filters( self, endpoint_filters: EndpointFilters ) -> Adapter<Srv>

Convert this adapter into one using the given endpoint filters.

source

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,

source

pub fn default_api_version(&self) -> Option<ApiVersion>

Default API version used when no version is specified.

source

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.

source

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>

source

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");
}
source

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.

source

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.

source

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

pub async fn supports_api_version( &self, version: ApiVersion ) -> Result<bool, Error>

Check if the service supports the API version.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

Trait Implementations§

source§

impl<Srv: Clone> Clone for Adapter<Srv>

source§

fn clone(&self) -> Adapter<Srv>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<Srv: Debug> Debug for Adapter<Srv>

source§

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

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

impl<Srv> From<Adapter<Srv>> for Session

source§

fn from(value: Adapter<Srv>) -> Session

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<Srv> !RefUnwindSafe for Adapter<Srv>

§

impl<Srv> Send for Adapter<Srv>where Srv: Send,

§

impl<Srv> Sync for Adapter<Srv>where Srv: Sync,

§

impl<Srv> Unpin for Adapter<Srv>where Srv: Unpin,

§

impl<Srv> !UnwindSafe for Adapter<Srv>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

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 Twhere 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more