pub struct BaseClient { /* private fields */ }Expand description
A base HTTP client that handles common request/response patterns.
This client provides reusable methods for making GET and POST requests with proper error handling, reducing boilerplate across API crates.
§Example
use yldfi_common::api::{ApiConfig, BaseClient};
// Create a client with configuration
let config = ApiConfig::new("https://api.example.com")
.api_key("your-api-key");
let client = BaseClient::new(config).unwrap();
// Build URLs
assert_eq!(client.url("/quote"), "https://api.example.com/quote");
// Access config
assert_eq!(client.config().get_api_key(), Some("your-api-key"));Implementations§
Source§impl BaseClient
impl BaseClient
Sourcepub fn new(config: ApiConfig) -> Result<Self, HttpError>
pub fn new(config: ApiConfig) -> Result<Self, HttpError>
Create a new base client from configuration.
This constructor validates the configuration, enforcing HTTPS for security (SEC-003 fix). HTTP is only allowed for localhost development URLs.
§Errors
Returns an error if:
- The URL uses HTTP (except localhost)
- The HTTP client cannot be built
Sourcepub fn url(&self, path: &str) -> String
pub fn url(&self, path: &str) -> String
Build the full URL for a path.
Uses cached normalized base URL to avoid repeated string allocation. LOW-003 fix: Uses proper URL joining for robustness.
Sourcepub fn default_headers(&self) -> HeaderMap
pub fn default_headers(&self) -> HeaderMap
Build default headers with API key (if present).
Override this in your client to add custom headers.
Sourcepub async fn get<T, E>(
&self,
path: &str,
params: &[(&str, impl AsRef<str>)],
) -> Result<T, ApiError<E>>where
T: DeserializeOwned,
E: Error,
pub async fn get<T, E>(
&self,
path: &str,
params: &[(&str, impl AsRef<str>)],
) -> Result<T, ApiError<E>>where
T: DeserializeOwned,
E: Error,
Make a GET request with query parameters.
§Type Parameters
T- The response type (must implementDeserializeOwned)E- Domain-specific error type (default:NoDomainError)
§Arguments
path- The API path (will be joined withbase_url)params- Query parameters as key-value pairs
§Errors
Returns an error if:
- Network request fails
- Response status indicates an error (4xx/5xx)
- Response body cannot be deserialized to type
T
Sourcepub async fn get_with_headers<T, E>(
&self,
path: &str,
params: &[(&str, impl AsRef<str>)],
headers: HeaderMap,
) -> Result<T, ApiError<E>>where
T: DeserializeOwned,
E: Error,
pub async fn get_with_headers<T, E>(
&self,
path: &str,
params: &[(&str, impl AsRef<str>)],
headers: HeaderMap,
) -> Result<T, ApiError<E>>where
T: DeserializeOwned,
E: Error,
Make a GET request with custom headers.
Use this when you need to add API-specific headers beyond the default Authorization header.
Sourcepub async fn post_json<T, B, E>(
&self,
path: &str,
body: &B,
) -> Result<T, ApiError<E>>
pub async fn post_json<T, B, E>( &self, path: &str, body: &B, ) -> Result<T, ApiError<E>>
Make a POST request with JSON body.
§Type Parameters
T- The response typeB- The request body type (must implementSerialize)E- Domain-specific error type
§Errors
Returns an error if:
- Network request fails
- Response status indicates an error (4xx/5xx)
- Response body cannot be deserialized to type
T
Trait Implementations§
Source§impl Clone for BaseClient
impl Clone for BaseClient
Source§fn clone(&self) -> BaseClient
fn clone(&self) -> BaseClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more