From 4faada8e9d1a37ee03c48acff3a9cf44559b94df Mon Sep 17 00:00:00 2001 From: remzi <13716567376yh@gmail.com> Date: Mon, 27 Jan 2025 17:25:01 +0800 Subject: [PATCH] define resource client Signed-off-by: remzi <13716567376yh@gmail.com> --- rucat_client/examples/engine.rs | 5 ++-- rucat_client/src/engine.rs | 21 -------------- rucat_client/src/lib.rs | 4 +-- rucat_client/src/resource_client.rs | 45 +++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 25 deletions(-) delete mode 100644 rucat_client/src/engine.rs create mode 100644 rucat_client/src/resource_client.rs diff --git a/rucat_client/examples/engine.rs b/rucat_client/examples/engine.rs index 9af2d0c..2d08e1b 100644 --- a/rucat_client/examples/engine.rs +++ b/rucat_client/examples/engine.rs @@ -1,4 +1,4 @@ -use ::rucat_client::{engine::create_engine, Credentials}; +use ::rucat_client::{resource_client, Credentials}; use ::rucat_common::engine::{CreateEngineRequest, EngineType}; use rucat_common::tokio; @@ -11,7 +11,8 @@ async fn main() -> Result<(), reqwest::Error> { config: None, }; let credentials = Credentials::Bearer { token: "admin" }; - let id = create_engine(&request, Some(&credentials)).await?; + let client = resource_client::ResourceClient::new("http://localhost:3000", Some(credentials)); + let id = client.create_engine(&request).await?; println!("Engine created with id: {}", id); Ok(()) } diff --git a/rucat_client/src/engine.rs b/rucat_client/src/engine.rs deleted file mode 100644 index 2adfc2a..0000000 --- a/rucat_client/src/engine.rs +++ /dev/null @@ -1,21 +0,0 @@ -//! Functions for managing the engine. - -use ::rucat_common::engine::{CreateEngineRequest, EngineId}; - -use crate::Credentials; - -pub async fn create_engine( - request: &CreateEngineRequest, - credentials: Option<&Credentials<'_>>, -) -> Result { - let client = reqwest::Client::new(); - let builder = client.post("http://localhost:3000/engine").json(request); - let builder = match credentials { - Some(Credentials::Basic { username, password }) => { - builder.basic_auth(username, Some(password)) - } - Some(Credentials::Bearer { token }) => builder.bearer_auth(token), - None => builder, - }; - builder.send().await?.error_for_status()?.json().await -} diff --git a/rucat_client/src/lib.rs b/rucat_client/src/lib.rs index e7053a4..1c4a4f0 100644 --- a/rucat_client/src/lib.rs +++ b/rucat_client/src/lib.rs @@ -1,9 +1,9 @@ -pub mod engine; +pub mod resource_client; pub enum Credentials<'a> { Basic { username: &'a str, - password: &'a str, + password: Option<&'a str>, }, Bearer { token: &'a str, diff --git a/rucat_client/src/resource_client.rs b/rucat_client/src/resource_client.rs new file mode 100644 index 0000000..97d76dd --- /dev/null +++ b/rucat_client/src/resource_client.rs @@ -0,0 +1,45 @@ +use ::rucat_common::engine::{CreateEngineRequest, EngineId}; + +use crate::Credentials; + +/// Client for managing Rucat resources. +pub struct ResourceClient<'a> { + /// Base URL of the Rucat server. + base_url: &'a str, + /// Credentials for authenticating with the Rucat server. + credentials: Option>, + /// HTTP client for making requests to the Rucat server. + client: reqwest::Client, +} + +impl<'a> ResourceClient<'a> { + /// Create a new `ResourceClient`. + pub fn new(base_url: &'a str, credentials: Option>) -> Self { + Self { + base_url, + credentials, + client: reqwest::Client::new(), + } + } + + pub async fn create_engine(&self, request: &CreateEngineRequest) -> Result { + let url = self.build_url("/engine"); + let builder = self.client.post(url).json(request); + let builder = self.enable_auth_for_request(builder); + builder.send().await?.error_for_status()?.json().await + } + + fn build_url(&self, path: &str) -> String { + self.base_url.to_owned() + path + } + + fn enable_auth_for_request(&self, builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder { + match self.credentials { + Some(Credentials::Basic { username, password }) => { + builder.basic_auth(username, password) + } + Some(Credentials::Bearer { token }) => builder.bearer_auth(token), + None => builder, + } + } +} \ No newline at end of file