Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pinecone/config/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import NamedTuple, Optional
from typing import NamedTuple, Optional, Dict
import os

from pinecone.exceptions import PineconeConfigurationError
Expand All @@ -11,7 +11,8 @@ class Config(NamedTuple):
api_key: str = ""
host: str = ""
openapi_config: Optional[OpenApiConfiguration] = None

additional_headers: Optional[Dict[str, str]] = {}


class ConfigBuilder:
"""
Expand All @@ -35,6 +36,7 @@ def build(
api_key: Optional[str] = None,
host: Optional[str] = None,
openapi_config: Optional[OpenApiConfiguration] = None,
additional_headers: Optional[Dict[str, str]] = {},
**kwargs,
) -> Config:
api_key = api_key or kwargs.pop("api_key", None) or os.getenv("PINECONE_API_KEY")
Expand All @@ -51,4 +53,4 @@ def build(
or OpenApiConfigFactory.build(api_key=api_key, host=host)
)

return Config(api_key, host, openapi_config)
return Config(api_key, host, openapi_config, additional_headers)
6 changes: 3 additions & 3 deletions pinecone/config/pinecone_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Dict
import os
from .config import ConfigBuilder, Config

Expand All @@ -7,6 +7,6 @@

class PineconeConfig():
@staticmethod
def build(api_key: Optional[str] = None, host: Optional[str] = None, **kwargs) -> Config:
def build(api_key: Optional[str] = None, host: Optional[str] = None, additional_headers: Optional[Dict[str, str]] = {}, **kwargs) -> Config:
host = host or kwargs.get("host") or os.getenv("PINECONE_CONTROLLER_HOST") or DEFAULT_CONTROLLER_HOST
return ConfigBuilder.build(api_key=api_key, host=host, **kwargs)
return ConfigBuilder.build(api_key=api_key, host=host, additional_headers=additional_headers, **kwargs)
6 changes: 5 additions & 1 deletion pinecone/control/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(
api_key: Optional[str] = None,
host: Optional[str] = None,
config: Optional[Config] = None,
additional_headers: Optional[Dict[str, str]] = {},
index_api: Optional[IndexOperationsApi] = None,
**kwargs,
):
Expand All @@ -35,13 +36,16 @@ def __init__(
else:
self.config = configKwarg
else:
self.config = PineconeConfig.build(api_key=api_key, host=host, **kwargs)
self.config = PineconeConfig.build(api_key=api_key, host=host, additional_headers=additional_headers, **kwargs)

if index_api:
self.index_api = index_api
else:
api_client = ApiClient(configuration=self.config.openapi_config)
api_client.user_agent = get_user_agent()
extra_headers = self.config.additional_headers or {}
for key, value in extra_headers.items():
api_client.set_default_header(key, value)
self.index_api = IndexOperationsApi(api_client)

self.index_host_store = IndexHostStore()
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def test_passing_host(self):
p = Pinecone(api_key="123-456-789", host="my-host")
assert p.index_api.api_client.configuration.host == "my-host"

def test_passing_additional_headers(self):
extras = {"header1": "my-value", "header2": "my-value2"}
p = Pinecone(api_key="123-456-789", additional_headers=extras)

for key, value in extras.items():
assert p.index_api.api_client.default_headers[key] == value

@pytest.mark.parametrize("timeout_value, describe_index_responses, expected_describe_index_calls, expected_sleep_calls", [
# When timeout=None, describe_index is called until ready
Expand Down