Skip to content

Commit

Permalink
fix env spacing and " issues
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant committed Oct 14, 2024
1 parent 09c0fa7 commit 63ba970
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ default-members = ["compute"]

[workspace.package]
edition = "2021"
version = "0.2.15"
version = "0.2.16"
license = "Apache-2.0"
readme = "README.md"

Expand Down
4 changes: 3 additions & 1 deletion workflows/src/apis/jina.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use eyre::{eyre, Context, Result};
use reqwest::Client;
use std::env;

use crate::utils::safe_read_env;

/// Makes a request for `example.com`.
const JINA_EXAMPLE_ENDPOINT: &str = "https://r.jina.ai/https://example.com";
const ENV_VAR_NAME: &str = "JINA_API_KEY";
Expand All @@ -17,7 +19,7 @@ impl JinaConfig {
/// Looks at the environment variables for Jina API key.
pub fn new() -> Self {
Self {
api_key: env::var(ENV_VAR_NAME).ok(),
api_key: safe_read_env(env::var(ENV_VAR_NAME)),
}
}

Expand Down
5 changes: 4 additions & 1 deletion workflows/src/apis/serper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use eyre::{eyre, Context, Result};
use reqwest::Client;
use std::env;

use crate::utils::safe_read_env;

/// Makes a search request.
const SERPER_EXAMPLE_ENDPOINT: &str = "https://google.serper.dev/search";
const ENV_VAR_NAME: &str = "SERPER_API_KEY";
Expand All @@ -17,7 +19,7 @@ impl SerperConfig {
/// Looks at the environment variables for Serper API key.
pub fn new() -> Self {
Self {
api_key: env::var(ENV_VAR_NAME).ok(),
api_key: safe_read_env(env::var(ENV_VAR_NAME)),
}
}

Expand Down Expand Up @@ -46,6 +48,7 @@ impl SerperConfig {
log::debug!("Serper API key not found, skipping Serper check");
return Ok(());
};
println!("API KEY: {}", api_key);
log::info!("Serper API key found, checking Serper service");

// make a dummy request
Expand Down
5 changes: 4 additions & 1 deletion workflows/src/providers/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use ollama_workflows::Model;
use reqwest::Client;
use serde::Deserialize;

use crate::utils::safe_read_env;

const OPENAI_MODELS_API: &str = "https://api.openai.com/v1/models";
const ENV_VAR_NAME: &str = "OPENAI_API_KEY";

/// [Model](https://platform.openai.com/docs/api-reference/models/object) API object.
#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -39,7 +42,7 @@ impl OpenAIConfig {
/// Looks at the environment variables for OpenAI API key.
pub fn new() -> Self {
Self {
api_key: std::env::var("OPENAI_API_KEY").ok(),
api_key: safe_read_env(std::env::var(ENV_VAR_NAME)),
}
}

Expand Down
24 changes: 22 additions & 2 deletions workflows/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,40 @@ pub fn split_csv_line(input: &str) -> Vec<String> {
.collect::<Vec<_>>()
}

/// Reads an environment variable and trims whitespace and `"` from both ends.
/// If the trimmed value is empty, returns `None`.
pub fn safe_read_env(var: Result<String, std::env::VarError>) -> Option<String> {
var.map(|s| s.trim_matches('"').trim().to_string())
.ok()
.filter(|s| !s.is_empty())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_example() {
fn test_abc_csv() {
// should ignore whitespaces and `"` at both ends, and ignore empty items
let input = "\"a, b , c ,, \"";
let expected = vec!["a".to_string(), "b".to_string(), "c".to_string()];
assert_eq!(split_csv_line(input), expected);
}

#[test]
fn test_empty() {
fn test_empty_csv() {
assert!(split_csv_line(Default::default()).is_empty());
}

#[test]
fn test_var_read() {
let var = Ok("\" value \"".to_string());
assert_eq!(safe_read_env(var), Some("value".to_string()));

let var = Ok("\" \"".to_string());
assert!(safe_read_env(var).is_none());

let var = Err(std::env::VarError::NotPresent);
assert!(safe_read_env(var).is_none());
}
}

0 comments on commit 63ba970

Please sign in to comment.