From 7659dea65ba2b263e49c1a3acab37f158f535059 Mon Sep 17 00:00:00 2001 From: Martin Vidner Date: Wed, 31 Jul 2024 13:39:28 +0200 Subject: [PATCH 1/3] BaseHTTPClient: fields made public, better constructor API, Default to enable - connecting to alternative servers, either production or test mocks - unauthenticated calls, either for the initial auth or test mocsk --- rust/agama-lib/src/base_http_client.rs | 37 +++++++++++++++++++------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/rust/agama-lib/src/base_http_client.rs b/rust/agama-lib/src/base_http_client.rs index 655a1ce5d7..bc31c485ef 100644 --- a/rust/agama-lib/src/base_http_client.rs +++ b/rust/agama-lib/src/base_http_client.rs @@ -1,4 +1,4 @@ -use reqwest::{header, Client, Response}; +use reqwest::{header, Response}; use serde::{de::DeserializeOwned, Serialize}; use crate::{auth::AuthToken, error::ServiceError}; @@ -21,15 +21,36 @@ use crate::{auth::AuthToken, error::ServiceError}; /// } /// ``` pub struct BaseHTTPClient { - client: Client, + pub client: reqwest::Client, pub base_url: String, } const API_URL: &str = "http://localhost/api"; +impl Default for BaseHTTPClient { + /// A `default` client + /// - is NOT authenticated (maybe you want to call `new` instead) + /// - uses `localhost` + fn default() -> Self { + Self { + client: reqwest::Client::new(), + base_url: API_URL.to_owned(), + } + } +} + impl BaseHTTPClient { - // if there is need for client without authorization, create new constructor for it + /// Uses `localhost`, authenticates with [`AuthToken`]. pub fn new() -> Result { + Ok(Self { + client: Self::authenticated_reqwest_client()?, + ..Default::default() + }) + } + + fn authenticated_reqwest_client() -> Result { + // TODO: this error is subtly misleading, leading me to believe the SERVER said it, + // but in fact it is the CLIENT not finding an auth token let token = AuthToken::find().ok_or(ServiceError::NotAuthenticated)?; let mut headers = header::HeaderMap::new(); @@ -39,12 +60,10 @@ impl BaseHTTPClient { headers.insert(header::AUTHORIZATION, value); - let client = Client::builder().default_headers(headers).build()?; - - Ok(Self { - client, - base_url: API_URL.to_string(), // TODO: add support for remote server - }) + let client = reqwest::Client::builder() + .default_headers(headers) + .build()?; + Ok(client) } /// Simple wrapper around [`Response`] to get object from response. From 08f43695292f63ea6d921f41208dd3c707b7df1a Mon Sep 17 00:00:00 2001 From: Martin Vidner Date: Thu, 1 Aug 2024 10:47:47 +0200 Subject: [PATCH 2/3] BaseHTTPClient.client private again, +new_unauthenticated_with_url and renaming the helper authenticator --- rust/agama-lib/src/base_http_client.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/rust/agama-lib/src/base_http_client.rs b/rust/agama-lib/src/base_http_client.rs index bc31c485ef..0a28307420 100644 --- a/rust/agama-lib/src/base_http_client.rs +++ b/rust/agama-lib/src/base_http_client.rs @@ -21,7 +21,7 @@ use crate::{auth::AuthToken, error::ServiceError}; /// } /// ``` pub struct BaseHTTPClient { - pub client: reqwest::Client, + client: reqwest::Client, pub base_url: String, } @@ -43,12 +43,19 @@ impl BaseHTTPClient { /// Uses `localhost`, authenticates with [`AuthToken`]. pub fn new() -> Result { Ok(Self { - client: Self::authenticated_reqwest_client()?, + client: Self::authenticated_client()?, ..Default::default() }) } - fn authenticated_reqwest_client() -> Result { + pub fn new_unauthenticated_with_url(url: String) -> Result { + Ok(Self { + client: reqwest::Client::new(), + base_url: url, + }) + } + + fn authenticated_client() -> Result { // TODO: this error is subtly misleading, leading me to believe the SERVER said it, // but in fact it is the CLIENT not finding an auth token let token = AuthToken::find().ok_or(ServiceError::NotAuthenticated)?; From 7c155d23f2754d5487318fa07907072b37cf856b Mon Sep 17 00:00:00 2001 From: Martin Vidner Date: Thu, 1 Aug 2024 16:24:29 +0200 Subject: [PATCH 3/3] BaseHTTPClient: just use the public base_url field --- rust/agama-lib/src/base_http_client.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/rust/agama-lib/src/base_http_client.rs b/rust/agama-lib/src/base_http_client.rs index 0a28307420..1f78c2e040 100644 --- a/rust/agama-lib/src/base_http_client.rs +++ b/rust/agama-lib/src/base_http_client.rs @@ -48,13 +48,6 @@ impl BaseHTTPClient { }) } - pub fn new_unauthenticated_with_url(url: String) -> Result { - Ok(Self { - client: reqwest::Client::new(), - base_url: url, - }) - } - fn authenticated_client() -> Result { // TODO: this error is subtly misleading, leading me to believe the SERVER said it, // but in fact it is the CLIENT not finding an auth token