feat!: wrap Client::new() return in a Result
BREAKING CHANGE: `Client::new()` now returns a `Result`.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user