refactor: optimize API keys reading (#6655)

* centralize API keys handling

* fix mypy and pylint

* rm utility function, be more explicit
This commit is contained in:
Stefano Fiorucci 2024-01-05 10:40:03 +01:00 committed by GitHub
parent 1336456b4f
commit bb2b1a20f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 30 deletions

View File

@ -1,7 +1,7 @@
from pathlib import Path
from typing import List, Union, Dict, Any, Optional
import os
import logging
import os
from haystack.lazy_imports import LazyImport
from haystack import component, Document, default_to_dict
@ -51,16 +51,15 @@ class AzureOCRDocumentConverter:
"""
azure_import.check()
if api_key is None:
try:
api_key = os.environ["AZURE_AI_API_KEY"]
except KeyError as e:
raise ValueError(
"AzureOCRDocumentConverter expects an Azure Credential key. "
"Set the AZURE_AI_API_KEY environment variable (recommended) or pass it explicitly."
) from e
api_key = api_key or os.environ.get("AZURE_AI_API_KEY")
# we check whether api_key is None or an empty string
if not api_key:
msg = (
"AzureOCRDocumentConverter expects an API key. "
"Set the AZURE_AI_API_KEY environment variable (recommended) or pass it explicitly."
)
raise ValueError(msg)
self.api_key = api_key
self.document_analysis_client = DocumentAnalysisClient(
endpoint=endpoint, credential=AzureKeyCredential(api_key)
)

View File

@ -1,7 +1,7 @@
import json
import os
import logging
from typing import Dict, List, Optional, Any
import os
import requests
@ -42,14 +42,15 @@ class SearchApiWebSearch:
For example, you can set 'num' to 100 to increase the number of search results.
See the [SearchApi website](https://www.searchapi.io/) for more details.
"""
if api_key is None:
try:
api_key = os.environ["SEARCHAPI_API_KEY"]
except KeyError as e:
raise ValueError(
"SearchApiWebSearch expects an API key. "
"Set the SEARCHAPI_API_KEY environment variable (recommended) or pass it explicitly."
) from e
api_key = api_key or os.environ.get("SEARCHAPI_API_KEY")
# we check whether api_key is None or an empty string
if not api_key:
msg = (
"SearchApiWebSearch expects an API key. "
"Set the SEARCHAPI_API_KEY environment variable (recommended) or pass it explicitly."
)
raise ValueError(msg)
self.api_key = api_key
self.top_k = top_k
self.allowed_domains = allowed_domains

View File

@ -1,7 +1,7 @@
import json
import os
import logging
from typing import Dict, List, Optional, Any
import os
import requests
@ -42,15 +42,15 @@ class SerperDevWebSearch:
For example, you can set 'num' to 20 to increase the number of search results.
See the [Serper Dev website](https://serper.dev/) for more details.
"""
if api_key is None:
try:
api_key = os.environ["SERPERDEV_API_KEY"]
except KeyError as e:
raise ValueError(
"SerperDevWebSearch expects an API key. "
"Set the SERPERDEV_API_KEY environment variable (recommended) or pass it explicitly."
) from e
raise ValueError("API key for SerperDev API must be set.")
api_key = api_key or os.environ.get("SERPERDEV_API_KEY")
# we check whether api_key is None or an empty string
if not api_key:
msg = (
"SerperDevWebSearch expects an API key. "
"Set the SERPERDEV_API_KEY environment variable (recommended) or pass it explicitly."
)
raise ValueError(msg)
self.api_key = api_key
self.top_k = top_k
self.allowed_domains = allowed_domains

View File

@ -10,7 +10,7 @@ from haystack.dataclasses import ByteStream
class TestAzureOCRDocumentConverter:
def test_init_fail_wo_api_key(self, monkeypatch):
monkeypatch.delenv("AZURE_AI_API_KEY", raising=False)
with pytest.raises(ValueError, match="AzureOCRDocumentConverter expects an Azure Credential key"):
with pytest.raises(ValueError):
AzureOCRDocumentConverter(endpoint="test_endpoint")
def test_to_dict(self):