feat!: wrap Client::new() return in a Result

BREAKING CHANGE: `Client::new()` now returns a `Result`.
This commit is contained in:
Ivan Gabriele
2024-03-04 04:43:16 +01:00
parent 1deab88251
commit 33876183e4
7 changed files with 25 additions and 20 deletions

View File

@@ -24,21 +24,21 @@ impl Client {
endpoint: Option<String>,
max_retries: Option<u32>,
timeout: Option<u32>,
) -> Self {
let api_key = api_key.unwrap_or(
std::env::var("MISTRAL_API_KEY")
.unwrap_or_else(|_| panic!("{}", ClientError::ApiKeyError)),
);
) -> Result<Self, ClientError> {
let api_key = api_key.unwrap_or(match std::env::var("MISTRAL_API_KEY") {
Ok(api_key_from_env) => api_key_from_env,
Err(_) => return Err(ClientError::ApiKeyError),
});
let endpoint = endpoint.unwrap_or(API_URL_BASE.to_string());
let max_retries = max_retries.unwrap_or(5);
let timeout = timeout.unwrap_or(120);
Self {
Ok(Self {
api_key,
endpoint,
max_retries,
timeout,
}
})
}
pub fn build_request(&self, request: minreq::Request) -> minreq::Request {

View File

@@ -12,7 +12,7 @@ impl fmt::Display for ApiError {
}
impl Error for ApiError {}
#[derive(Debug, thiserror::Error)]
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum ClientError {
#[error("You must either set the `MISTRAL_API_KEY` environment variable or specify it in `Client::new(api_key, ...).")]
ApiKeyError,