Skip to content

Commit 21d0755

Browse files
committed
fix API and change internal function
1 parent 4c83e6e commit 21d0755

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

openapi_to_sdk/sdk_automation.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
class PythonSDKBuilder(PythonWriter):
1010
def __init__(self, url: str='', sample_endpoint: str='',
1111
inherited_properties: List[str]=[], json_fn: str=None,
12-
decorators: Dict[str, str]={}, override_param_defaults={}):
12+
decorators: Dict[str, str]={}, override_param_defaults={},
13+
internal_functions: set={}):
1314
"""
1415
Args:
1516
url:
@@ -26,6 +27,7 @@ def __init__(self, url: str='', sample_endpoint: str='',
2627
self.decorators = decorators
2728
self.indent_level = 0
2829
self.override_param_defaults=override_param_defaults
30+
self.internal_functions = internal_functions
2931

3032
def _download_json(self):
3133
return requests.get(self.url + "/openapi.json").json()
@@ -75,16 +77,15 @@ def get_params(self, endpoint_metadata):
7577
def get_request_func(self, endpoint_metadata):
7678
return getattr(requests, self.get_request_type(endpoint_metadata))
7779

78-
def create_endpoint_metadata_string(self, endpoint, include_response_parsing=False, internal_functions=[]):
80+
def create_endpoint_metadata_string(self, endpoint, include_response_parsing=False):
7981
function_name = endpoint.split('/')[-1]
8082
endpoint_metadata = self.data['paths'][endpoint]
8183
body_kwargs = self.get_body_kwargs(endpoint_metadata)
82-
is_internal_function = function_name in internal_functions
8384
return self.get_request_template(
8485
function_name,
8586
endpoint,
8687
self.get_request_type(endpoint_metadata), body_kwargs,
87-
include_response_parsing, is_internal_function=is_internal_function)
88+
include_response_parsing)
8889

8990
def get_body_kwargs(self, endpoint_metadata):
9091
if self.get_request_type(endpoint_metadata) == 'post':
@@ -93,14 +94,14 @@ def get_body_kwargs(self, endpoint_metadata):
9394
return self.get_body_info(endpoint_metadata)
9495

9596
def get_request_template(self, endpoint_metadata_name, endpoint, endpoint_metadata_type,
96-
body_kwargs, include_response_parsing: bool, is_internal_function: bool):
97+
body_kwargs, include_response_parsing: bool):
9798
decorator_string = self.get_decorator_string()
9899
if endpoint_metadata_type == 'post':
99100
return decorator_string + self.get_request_post_template(endpoint_metadata_name,
100-
endpoint, body_kwargs, include_response_parsing, is_internal_function)[0]
101+
endpoint, body_kwargs, include_response_parsing)[0]
101102
elif endpoint_metadata_type == 'get':
102103
return decorator_string + self.get_request_get_template(endpoint_metadata_name,
103-
endpoint, body_kwargs, include_response_parsing, is_internal_function)[0]
104+
endpoint, body_kwargs, include_response_parsing)[0]
104105

105106
def create_documentation(self, endpoint):
106107
documentation = ''
@@ -132,8 +133,13 @@ def get_default_value_from_override(self, param):
132133
if param['name'] in self.override_param_defaults:
133134
return self.override_param_defaults[param['name']]
134135
return self.missing_value
136+
137+
def get_default_value_from_override_by_param_name(self, param_name: str):
138+
if param_name in self.override_param_defaults.keys():
139+
return self.override_param_defaults[param_name]
140+
return self.missing_value
135141

136-
def get_default_value_in_param(self, param):
142+
def get_default_value_in_param(self, param=None):
137143
default_value = self.get_default_value_from_override(param)
138144
if default_value != self.missing_value:
139145
return default_value
@@ -142,8 +148,6 @@ def get_default_value_in_param(self, param):
142148
if 'schema' in param.keys():
143149
if 'default' in param['schema']:
144150
return param['schema']['default']
145-
# if 'minimum' in param['schema']:
146-
# return param['schema']['minimum']
147151
return self.missing_value
148152

149153
@property
@@ -164,8 +168,9 @@ def internal_function_prefix(self):
164168
return "_"
165169

166170
def get_request_get_template(self, endpoint_metadata_name, endpoint, body_kwargs,
167-
include_response_parsing: bool=False, is_internal_function: bool=False):
171+
include_response_parsing: bool=False):
168172
string = self.add_indent() + f"""def """
173+
is_internal_function = endpoint_metadata_name in self.internal_functions
169174
if is_internal_function:
170175
string += self.internal_function_prefix
171176
string += f"""{endpoint_metadata_name}(self,"""
@@ -214,18 +219,22 @@ def indenter(self):
214219
return '\t'
215220

216221
def get_request_post_template(self, endpoint_metadata_name, endpoint, body_kwargs,
217-
include_response_parsing=False, is_internal_function: bool=False):
222+
include_response_parsing=False):
218223
string = self.add_indent() + f"""def """
224+
is_internal_function = endpoint_metadata_name in self.internal_functions
219225
if is_internal_function:
220226
string += self.internal_function_prefix
221-
string += f"""{endpoint_metadata_name}(self,"""
222-
string = self.add_indent() + f"""def {endpoint_metadata_name}(self,"""
227+
string += f"""{endpoint_metadata_name}(self, """
228+
# string = self.add_indent() + f"""def {endpoint_metadata_name}(self,"""
223229
# Store default parameters so you can add them last
224230
default_parameters = {}
225231
for k, v in body_kwargs:
226232
if k in self.inherited_properties:
227233
continue
228-
default_parameter = self.get_default_value_in_param(v)
234+
if k in self.override_param_defaults.keys():
235+
default_parameter = self.get_default_value_from_override_by_param_name(k)
236+
else:
237+
default_parameter = self.get_default_value_in_param(v)
229238
if default_parameter != self.missing_value:
230239
if isinstance(default_parameter, str):
231240
default_parameter = '"' + str(default_parameter) + '"'
@@ -248,7 +257,7 @@ def get_request_post_template(self, endpoint_metadata_name, endpoint, body_kwarg
248257
for k, v in body_kwargs:
249258
if 'default' in v.keys():
250259
default_arguments.append(v['default'])
251-
continue
260+
string += self.add_indent() + k + '=' + k + ', '
252261
elif k in self.inherited_properties:
253262
string += self.add_indent() + k + '=' + 'self.' + k + ','
254263
else:
@@ -290,8 +299,7 @@ def create_function_string(self):
290299
endpoint_metadatas_dict.update({new_func.__name__: func_string})
291300
return endpoint_metadatas_dict
292301

293-
def to_python_file(self, class_name, filename='api.py', import_strings=[], include_response_parsing=True,
294-
internal_functions=[]):
302+
def to_python_file(self, class_name, filename='api.py', import_strings=[], include_response_parsing=True):
295303
"""
296304
Args:
297305
class_name: THe name of the class
@@ -307,7 +315,6 @@ def to_python_file(self, class_name, filename='api.py', import_strings=[], inclu
307315
func_string = self.create_endpoint_metadata_string(
308316
path,
309317
include_response_parsing=include_response_parsing,
310-
internal_functions=internal_functions
311318
)
312319
func_strings.append(func_string)
313320
self.write_python_instance_methods(func_strings, filename=filename)

tests/test_vectorai.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ def test_smoke():
1919
# 'retry()',
2020
"return_curl_or_response('json')"],
2121
override_param_defaults={'min_score':None},
22-
)
23-
sdk.to_python_file(
24-
class_name="ViAPIClient",
25-
filename='api.py',
26-
import_strings=['import requests', 'from vectorai.api.utils import retry, return_curl_or_response'],
2722
internal_functions=[
2823
"list_collections",
2924
"create_collection",
3025
"search"
3126
],
27+
)
28+
sdk.to_python_file(
29+
class_name="ViAPIClient",
30+
filename='api.py',
31+
import_strings=['import requests', 'from vectorai.api.utils import retry, return_curl_or_response'],
3232
include_response_parsing=False
3333
)
3434

0 commit comments

Comments
 (0)