From a506d191452010a1d10c46a1413374a5fe29c31f Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Mon, 14 Jul 2025 09:29:43 -0500 Subject: [PATCH] refactor: handle empty strings as defaults in provider factories --- src/providers/anthropic/anthropic_factory.cpp | 33 +++++++++++++---- src/providers/openai/openai_factory.cpp | 37 ++++++++++++++----- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/src/providers/anthropic/anthropic_factory.cpp b/src/providers/anthropic/anthropic_factory.cpp index f8abaac..bc6540c 100644 --- a/src/providers/anthropic/anthropic_factory.cpp +++ b/src/providers/anthropic/anthropic_factory.cpp @@ -1,33 +1,50 @@ #include "anthropic_factory.h" +#include "ai/errors.h" #include "anthropic_client.h" #include #include #include -#include namespace ai { namespace anthropic { namespace { constexpr const char* kDefaultBaseUrl = "https://api.anthropic.com"; + +std::string get_api_key_or_default(const std::string& api_key) { + if (!api_key.empty()) { + return api_key; + } + + const char* env_api_key = std::getenv("ANTHROPIC_API_KEY"); + if (!env_api_key) { + throw ConfigurationError( + "API key not provided and ANTHROPIC_API_KEY environment variable not " + "set"); + } + return env_api_key; +} + +std::string get_base_url_or_default(const std::string& base_url) { + return base_url.empty() ? kDefaultBaseUrl : base_url; +} } // namespace Client create_client() { - const char* api_key = std::getenv("ANTHROPIC_API_KEY"); - if (!api_key) { - throw std::runtime_error("ANTHROPIC_API_KEY environment variable not set"); - } - return Client(std::make_unique(api_key, kDefaultBaseUrl)); + return Client(std::make_unique(get_api_key_or_default(""), + kDefaultBaseUrl)); } Client create_client(const std::string& api_key) { - return Client(std::make_unique(api_key, kDefaultBaseUrl)); + return Client(std::make_unique( + get_api_key_or_default(api_key), kDefaultBaseUrl)); } Client create_client(const std::string& api_key, const std::string& base_url) { - return Client(std::make_unique(api_key, base_url)); + return Client(std::make_unique( + get_api_key_or_default(api_key), get_base_url_or_default(base_url))); } std::optional try_create_client() { diff --git a/src/providers/openai/openai_factory.cpp b/src/providers/openai/openai_factory.cpp index d2dfacc..808e841 100644 --- a/src/providers/openai/openai_factory.cpp +++ b/src/providers/openai/openai_factory.cpp @@ -1,40 +1,57 @@ #include "openai_factory.h" +#include "ai/errors.h" #include "openai_client.h" #include #include #include -#include namespace ai { namespace openai { namespace { constexpr const char* kDefaultBaseUrl = "https://api.openai.com"; + +std::string get_api_key_or_default(const std::string& api_key) { + if (!api_key.empty()) { + return api_key; + } + + const char* env_api_key = std::getenv("OPENAI_API_KEY"); + if (!env_api_key) { + throw ConfigurationError( + "API key not provided and OPENAI_API_KEY environment variable not set"); + } + return env_api_key; +} + +std::string get_base_url_or_default(const std::string& base_url) { + return base_url.empty() ? kDefaultBaseUrl : base_url; +} } // namespace Client create_client() { - const char* api_key = std::getenv("OPENAI_API_KEY"); - if (!api_key) { - throw std::runtime_error("OPENAI_API_KEY environment variable not set"); - } - return Client(std::make_unique(api_key, kDefaultBaseUrl)); + return Client(std::make_unique(get_api_key_or_default(""), + kDefaultBaseUrl)); } Client create_client(const std::string& api_key) { - return Client(std::make_unique(api_key, kDefaultBaseUrl)); + return Client(std::make_unique(get_api_key_or_default(api_key), + kDefaultBaseUrl)); } Client create_client(const std::string& api_key, const std::string& base_url) { - return Client(std::make_unique(api_key, base_url)); + return Client(std::make_unique( + get_api_key_or_default(api_key), get_base_url_or_default(base_url))); } Client create_client(const std::string& api_key, const std::string& base_url, const retry::RetryConfig& retry_config) { - return Client( - std::make_unique(api_key, base_url, retry_config)); + return Client(std::make_unique( + get_api_key_or_default(api_key), get_base_url_or_default(base_url), + retry_config)); } std::optional try_create_client() {