@@ -247,66 +247,82 @@ void HandleStateRequest(HttpListenerRequest request, HttpListenerResponse respon
247247 /// <param name="response">The HTTP response, which will get a No Content response.</param>
248248 void HandleAgentProfileRequest ( HttpListenerRequest request , HttpListenerResponse response )
249249 {
250- var method = new HttpMethod ( request . HttpMethod ) ;
250+ var method = new HttpMethod ( request . HttpMethod ) . ToString ( ) ;
251251 var profileIdString = request . QueryString [ "profileId" ] ;
252252 AgentProfileDocument profileDocument = null ;
253-
254- if ( method == HttpMethod . Get )
253+ try
255254 {
256- if ( string . IsNullOrWhiteSpace ( profileIdString ) )
255+ switch ( method )
257256 {
258- // todo: return all available IDs
257+ case string m when HttpMethod . Get . ToString ( ) == method :
258+ if ( string . IsNullOrWhiteSpace ( profileIdString ) )
259+ {
260+ // todo: return all available IDs
259261#pragma warning disable CA1303 // Do not pass literals as localized parameters
260- throw new NotImplementedException ( "GET requests for all documents are not yet implemented" ) ;
262+ throw new NotImplementedException ( "GET requests for all documents are not yet implemented" ) ;
261263#pragma warning restore CA1303 // Do not pass literals as localized parameters
262- }
264+ }
265+
266+ if ( serverDelegate != null )
267+ {
268+ profileDocument = serverDelegate . AgentProfileDocumentForProfileId ( profileIdString ) ;
269+ }
270+
271+ WriteToStream ( response , profileDocument . content , ContentType . Json , HttpStatusCode . OK ) ;
272+ return ;
273+ case string m when HttpMethod . Post . ToString ( ) == method :
274+ case string m1 when HttpMethod . Put . ToString ( ) == method :
275+ profileDocument = new AgentProfileDocument
276+ {
277+ id = WebUtility . UrlDecode ( profileIdString ) ,
278+ } ;
279+
280+ var agentString = WebUtility . UrlDecode ( request . QueryString [ "agent" ] ) ;
281+
282+ if ( agentString != null )
283+ {
284+ profileDocument . agent = new Agent ( new StringOfJSON ( agentString ) ) ;
285+ }
286+
287+ Encoding encoding = request . ContentEncoding ;
288+ var bytes = new byte [ request . ContentLength64 ] ;
289+ request . InputStream . Read ( bytes , 0 , ( int ) request . ContentLength64 ) ;
290+ profileDocument . content = bytes ;
291+ profileDocument . contentType = request . ContentType ;
292+ RaiseAgentProfileDocumentEvent ( new AgentProfileDocumentEventArgs ( profileDocument ) ) ;
293+
294+ if ( serverDelegate != null )
295+ {
296+ serverDelegate . AlterAgentProfileResponse ( request , ref response , ref profileDocument ) ;
297+ }
263298
264- if ( serverDelegate != null )
265- {
266- profileDocument = serverDelegate . AgentProfileDocumentForProfileId ( profileIdString ) ;
267- }
268- }
269- else if ( method == HttpMethod . Delete )
270- {
271- // todo: implement delete requests
299+ SendResponse ( response , HttpStatusCode . NoContent ) ;
300+ return ;
301+ case string m when HttpMethod . Delete . ToString ( ) == method :
302+ if ( serverDelegate != null )
303+ {
304+ serverDelegate . AlterAgentProfileResponse ( request , ref response , ref profileDocument ) ;
305+ SendResponse ( response , HttpStatusCode . NoContent ) ;
306+ }
307+ else
308+ {
309+ // todo: implement delete requests
272310#pragma warning disable CA1303 // Do not pass literals as localized parameters
273- throw new NotImplementedException ( "DELETE requests are not yet implemented" ) ;
311+ throw new NotImplementedException ( "DELETE requests are not yet implemented" ) ;
274312#pragma warning restore CA1303 // Do not pass literals as localized parameters
275- }
276- else if ( method == HttpMethod . Put || method == HttpMethod . Post )
277- {
278- profileDocument = new AgentProfileDocument
279- {
280- id = WebUtility . UrlDecode ( profileIdString ) ,
281- } ;
282-
283- var agentString = WebUtility . UrlDecode ( request . QueryString [ "agent" ] ) ;
313+ }
284314
285- if ( agentString != null )
286- {
287- profileDocument . agent = new Agent ( new StringOfJSON ( agentString ) ) ;
315+ return ;
316+ default :
317+ throw new InvalidOperationException ( $ "Only GET, DELETE, PUT, and POST are supported for { nameof ( HandleAgentProfileRequest ) } . Received { method } " ) ;
288318 }
289-
290- Encoding encoding = request . ContentEncoding ;
291- var bytes = new byte [ request . ContentLength64 ] ;
292- request . InputStream . Read ( bytes , 0 , ( int ) request . ContentLength64 ) ;
293- profileDocument . content = bytes ;
294- profileDocument . contentType = request . ContentType ;
295-
296- RaiseAgentProfileDocumentEvent ( new AgentProfileDocumentEventArgs ( profileDocument ) ) ;
297- }
298- else
299- {
300- throw new InvalidOperationException ( $ "Only GET, DELETE, PUT, and POST are supported for { nameof ( HandleAgentProfileRequest ) } . Received { method } ") ;
301319 }
302320
303- if ( method == HttpMethod . Put || method == HttpMethod . Post || method == HttpMethod . Delete )
304- {
305- SendResponse ( response , HttpStatusCode . NoContent ) ;
306- }
307- else
321+ #pragma warning disable CA1031 // Do not catch general exception types
322+ catch
323+ #pragma warning restore CA1031 // Do not catch general exception types
308324 {
309- WriteToStream ( response , profileDocument . content , ContentType . Json , HttpStatusCode . OK ) ;
325+ SendResponse ( response , HttpStatusCode . BadRequest ) ;
310326 }
311327 }
312328
0 commit comments