@@ -114,7 +114,7 @@ def __init__(
114114 self ,
115115 template : Dict [str , Any ],
116116 id_path : Optional [Path ],
117- cursor_path : Optional [Union [FieldPath , NestedPath , RootPath ]],
117+ cursor_path : Optional [Union [FieldPath , NestedPath ]],
118118 ):
119119 self ._record = template
120120 self ._id_path = id_path
@@ -168,12 +168,14 @@ def build(self) -> Dict[str, Any]:
168168class HttpResponseBuilder :
169169 def __init__ (
170170 self ,
171- template : Dict [str , Any ],
171+ template : Union [ Dict [str , Any ], List [ Dict [ str , Any ]] ],
172172 records_path : Union [FieldPath , NestedPath , RootPath ],
173173 pagination_strategy : Optional [PaginationStrategy ],
174174 ):
175- self ._response = template
175+ _validate_path_with_response (records_path , template )
176+
176177 self ._records : List [RecordBuilder ] = []
178+ self ._response = template
177179 self ._records_path = records_path
178180 self ._pagination_strategy = pagination_strategy
179181 self ._status_code = 200
@@ -188,6 +190,9 @@ def with_pagination(self) -> "HttpResponseBuilder":
188190 "`pagination_strategy` was not provided and hence, fields related to the pagination can't be modified. Please provide "
189191 "`pagination_strategy` while instantiating ResponseBuilder to leverage this capability"
190192 )
193+ elif isinstance (self ._response , List ):
194+ raise ValueError ("pagination_strategy requires the response to be a dict but was list" )
195+
191196 self ._pagination_strategy .update (self ._response )
192197 return self
193198
@@ -196,7 +201,7 @@ def with_status_code(self, status_code: int) -> "HttpResponseBuilder":
196201 return self
197202
198203 def build (self ) -> HttpResponse :
199- self ._records_path .update (self ._response , [record .build () for record in self ._records ])
204+ self ._records_path .update (self ._response , [record .build () for record in self ._records ]) # type: ignore # validated using _validate_path_with_response
200205 return HttpResponse (json .dumps (self ._response ), self ._status_code )
201206
202207
@@ -234,8 +239,9 @@ def create_record_builder(
234239 """
235240 This will use the first record define at `records_path` as a template for the records. If more records are defined, they will be ignored
236241 """
242+ _validate_path_with_response (records_path , response_template )
237243 try :
238- record_template = records_path .extract (response_template )[0 ]
244+ record_template = records_path .extract (response_template )[0 ] # type: ignore # validated using _validate_path_with_response
239245 if not record_template :
240246 raise ValueError (
241247 f"Could not extract any record from template at path `{ records_path } `. "
@@ -249,8 +255,15 @@ def create_record_builder(
249255
250256
251257def create_response_builder (
252- response_template : Dict [str , Any ],
258+ response_template : Union [ Dict [str , Any ], List [ Dict [ str , Any ]] ],
253259 records_path : Union [FieldPath , NestedPath , RootPath ],
254260 pagination_strategy : Optional [PaginationStrategy ] = None ,
255261) -> HttpResponseBuilder :
256262 return HttpResponseBuilder (response_template , records_path , pagination_strategy )
263+
264+
265+ def _validate_path_with_response (records_path : Union [FieldPath , NestedPath , RootPath ], response_template : Union [Dict [str , Any ], List [Dict [str , Any ]]]) -> None :
266+ if isinstance (response_template , List ) and not isinstance (records_path , RootPath ):
267+ raise ValueError ("templates of type lists require RootPath" )
268+ elif isinstance (response_template , Dict ) and not isinstance (records_path , (FieldPath , NestedPath )):
269+ raise ValueError ("templates of type dict either require FieldPath or NestedPath" )
0 commit comments