diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index 97430fe1d..02752571f 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -7,7 +7,7 @@ from pinecone.core.client.api.manage_indexes_api import ManageIndexesApi as IndexOperationsApi from pinecone.core.client.api_client import ApiClient -from pinecone.utils import get_user_agent +from pinecone.utils import get_user_agent, normalize_host from pinecone.core.client.models import ( CreateCollectionRequest, CreateIndexRequest, @@ -213,7 +213,15 @@ def _get_status(self, name: str): response = api_instance.describe_index(name) return response["status"] - def Index(self, name: str, host: Optional[str] = None): - if host is None: - host = self.index_host_store.get_host(self.index_api, self.config, name) - return Index(api_key=self.config.api_key, host=host, pool_threads=self.pool_threads) + def Index(self, name: str = '', host: str = ''): + if name == '' and host == '': + raise ValueError("Either name or host must be specified") + + if host != '': + # Use host url if it is provided + return Index(api_key=self.config.api_key, host=normalize_host(host), pool_threads=self.pool_threads) + + if name != '': + # Otherwise, get host url from describe_index using the index name + index_host = self.index_host_store.get_host(self.index_api, self.config, name) + return Index(api_key=self.config.api_key, host=index_host, pool_threads=self.pool_threads) diff --git a/pinecone/grpc/pinecone.py b/pinecone/grpc/pinecone.py index 378c07631..7279b1078 100644 --- a/pinecone/grpc/pinecone.py +++ b/pinecone/grpc/pinecone.py @@ -1,11 +1,19 @@ from ..control.pinecone import Pinecone from ..config.config import ConfigBuilder from .index_grpc import GRPCIndex -from typing import Optional class PineconeGRPC(Pinecone): - def Index(self, name: str, host: Optional[str] = None): - if host is None: - host = self.index_host_store.get_host(self.index_api, self.config, name) - config = ConfigBuilder.build(api_key=self.config.api_key, host=host) - return GRPCIndex(index_name=name, config=config) + def Index(self, name: str = '', host: str = ''): + if name == '' and host == '': + raise ValueError("Either name or host must be specified") + + if host != '': + # Use host if it is provided + config = ConfigBuilder.build(api_key=self.config.api_key, host=host) + return GRPCIndex(index_name=name, config=config) + + if name != '': + # Otherwise, get host url from describe_index using the index name + index_host = self.index_host_store.get_host(self.index_api, self.config, name) + config = ConfigBuilder.build(api_key=self.config.api_key, host=index_host) + return GRPCIndex(index_name=name, config=config) diff --git a/tests/integration/data/test_initialization.py b/tests/integration/data/test_initialization.py new file mode 100644 index 000000000..06273f0ad --- /dev/null +++ b/tests/integration/data/test_initialization.py @@ -0,0 +1,38 @@ +import pytest + +class TestIndexClientInitialization: + def test_index_direct_host_kwarg(self, client, index_host): + index = client.Index(host=index_host) + index.fetch(ids=['1', '2', '3']) + + def test_index_direct_host_with_https(self, client, index_host): + if not index_host.startswith('https://'): + index_host = 'https://' + index_host + index = client.Index(host=index_host) + index.fetch(ids=['1', '2', '3']) + + def test_index_direct_host_without_https(self, client, index_host): + if index_host.startswith('https://'): + index_host = index_host[8:] + index = client.Index(host=index_host) + index.fetch(ids=['1', '2', '3']) + + def test_index_by_name_positional_only(self, client, index_name, index_host): + index = client.Index(index_name) + index.fetch(ids=['1', '2', '3']) + + def test_index_by_name_positional_with_host(self, client, index_name, index_host): + index = client.Index(index_name, index_host) + index.fetch(ids=['1', '2', '3']) + + def test_index_by_name_kwargs(self, client, index_name): + index = client.Index(name=index_name) + index.fetch(ids=['1', '2', '3']) + + def test_index_by_name_kwargs_with_host(self, client, index_name, index_host): + index = client.Index(name=index_name, host=index_host) + index.fetch(ids=['1', '2', '3']) + + def test_raises_when_no_name_or_host(self, client, index_host): + with pytest.raises(ValueError): + client.Index() \ No newline at end of file