SclClient

Struct SclClient 

Source
pub struct SclClient {
    pub api_url: String,
    client: Client,
}
Expand description

A wrapper object for a [Client] to provide convenient functions for interacting with the SCL API.

It implements the Deref trait so that custom requests can still be made directly via the wrapped client object.

§Examples

use scl_lib::api_objects::VirtualMachine;
use scl_lib::client::error::ClientError;
use scl_lib::client::{reqwest_client, SclClient, SclRequest};

#[tokio::main]
async fn main() -> Result<(), ClientError> {
    // Create client instance
    let client = {
        let reqwest_client = reqwest_client(None, None)
            .map_err(|e| ClientError::Custom(e.to_string()))?;
        SclClient::new("https://127.0.0.1:8008/api/v1".to_string(), reqwest_client)
    };
    // List all VMs of node "node-01"
    let vms: Vec<VirtualMachine> = client.list(
        &SclRequest::new().query(&[("node", "node-01")])
    ).await.unwrap();
    // Get the VM with the name "vm-01" of separation context "sc-01"
    let vm: VirtualMachine = client.show(
        &SclRequest::new().obj_name("vm-01").sc("sc-01")
    ).await.unwrap();
    // Update the virtual machine
    let _ = client.update(
        &SclRequest::new().obj_name(vm.metadata.name()).sc(&vm.separation_context).body(&vm)
    ).await.unwrap();
    // Delete the VM
    let _ = client.delete(
        &SclRequest::<VirtualMachine>::new().obj_name(vm.metadata.name())
    ).await.unwrap();
    Ok(())
}

Fields§

§api_url: String§client: Client

Implementations§

Source§

impl SclClient

Source

pub fn new(api_url: String, client: Client) -> Self

Creates a new SclClient from a URL string and an preconfigured [Client].

Source

pub async fn list<T: SclObject>( &self, req: &SclRequest<'_, T>, ) -> Result<Vec<T>, Error>

Performs a HTTP GET request to the SCL API to fetch and return all SclObjects of type T.

Source

pub async fn show<T: SclObject>( &self, req: &SclRequest<'_, T>, ) -> Result<T, Error>

Performs a HTTP GET request to the SCL API to fetch and return a object of type T.

Source

pub async fn delete<T: SclObject>( &self, req: &SclRequest<'_, T>, ) -> Result<(), Error>

Performs a HTTP DELETE request to the SCL API to delete a object of type T.

Source

pub async fn create<T: SclObject>( &self, req: &SclRequest<'_, T>, ) -> Result<(), Error>

Performs a HTTP POST request to the SCL API to create a object of type T.

Source

pub async fn update<T: SclObject>( &self, req: &SclRequest<'_, T>, ) -> Result<(), Error>

Performs a HTTP PUT request to the SCL API to update a object of type T.

Source

pub async fn watch<T: EventHandler>(&self, handler: T) -> Result<(), Error>

Performs an HTTP GET request to the watch endpoint of the item associated with the EventHandler and calls the handle function for every received SclEvent.

Invalid events are logged and then the function waits for further data. The function either returns Ok(()) when the bytestream ends, or returns an reqwest::Error if the connection fails or breaks. For an automatic reconnect the function call should be executed in a loop{ }.

Source

pub async fn recv_event<T: SclObject>( resp: &mut Response, ) -> Result<SclEvent<T>>

Reads chunks of bytes from an open HTTP stream, parses them into an SclEvent<T> object and then returns the result. Returns a client::error::Error if the stream fails to read or the response body has been exhausted.

Methods from Deref<Target = Client>§

pub fn get<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a GET request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

pub fn post<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a POST request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

pub fn put<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a PUT request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

pub fn patch<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a PATCH request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

pub fn delete<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a DELETE request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

pub fn head<U>(&self, url: U) -> RequestBuilder
where U: IntoUrl,

Convenience method to make a HEAD request to a URL.

§Errors

This method fails whenever the supplied Url cannot be parsed.

pub fn request<U>(&self, method: Method, url: U) -> RequestBuilder
where U: IntoUrl,

Start building a Request with the Method and Url.

Returns a RequestBuilder, which will allow setting headers and the request body before sending.

§Errors

This method fails whenever the supplied Url cannot be parsed.

pub fn execute( &self, request: Request, ) -> impl Future<Output = Result<Response, Error>>

Executes a Request.

A Request can be built manually with Request::new() or obtained from a RequestBuilder with RequestBuilder::build().

You should prefer to use the RequestBuilder and RequestBuilder::send().

§Errors

This method fails if there was an error while sending request, redirect loop was detected or redirect limit was exhausted.

Trait Implementations§

Source§

impl Clone for SclClient

Source§

fn clone(&self) -> SclClient

Returns a duplicate 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 Deref for SclClient

Source§

type Target = Client

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

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.

§

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 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = 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 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.
§

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