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: ClientImplementations§
Source§impl SclClient
impl SclClient
Sourcepub fn new(api_url: String, client: Client) -> Self
pub fn new(api_url: String, client: Client) -> Self
Creates a new SclClient from a URL string and an preconfigured [Client].
Sourcepub async fn list<T: SclObject>(
&self,
req: &SclRequest<'_, T>,
) -> Result<Vec<T>, Error>
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.
Sourcepub async fn show<T: SclObject>(
&self,
req: &SclRequest<'_, T>,
) -> Result<T, Error>
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.
Sourcepub async fn delete<T: SclObject>(
&self,
req: &SclRequest<'_, T>,
) -> Result<(), Error>
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.
Sourcepub async fn create<T: SclObject>(
&self,
req: &SclRequest<'_, T>,
) -> Result<(), Error>
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.
Sourcepub async fn update<T: SclObject>(
&self,
req: &SclRequest<'_, T>,
) -> Result<(), Error>
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.
Sourcepub async fn watch<T: EventHandler>(&self, handler: T) -> Result<(), Error>
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{ }.
Sourcepub async fn recv_event<T: SclObject>(
resp: &mut Response,
) -> Result<SclEvent<T>>
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) -> RequestBuilderwhere
U: IntoUrl,
pub fn get<U>(&self, url: U) -> RequestBuilderwhere
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) -> RequestBuilderwhere
U: IntoUrl,
pub fn post<U>(&self, url: U) -> RequestBuilderwhere
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) -> RequestBuilderwhere
U: IntoUrl,
pub fn put<U>(&self, url: U) -> RequestBuilderwhere
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) -> RequestBuilderwhere
U: IntoUrl,
pub fn patch<U>(&self, url: U) -> RequestBuilderwhere
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) -> RequestBuilderwhere
U: IntoUrl,
pub fn delete<U>(&self, url: U) -> RequestBuilderwhere
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) -> RequestBuilderwhere
U: IntoUrl,
pub fn head<U>(&self, url: U) -> RequestBuilderwhere
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) -> RequestBuilderwhere
U: IntoUrl,
pub fn request<U>(&self, method: Method, url: U) -> RequestBuilderwhere
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>>
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.