@@ -61,6 +61,11 @@ class Configuration:
61
61
disabled. This can be useful to troubleshoot data validation problem, such as
62
62
when the OpenAPI document validation rules do not match the actual API data
63
63
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.
64
69
:param ssl_ca_cert: str - the path to a file of concatenated CA certificates
65
70
in PEM format
66
71
@@ -95,13 +100,22 @@ def __init__(
95
100
api_key_prefix = None ,
96
101
discard_unknown_keys = False ,
97
102
disabled_client_side_validations = "" ,
103
+ server_index = None ,
104
+ server_variables = None ,
105
+ server_operation_index = None ,
106
+ server_operation_variables = None ,
98
107
ssl_ca_cert = None ,
99
108
):
100
109
"""Constructor"""
101
110
self ._base_path = "https://api.pinecone.io" if host is None else host
102
111
"""Default Base url
103
112
"""
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 {}
105
119
"""Default server variables
106
120
"""
107
121
self .temp_folder_path = None
@@ -353,10 +367,56 @@ def auth_settings(self):
353
367
}
354
368
return auth
355
369
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
+
356
416
@property
357
417
def host (self ):
358
418
"""Return generated host."""
359
- return self ._base_path
419
+ return self .get_host_from_settings ( self . server_index , variables = self . server_variables )
360
420
361
421
@host .setter
362
422
def host (self , value ):
@@ -372,6 +432,10 @@ def __repr__(self):
372
432
f"connection_pool_maxsize={ self .connection_pool_maxsize } " ,
373
433
f"discard_unknown_keys={ self .discard_unknown_keys } " ,
374
434
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 } " ,
375
439
f"ssl_ca_cert={ self .ssl_ca_cert } " ,
376
440
]
377
441
return f"Configuration({ ', ' .join (attrs )} )"
0 commit comments