9
9
class PythonSDKBuilder (PythonWriter ):
10
10
def __init__ (self , url : str = '' , sample_endpoint : str = '' ,
11
11
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 = {}):
13
14
"""
14
15
Args:
15
16
url:
@@ -26,6 +27,7 @@ def __init__(self, url: str='', sample_endpoint: str='',
26
27
self .decorators = decorators
27
28
self .indent_level = 0
28
29
self .override_param_defaults = override_param_defaults
30
+ self .internal_functions = internal_functions
29
31
30
32
def _download_json (self ):
31
33
return requests .get (self .url + "/openapi.json" ).json ()
@@ -75,16 +77,15 @@ def get_params(self, endpoint_metadata):
75
77
def get_request_func (self , endpoint_metadata ):
76
78
return getattr (requests , self .get_request_type (endpoint_metadata ))
77
79
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 ):
79
81
function_name = endpoint .split ('/' )[- 1 ]
80
82
endpoint_metadata = self .data ['paths' ][endpoint ]
81
83
body_kwargs = self .get_body_kwargs (endpoint_metadata )
82
- is_internal_function = function_name in internal_functions
83
84
return self .get_request_template (
84
85
function_name ,
85
86
endpoint ,
86
87
self .get_request_type (endpoint_metadata ), body_kwargs ,
87
- include_response_parsing , is_internal_function = is_internal_function )
88
+ include_response_parsing )
88
89
89
90
def get_body_kwargs (self , endpoint_metadata ):
90
91
if self .get_request_type (endpoint_metadata ) == 'post' :
@@ -93,14 +94,14 @@ def get_body_kwargs(self, endpoint_metadata):
93
94
return self .get_body_info (endpoint_metadata )
94
95
95
96
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 ):
97
98
decorator_string = self .get_decorator_string ()
98
99
if endpoint_metadata_type == 'post' :
99
100
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 ]
101
102
elif endpoint_metadata_type == 'get' :
102
103
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 ]
104
105
105
106
def create_documentation (self , endpoint ):
106
107
documentation = ''
@@ -132,8 +133,13 @@ def get_default_value_from_override(self, param):
132
133
if param ['name' ] in self .override_param_defaults :
133
134
return self .override_param_defaults [param ['name' ]]
134
135
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
135
141
136
- def get_default_value_in_param (self , param ):
142
+ def get_default_value_in_param (self , param = None ):
137
143
default_value = self .get_default_value_from_override (param )
138
144
if default_value != self .missing_value :
139
145
return default_value
@@ -142,8 +148,6 @@ def get_default_value_in_param(self, param):
142
148
if 'schema' in param .keys ():
143
149
if 'default' in param ['schema' ]:
144
150
return param ['schema' ]['default' ]
145
- # if 'minimum' in param['schema']:
146
- # return param['schema']['minimum']
147
151
return self .missing_value
148
152
149
153
@property
@@ -164,8 +168,9 @@ def internal_function_prefix(self):
164
168
return "_"
165
169
166
170
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 ):
168
172
string = self .add_indent () + f"""def """
173
+ is_internal_function = endpoint_metadata_name in self .internal_functions
169
174
if is_internal_function :
170
175
string += self .internal_function_prefix
171
176
string += f"""{ endpoint_metadata_name } (self,"""
@@ -214,18 +219,22 @@ def indenter(self):
214
219
return '\t '
215
220
216
221
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 ):
218
223
string = self .add_indent () + f"""def """
224
+ is_internal_function = endpoint_metadata_name in self .internal_functions
219
225
if is_internal_function :
220
226
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,"""
223
229
# Store default parameters so you can add them last
224
230
default_parameters = {}
225
231
for k , v in body_kwargs :
226
232
if k in self .inherited_properties :
227
233
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 )
229
238
if default_parameter != self .missing_value :
230
239
if isinstance (default_parameter , str ):
231
240
default_parameter = '"' + str (default_parameter ) + '"'
@@ -248,7 +257,7 @@ def get_request_post_template(self, endpoint_metadata_name, endpoint, body_kwarg
248
257
for k , v in body_kwargs :
249
258
if 'default' in v .keys ():
250
259
default_arguments .append (v ['default' ])
251
- continue
260
+ string += self . add_indent () + k + '=' + k + ', '
252
261
elif k in self .inherited_properties :
253
262
string += self .add_indent () + k + '=' + 'self.' + k + ','
254
263
else :
@@ -290,8 +299,7 @@ def create_function_string(self):
290
299
endpoint_metadatas_dict .update ({new_func .__name__ : func_string })
291
300
return endpoint_metadatas_dict
292
301
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 ):
295
303
"""
296
304
Args:
297
305
class_name: THe name of the class
@@ -307,7 +315,6 @@ def to_python_file(self, class_name, filename='api.py', import_strings=[], inclu
307
315
func_string = self .create_endpoint_metadata_string (
308
316
path ,
309
317
include_response_parsing = include_response_parsing ,
310
- internal_functions = internal_functions
311
318
)
312
319
func_strings .append (func_string )
313
320
self .write_python_instance_methods (func_strings , filename = filename )
0 commit comments