Skip to content

Commit 1f1fe24

Browse files
authored
ref: Improve maintainability for baked request and response (#729)
1 parent e4df29f commit 1f1fe24

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

linodecli/baked/request.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
Request details for a CLI Operation
33
"""
44

5+
from typing import List, Optional
6+
7+
from openapi3.paths import MediaType
58
from openapi3.schemas import Schema
69

710
from linodecli.baked.parsing import simplify_description
11+
from linodecli.baked.response import OpenAPIResponse
812
from linodecli.baked.util import _aggregate_schema_properties
913

1014

@@ -13,16 +17,16 @@ class OpenAPIRequestArg:
1317
A single argument to a request as defined by a Schema in the OpenAPI spec
1418
"""
1519

16-
def __init__(
20+
def __init__( # pylint: disable=too-many-arguments
1721
self,
18-
name,
19-
schema,
20-
required,
21-
prefix=None,
22-
is_parent=False,
23-
parent=None,
24-
depth=0,
25-
): # pylint: disable=too-many-arguments
22+
name: str,
23+
schema: Schema,
24+
required: bool,
25+
prefix: Optional[str] = None,
26+
is_parent: bool = False,
27+
parent: Optional[str] = None,
28+
depth: int = 0,
29+
) -> None:
2630
"""
2731
Parses a single Schema node into a argument the CLI can use when making
2832
requests.
@@ -120,9 +124,14 @@ def __init__(
120124
)
121125

122126

123-
def _parse_request_model(schema, prefix=None, parent=None, depth=0):
127+
def _parse_request_model(
128+
schema: Schema,
129+
prefix: Optional[str] = None,
130+
parent: Optional[str] = None,
131+
depth: int = 0,
132+
) -> List[OpenAPIRequestArg]:
124133
"""
125-
Parses a schema into a list of OpenAPIRequest objects
134+
Parses an OpenAPI schema into a list of OpenAPIRequest objects
126135
:param schema: The schema to parse as a request model
127136
:type schema: openapi3.Schema
128137
:param prefix: The prefix to add to all keys in this schema, as a json path
@@ -143,6 +152,7 @@ def _parse_request_model(schema, prefix=None, parent=None, depth=0):
143152
return args
144153

145154
for k, v in properties.items():
155+
# Handle nested objects which aren't read-only and have properties
146156
if (
147157
v.type == "object"
148158
and not v.readOnly
@@ -159,6 +169,8 @@ def _parse_request_model(schema, prefix=None, parent=None, depth=0):
159169
# parent arguments.
160170
depth=depth,
161171
)
172+
173+
# Handle arrays of objects that not marked as JSON
162174
elif (
163175
v.type == "array"
164176
and v.items
@@ -209,7 +221,7 @@ class OpenAPIRequest:
209221
on the MediaType object of a requestBody portion of an OpenAPI Operation
210222
"""
211223

212-
def __init__(self, request):
224+
def __init__(self, request: MediaType) -> None:
213225
"""
214226
:param request: The request's MediaType object in the OpenAPI spec,
215227
corresponding to the application/json data the endpoint
@@ -256,7 +268,7 @@ class OpenAPIFilteringRequest:
256268
endpoints where filters are accepted.
257269
"""
258270

259-
def __init__(self, response_model):
271+
def __init__(self, response_model: OpenAPIResponse) -> None:
260272
"""
261273
:param response_model: The parsed response model whose properties may be
262274
filterable.

linodecli/baked/response.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
Converting the processed OpenAPI Responses into something the CLI can work with
33
"""
44

5+
from typing import Optional
6+
57
from openapi3.paths import MediaType
8+
from openapi3.schemas import Schema
69

710
from linodecli.baked.util import _aggregate_schema_properties
811

@@ -30,7 +33,13 @@ class OpenAPIResponseAttr:
3033
from it.
3134
"""
3235

33-
def __init__(self, name, schema, prefix=None, nested_list_depth=0):
36+
def __init__(
37+
self,
38+
name: str,
39+
schema: Schema,
40+
prefix: Optional[str] = None,
41+
nested_list_depth: int = 0,
42+
) -> None:
3443
"""
3544
:param name: The key that held this schema in the properties list, representing
3645
its name in a response.
@@ -84,10 +93,13 @@ def __init__(self, name, schema, prefix=None, nested_list_depth=0):
8493
self.item_type = schema.items.type
8594

8695
@property
87-
def path(self):
96+
def path(self) -> str:
8897
"""
8998
This is a helper for filterable fields to return the json path to this
9099
element in a response.
100+
101+
:returns: The json path to the element in a response.
102+
:rtype: str
91103
"""
92104
return self.name
93105

@@ -129,6 +141,7 @@ def render_value(self, model, colorize=True):
129141
value = str(value)
130142
color = self.color_map.get(value) or self.color_map["default_"]
131143
value = f"[{color}]{value}[/]"
144+
# Convert None value to an empty string for better display
132145
if value is None:
133146
# Prints the word None if you don't change it
134147
value = ""
@@ -194,12 +207,14 @@ def _parse_response_model(schema, prefix=None, nested_list_depth=0):
194207
elif v.type == "object":
195208
attrs += _parse_response_model(v, prefix=pref)
196209
elif v.type == "array" and v.items.type == "object":
210+
# Parse arrays for objects recursively and increase the nesting depth
197211
attrs += _parse_response_model(
198212
v.items,
199213
prefix=pref,
200214
nested_list_depth=nested_list_depth + 1,
201215
)
202216
else:
217+
# Handle any other simple types
203218
attrs.append(
204219
OpenAPIResponseAttr(
205220
k, v, prefix=prefix, nested_list_depth=nested_list_depth
@@ -215,7 +230,7 @@ class OpenAPIResponse:
215230
responses section of an OpenAPI Operation
216231
"""
217232

218-
def __init__(self, response: MediaType):
233+
def __init__(self, response: MediaType) -> None:
219234
"""
220235
:param response: The response's MediaType object in the OpenAPI spec,
221236
corresponding to the application/json response type
@@ -287,15 +302,22 @@ def _fix_nested_list(self, json):
287302

288303
nested_lists = [c.strip() for c in self.nested_list.split(",")]
289304
result = []
305+
290306
for nested_list in nested_lists:
291307
path_parts = nested_list.split(".")
308+
292309
if not isinstance(json, list):
293310
json = [json]
311+
294312
for cur in json:
313+
# Get the nested list using the path
295314
nlist_path = cur
296315
for p in path_parts:
297316
nlist_path = nlist_path.get(p)
298317
nlist = nlist_path
318+
319+
# For each item in the nested list,
320+
# combine the parent properties with the nested item
299321
for item in nlist:
300322
cobj = {k: v for k, v in cur.items() if k != path_parts[0]}
301323
cobj["_split"] = path_parts[-1]

0 commit comments

Comments
 (0)