feat!: wrap Client::new() return in a Result
BREAKING CHANGE: `Client::new()` now returns a `Result`.
This commit is contained in:
@@ -54,7 +54,7 @@ use mistralai_client::v1::client::Client;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let api_key = "your_api_key";
|
let api_key = "your_api_key";
|
||||||
|
|
||||||
let client = Client::new(Some(api_key), None, None, None);
|
let client = Client::new(Some(api_key), None, None, None).unwrap();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ use mistralai_client::v1::{
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
|
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
|
||||||
let client = Client::new(None, None, None, None);
|
let client = Client::new(None, None, None, None).unwrap();
|
||||||
|
|
||||||
let model = Model::OpenMistral7b;
|
let model = Model::OpenMistral7b;
|
||||||
let messages = vec![ChatCompletionMessage {
|
let messages = vec![ChatCompletionMessage {
|
||||||
@@ -101,7 +101,7 @@ use mistralai_client::v1::{client::Client, constants::EmbedModel};
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
|
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
|
||||||
let client: Client = Client::new(None, None, None, None);
|
let client: Client = Client::new(None, None, None, None).unwrap();
|
||||||
|
|
||||||
let model = EmbedModel::MistralEmbed;
|
let model = EmbedModel::MistralEmbed;
|
||||||
let input = vec!["Embed this sentence.", "As well as this one."]
|
let input = vec!["Embed this sentence.", "As well as this one."]
|
||||||
@@ -123,7 +123,7 @@ use mistralai_client::v1::client::Client;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
|
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
|
||||||
let client = Client::new(None, None, None, None);
|
let client = Client::new(None, None, None, None).unwrap();
|
||||||
|
|
||||||
let result = client.list_models().unwrap();
|
let result = client.list_models().unwrap();
|
||||||
println!("First Model ID: {:?}", result.data[0].id);
|
println!("First Model ID: {:?}", result.data[0].id);
|
||||||
|
|||||||
@@ -24,21 +24,21 @@ impl Client {
|
|||||||
endpoint: Option<String>,
|
endpoint: Option<String>,
|
||||||
max_retries: Option<u32>,
|
max_retries: Option<u32>,
|
||||||
timeout: Option<u32>,
|
timeout: Option<u32>,
|
||||||
) -> Self {
|
) -> Result<Self, ClientError> {
|
||||||
let api_key = api_key.unwrap_or(
|
let api_key = api_key.unwrap_or(match std::env::var("MISTRAL_API_KEY") {
|
||||||
std::env::var("MISTRAL_API_KEY")
|
Ok(api_key_from_env) => api_key_from_env,
|
||||||
.unwrap_or_else(|_| panic!("{}", ClientError::ApiKeyError)),
|
Err(_) => return Err(ClientError::ApiKeyError),
|
||||||
);
|
});
|
||||||
let endpoint = endpoint.unwrap_or(API_URL_BASE.to_string());
|
let endpoint = endpoint.unwrap_or(API_URL_BASE.to_string());
|
||||||
let max_retries = max_retries.unwrap_or(5);
|
let max_retries = max_retries.unwrap_or(5);
|
||||||
let timeout = timeout.unwrap_or(120);
|
let timeout = timeout.unwrap_or(120);
|
||||||
|
|
||||||
Self {
|
Ok(Self {
|
||||||
api_key,
|
api_key,
|
||||||
endpoint,
|
endpoint,
|
||||||
max_retries,
|
max_retries,
|
||||||
timeout,
|
timeout,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_request(&self, request: minreq::Request) -> minreq::Request {
|
pub fn build_request(&self, request: minreq::Request) -> minreq::Request {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ impl fmt::Display for ApiError {
|
|||||||
}
|
}
|
||||||
impl Error for ApiError {}
|
impl Error for ApiError {}
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, PartialEq, thiserror::Error)]
|
||||||
pub enum ClientError {
|
pub enum ClientError {
|
||||||
#[error("You must either set the `MISTRAL_API_KEY` environment variable or specify it in `Client::new(api_key, ...).")]
|
#[error("You must either set the `MISTRAL_API_KEY` environment variable or specify it in `Client::new(api_key, ...).")]
|
||||||
ApiKeyError,
|
ApiKeyError,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use mistralai_client::v1::{
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_client_chat() {
|
fn test_client_chat() {
|
||||||
let client = Client::new(None, None, None, None);
|
let client = Client::new(None, None, None, None).unwrap();
|
||||||
|
|
||||||
let model = Model::OpenMistral7b;
|
let model = Model::OpenMistral7b;
|
||||||
let messages = vec![ChatCompletionMessage {
|
let messages = vec![ChatCompletionMessage {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use mistralai_client::v1::{client::Client, constants::EmbedModel};
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_client_embeddings() {
|
fn test_client_embeddings() {
|
||||||
let client: Client = Client::new(None, None, None, None);
|
let client: Client = Client::new(None, None, None, None).unwrap();
|
||||||
|
|
||||||
let model = EmbedModel::MistralEmbed;
|
let model = EmbedModel::MistralEmbed;
|
||||||
let input = vec!["Embed this sentence.", "As well as this one."]
|
let input = vec!["Embed this sentence.", "As well as this one."]
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use mistralai_client::v1::client::Client;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_client_list_models() {
|
fn test_client_list_models() {
|
||||||
let client = Client::new(None, None, None, None);
|
let client = Client::new(None, None, None, None).unwrap();
|
||||||
|
|
||||||
let response = client.list_models().unwrap();
|
let response = client.list_models().unwrap();
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use jrest::expect;
|
use jrest::expect;
|
||||||
use mistralai_client::v1::client::Client;
|
use mistralai_client::v1::{client::Client, error::ClientError};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_client_new_with_none_params() {
|
fn test_client_new_with_none_params() {
|
||||||
@@ -7,7 +7,7 @@ fn test_client_new_with_none_params() {
|
|||||||
std::env::remove_var("MISTRAL_API_KEY");
|
std::env::remove_var("MISTRAL_API_KEY");
|
||||||
std::env::set_var("MISTRAL_API_KEY", "test_api_key_from_env");
|
std::env::set_var("MISTRAL_API_KEY", "test_api_key_from_env");
|
||||||
|
|
||||||
let client = Client::new(None, None, None, None);
|
let client = Client::new(None, None, None, None).unwrap();
|
||||||
|
|
||||||
expect!(client.api_key).to_be("test_api_key_from_env".to_string());
|
expect!(client.api_key).to_be("test_api_key_from_env".to_string());
|
||||||
expect!(client.endpoint).to_be("https://api.mistral.ai/v1".to_string());
|
expect!(client.endpoint).to_be("https://api.mistral.ai/v1".to_string());
|
||||||
@@ -38,7 +38,8 @@ fn test_client_new_with_all_params() {
|
|||||||
endpoint.clone(),
|
endpoint.clone(),
|
||||||
max_retries.clone(),
|
max_retries.clone(),
|
||||||
timeout.clone(),
|
timeout.clone(),
|
||||||
);
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
expect!(client.api_key).to_be(api_key.unwrap());
|
expect!(client.api_key).to_be(api_key.unwrap());
|
||||||
expect!(client.endpoint).to_be(endpoint.unwrap());
|
expect!(client.endpoint).to_be(endpoint.unwrap());
|
||||||
@@ -54,12 +55,16 @@ fn test_client_new_with_all_params() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn test_client_new_with_missing_api_key() {
|
fn test_client_new_with_missing_api_key() {
|
||||||
let maybe_original_mistral_api_key = std::env::var("MISTRAL_API_KEY").ok();
|
let maybe_original_mistral_api_key = std::env::var("MISTRAL_API_KEY").ok();
|
||||||
std::env::remove_var("MISTRAL_API_KEY");
|
std::env::remove_var("MISTRAL_API_KEY");
|
||||||
|
|
||||||
let _client = Client::new(None, None, None, None);
|
let call = || Client::new(None, None, None, None);
|
||||||
|
|
||||||
|
match call() {
|
||||||
|
Ok(_) => panic!("Expected `ClientError::ApiKeyError` but got Ok.`"),
|
||||||
|
Err(error) => assert_eq!(error, ClientError::ApiKeyError),
|
||||||
|
}
|
||||||
|
|
||||||
match maybe_original_mistral_api_key {
|
match maybe_original_mistral_api_key {
|
||||||
Some(original_mistral_api_key) => {
|
Some(original_mistral_api_key) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user