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<BaseClient, HttpError>
pub fn new(config: ApiConfig) -> Result<BaseClient, HttpError>
Create a new base client from configuration.
§Errors
Returns an error if the HTTP client cannot be built.
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,
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
Trait Implementations§
Source§impl Clone for BaseClient
impl Clone for BaseClient
Source§fn clone(&self) -> BaseClient
fn clone(&self) -> BaseClient
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for BaseClient
impl !RefUnwindSafe for BaseClient
impl Send for BaseClient
impl Sync for BaseClient
impl Unpin for BaseClient
impl !UnwindSafe for BaseClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more