@@ -75,6 +75,25 @@ def __str__(self) -> str:
7575 return f"NestedPath(path={ self ._path } )"
7676
7777
78+ class RootPath (Path ):
79+ """
80+ Path to use when the root of the response is an array.
81+ """
82+
83+ def write (self , template : List [Dict [str , Any ]], value : List [Dict [str , Any ]]) -> None :
84+ template .extend (value )
85+
86+ def update (self , template : List [Dict [str , Any ]], value : List [Any ]) -> None :
87+ template .clear ()
88+ template .extend (value )
89+
90+ def extract (self , template : List [Dict [str , Any ]]) -> Any :
91+ return template
92+
93+ def __str__ (self ) -> str :
94+ return f"RootPath"
95+
96+
7897class PaginationStrategy (ABC ):
7998 @abstractmethod
8099 def update (self , response : Dict [str , Any ]) -> None :
@@ -95,7 +114,7 @@ def __init__(
95114 self ,
96115 template : Dict [str , Any ],
97116 id_path : Optional [Path ],
98- cursor_path : Optional [Union [FieldPath , NestedPath ]],
117+ cursor_path : Optional [Union [FieldPath , NestedPath , RootPath ]],
99118 ):
100119 self ._record = template
101120 self ._id_path = id_path
@@ -150,7 +169,7 @@ class HttpResponseBuilder:
150169 def __init__ (
151170 self ,
152171 template : Dict [str , Any ],
153- records_path : Union [FieldPath , NestedPath ],
172+ records_path : Union [FieldPath , NestedPath , RootPath ],
154173 pagination_strategy : Optional [PaginationStrategy ],
155174 ):
156175 self ._response = template
@@ -208,9 +227,9 @@ def find_binary_response(resource: str, execution_folder: str) -> bytes:
208227
209228def create_record_builder (
210229 response_template : Dict [str , Any ],
211- records_path : Union [FieldPath , NestedPath ],
230+ records_path : Union [FieldPath , NestedPath , RootPath ],
212231 record_id_path : Optional [Path ] = None ,
213- record_cursor_path : Optional [Union [FieldPath , NestedPath ]] = None ,
232+ record_cursor_path : Optional [Union [FieldPath , NestedPath , RootPath ]] = None ,
214233) -> RecordBuilder :
215234 """
216235 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
@@ -231,7 +250,7 @@ def create_record_builder(
231250
232251def create_response_builder (
233252 response_template : Dict [str , Any ],
234- records_path : Union [FieldPath , NestedPath ],
253+ records_path : Union [FieldPath , NestedPath , RootPath ],
235254 pagination_strategy : Optional [PaginationStrategy ] = None ,
236255) -> HttpResponseBuilder :
237256 return HttpResponseBuilder (response_template , records_path , pagination_strategy )
0 commit comments