Skip to content

Commit

Permalink
define resource client
Browse files Browse the repository at this point in the history
Signed-off-by: remzi <[email protected]>
  • Loading branch information
HaoYang670 committed Jan 27, 2025
1 parent fa3b651 commit 4faada8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
5 changes: 3 additions & 2 deletions rucat_client/examples/engine.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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(())
}
21 changes: 0 additions & 21 deletions rucat_client/src/engine.rs

This file was deleted.

4 changes: 2 additions & 2 deletions rucat_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
45 changes: 45 additions & 0 deletions rucat_client/src/resource_client.rs
Original file line number Diff line number Diff line change
@@ -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<Credentials<'a>>,
/// 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<Credentials<'a>>) -> Self {
Self {
base_url,
credentials,
client: reqwest::Client::new(),
}
}

pub async fn create_engine(&self, request: &CreateEngineRequest) -> Result<EngineId, reqwest::Error> {
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,
}
}
}

0 comments on commit 4faada8

Please sign in to comment.