Skip to content

Commit 2c7334e

Browse files
authored
[WIP] KE compatibility (#457)
## Problem While cleaning up and simplifying the Configuration class (which originated from generated code but is now something we maintain by hand for greater control), a congif attribute in use by the assistants plugin was accidentally removed. ## Solution Restore config attributes being used by assistants ## Type of Change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update - [ ] Infrastructure change (CI configs, etc) - [ ] Non-code change (docs, etc) - [ ] None of the above: (explain here) ## Test Plan Describe specific steps for validating this change.
2 parents d91e31b + 1747b07 commit 2c7334e

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

pinecone/openapi_support/configuration.py

+66-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class Configuration:
6161
disabled. This can be useful to troubleshoot data validation problem, such as
6262
when the OpenAPI document validation rules do not match the actual API data
6363
received by the server.
64+
:param server_operation_index: Mapping from operation ID to an index to server
65+
configuration.
66+
:param server_operation_variables: Mapping from operation ID to a mapping with
67+
string values to replace variables in templated server configuration.
68+
The validation of enums is performed for variables with defined enum values before.
6469
:param ssl_ca_cert: str - the path to a file of concatenated CA certificates
6570
in PEM format
6671
@@ -95,13 +100,22 @@ def __init__(
95100
api_key_prefix=None,
96101
discard_unknown_keys=False,
97102
disabled_client_side_validations="",
103+
server_index=None,
104+
server_variables=None,
105+
server_operation_index=None,
106+
server_operation_variables=None,
98107
ssl_ca_cert=None,
99108
):
100109
"""Constructor"""
101110
self._base_path = "https://api.pinecone.io" if host is None else host
102111
"""Default Base url
103112
"""
104-
113+
self.server_index = 0 if server_index is None and host is None else server_index
114+
self.server_operation_index = server_operation_index or {}
115+
"""Default server index
116+
"""
117+
self.server_variables = server_variables or {}
118+
self.server_operation_variables = server_operation_variables or {}
105119
"""Default server variables
106120
"""
107121
self.temp_folder_path = None
@@ -353,10 +367,56 @@ def auth_settings(self):
353367
}
354368
return auth
355369

370+
def get_host_settings(self):
371+
"""Gets an array of host settings
372+
373+
:return: An array of host settings
374+
"""
375+
return [{"url": "https://api.pinecone.io", "description": "Production API endpoints"}]
376+
377+
def get_host_from_settings(self, index, variables=None, servers=None):
378+
"""Gets host URL based on the index and variables
379+
:param index: array index of the host settings
380+
:param variables: hash of variable and the corresponding value
381+
:param servers: an array of host settings or None
382+
:return: URL based on host settings
383+
"""
384+
if index is None:
385+
return self._base_path
386+
387+
variables = {} if variables is None else variables
388+
servers = self.get_host_settings() if servers is None else servers
389+
390+
try:
391+
server = servers[index]
392+
except IndexError:
393+
raise ValueError(
394+
"Invalid index {0} when selecting the host settings. Must be less than {1}".format(
395+
index, len(servers)
396+
)
397+
)
398+
399+
url = server["url"]
400+
401+
# go through variables and replace placeholders
402+
for variable_name, variable in server.get("variables", {}).items():
403+
used_value = variables.get(variable_name, variable["default_value"])
404+
405+
if "enum_values" in variable and used_value not in variable["enum_values"]:
406+
raise ValueError(
407+
"The variable `{0}` in the host URL has invalid value {1}. Must be {2}.".format(
408+
variable_name, variables[variable_name], variable["enum_values"]
409+
)
410+
)
411+
412+
url = url.replace("{" + variable_name + "}", used_value)
413+
414+
return url
415+
356416
@property
357417
def host(self):
358418
"""Return generated host."""
359-
return self._base_path
419+
return self.get_host_from_settings(self.server_index, variables=self.server_variables)
360420

361421
@host.setter
362422
def host(self, value):
@@ -372,6 +432,10 @@ def __repr__(self):
372432
f"connection_pool_maxsize={self.connection_pool_maxsize}",
373433
f"discard_unknown_keys={self.discard_unknown_keys}",
374434
f"disabled_client_side_validations={self.disabled_client_side_validations}",
435+
f"server_index={self.server_index}",
436+
f"server_variables={self.server_variables}",
437+
f"server_operation_index={self.server_operation_index}",
438+
f"server_operation_variables={self.server_operation_variables}",
375439
f"ssl_ca_cert={self.ssl_ca_cert}",
376440
]
377441
return f"Configuration({', '.join(attrs)})"

0 commit comments

Comments
 (0)