@@ -19,6 +19,7 @@ public enum CrudApiActionType
1919 Create ,
2020 GetAll ,
2121 GetOne ,
22+ GetMany ,
2223 Merge ,
2324 Update ,
2425 Delete
@@ -85,7 +86,6 @@ private void LoadData()
8586 var dataFilePath = Path . GetFullPath ( ProxyUtils . ReplacePathTokens ( _configuration . DataFile ) , Path . GetDirectoryName ( _proxyConfiguration ? . ConfigFile ?? string . Empty ) ?? string . Empty ) ;
8687 if ( ! File . Exists ( dataFilePath ) )
8788 {
88- _logger ? . LogWarn ( $ "File { dataFilePath } not found. CRUD API will be disabled") ;
8989 _configuration . Actions = Array . Empty < CrudApiAction > ( ) ;
9090 return ;
9191 }
@@ -166,68 +166,128 @@ private void GetAll(SessionEventArgs e, CrudApiAction action, IDictionary<string
166166
167167 private void GetOne ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
168168 {
169- var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
170- if ( item is null )
169+ try
171170 {
172- SendNotFoundResponse ( e ) ;
173- _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
174- return ;
171+ var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
172+ if ( item is null )
173+ {
174+ SendNotFoundResponse ( e ) ;
175+ _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
176+ return ;
177+ }
178+
179+ SendJsonResponse ( JsonConvert . SerializeObject ( item , Formatting . Indented ) , HttpStatusCode . OK , e ) ;
180+ _logger ? . LogRequest ( [ $ "200 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
175181 }
182+ catch ( Exception ex )
183+ {
184+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
185+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
186+ }
187+ }
176188
177- SendJsonResponse ( JsonConvert . SerializeObject ( item , Formatting . Indented ) , HttpStatusCode . OK , e ) ;
178- _logger ? . LogRequest ( [ $ "200 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
189+ private void GetMany ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
190+ {
191+ try
192+ {
193+ var items = _data ? . SelectTokens ( ReplaceParams ( action . Query , parameters ) ) ;
194+ if ( items is null )
195+ {
196+ items = Array . Empty < JToken > ( ) ;
197+ }
198+
199+ SendJsonResponse ( JsonConvert . SerializeObject ( items , Formatting . Indented ) , HttpStatusCode . OK , e ) ;
200+ _logger ? . LogRequest ( [ $ "200 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
201+ }
202+ catch ( Exception ex )
203+ {
204+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
205+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
206+ }
179207 }
180208
181209 private void Create ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
182210 {
183- _data ? . Add ( JObject . Parse ( e . HttpClient . Request . BodyString ) ) ;
184- SendJsonResponse ( JsonConvert . SerializeObject ( e . HttpClient . Request . BodyString , Formatting . Indented ) , HttpStatusCode . Created , e ) ;
185- _logger ? . LogRequest ( [ $ "201 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
211+ try
212+ {
213+ _data ? . Add ( JObject . Parse ( e . HttpClient . Request . BodyString ) ) ;
214+ SendJsonResponse ( JsonConvert . SerializeObject ( e . HttpClient . Request . BodyString , Formatting . Indented ) , HttpStatusCode . Created , e ) ;
215+ _logger ? . LogRequest ( [ $ "201 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
216+ }
217+ catch ( Exception ex )
218+ {
219+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
220+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
221+ }
186222 }
187223
188224 private void Merge ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
189225 {
190- var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
191- if ( item is null )
226+ try
227+ {
228+ var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
229+ if ( item is null )
230+ {
231+ SendNotFoundResponse ( e ) ;
232+ _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
233+ return ;
234+ }
235+ var update = JObject . Parse ( e . HttpClient . Request . BodyString ) ;
236+ ( ( JContainer ) item ) ? . Merge ( update ) ;
237+ SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
238+ _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
239+ }
240+ catch ( Exception ex )
192241 {
193- SendNotFoundResponse ( e ) ;
194- _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
195- return ;
242+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
243+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
196244 }
197- var update = JObject . Parse ( e . HttpClient . Request . BodyString ) ;
198- ( ( JContainer ) item ) ? . Merge ( update ) ;
199- SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
200- _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
201245 }
202246
203247 private void Update ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
204248 {
205- var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
206- if ( item is null )
249+ try
250+ {
251+ var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
252+ if ( item is null )
253+ {
254+ SendNotFoundResponse ( e ) ;
255+ _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
256+ return ;
257+ }
258+ var update = JObject . Parse ( e . HttpClient . Request . BodyString ) ;
259+ ( ( JContainer ) item ) ? . Replace ( update ) ;
260+ SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
261+ _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
262+ }
263+ catch ( Exception ex )
207264 {
208- SendNotFoundResponse ( e ) ;
209- _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
210- return ;
265+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
266+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
211267 }
212- var update = JObject . Parse ( e . HttpClient . Request . BodyString ) ;
213- ( ( JContainer ) item ) ? . Replace ( update ) ;
214- SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
215- _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
216268 }
217269
218270 private void Delete ( SessionEventArgs e , CrudApiAction action , IDictionary < string , string > parameters )
219271 {
220- var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
221- if ( item is null )
272+ try
222273 {
223- SendNotFoundResponse ( e ) ;
224- _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
225- return ;
226- }
274+ var item = _data ? . SelectToken ( ReplaceParams ( action . Query , parameters ) ) ;
275+ if ( item is null )
276+ {
277+ SendNotFoundResponse ( e ) ;
278+ _logger ? . LogRequest ( [ $ "404 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
279+ return ;
280+ }
227281
228- item ? . Remove ( ) ;
229- SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
230- _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
282+ item ? . Remove ( ) ;
283+ SendEmptyResponse ( HttpStatusCode . NoContent , e ) ;
284+ _logger ? . LogRequest ( [ $ "204 { action . Url } "] , MessageType . Mocked , new LoggingContext ( e ) ) ;
285+ }
286+ catch ( Exception ex )
287+ {
288+ SendJsonResponse ( JsonConvert . SerializeObject ( ex , Formatting . Indented ) , HttpStatusCode . InternalServerError , e ) ;
289+ _logger ? . LogRequest ( [ $ "500 { action . Url } "] , MessageType . Failed , new LoggingContext ( e ) ) ;
290+ }
231291 }
232292
233293 private Tuple < Action < SessionEventArgs , CrudApiAction , IDictionary < string , string > > , CrudApiAction , IDictionary < string , string > > ? GetMatchingActionHandler ( Request request )
@@ -291,6 +351,7 @@ private void Delete(SessionEventArgs e, CrudApiAction action, IDictionary<string
291351 CrudApiActionType . Create => Create ,
292352 CrudApiActionType . GetAll => GetAll ,
293353 CrudApiActionType . GetOne => GetOne ,
354+ CrudApiActionType . GetMany => GetMany ,
294355 CrudApiActionType . Merge => Merge ,
295356 CrudApiActionType . Update => Update ,
296357 CrudApiActionType . Delete => Delete ,
0 commit comments