/// <summary> /// Initialize a test /// </summary> /// <param name="resource"></param> /// <response code="200">Test initialized.</response> public HttpResponseMessage Put(HttpRequestMessage request) { string nameValuePairs = request.Content.ReadAsStringAsync().GetAwaiter().GetResult(); Dictionary <string, string> resourceInfo = JsonSerializer.DeserializeDictionary(nameValuePairs); string resourceName = null; resourceInfo.TryGetValue("name", out resourceName); resource resource = resourceName == null ? null : new resource { name = resourceName }; Trace.WriteLine(String.Format("{0:T} - Received PUT request for resource {1}", DateTime.Now, String.IsNullOrWhiteSpace(resourceName) ? "null" : resourceName), this.GetType().Name); if (String.IsNullOrWhiteSpace(resourceName)) { Trace.WriteLine(String.Format("{0:T} - PUT response is BadRequest due to missing name", DateTime.Now), this.GetType().Name); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "resource data { name:'...' } not specified.")); } var correlationId = Guid.NewGuid(); try { var result = ResourceInvoker.DynamicInvokePut(resource); resourceResponse resourceResponse = new resourceResponse { id = correlationId, details = result.ToString() }; Trace.WriteLine(String.Format("{0:T} - PUT response for {1} is:{2}{3}", DateTime.Now, resourceName, Environment.NewLine, resourceResponse.ToString()), this.GetType().Name); // Directly return a json string to avoid use of MediaTypeFormatters HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(resourceResponse.ToString()); response.Content.Headers.ContentType = new MediaTypeHeaderValue(JsonSerializer.JsonMediaType); return(response); } catch (Exception exception) { Trace.WriteLine(String.Format("{0:T} - Exception executing PUT for resource {1}{2}:{3}", DateTime.Now, resourceName, Environment.NewLine, exception.ToString()), this.GetType().Name); return(Request.CreateResponse(HttpStatusCode.InternalServerError, new resourceResponse { id = correlationId, details = exception.Message })); } }
/// <summary> /// Initialize a test /// </summary> /// <param name="resource"></param> /// <response code="200">Test initialized.</response> public HttpResponseMessage Put(HttpRequestMessage request) { string nameValuePairs = request.Content.ReadAsStringAsync().GetAwaiter().GetResult(); Dictionary<string, string> resourceInfo = JsonSerializer.DeserializeDictionary(nameValuePairs); string resourceName = null; resourceInfo.TryGetValue("name", out resourceName); resource resource = resourceName == null ? null : new resource { name = resourceName }; Trace.WriteLine(String.Format("{0:T} - Received PUT request for resource {1}", DateTime.Now, String.IsNullOrWhiteSpace(resourceName) ? "null" : resourceName), this.GetType().Name); if (String.IsNullOrWhiteSpace(resourceName)) { Trace.WriteLine(String.Format("{0:T} - PUT response is BadRequest due to missing name", DateTime.Now), this.GetType().Name); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "resource data { name:'...' } not specified."); } var correlationId = Guid.NewGuid(); try { var result = ResourceInvoker.DynamicInvokePut(resource); resourceResponse resourceResponse = new resourceResponse { id = correlationId, details = result.ToString() }; Trace.WriteLine(String.Format("{0:T} - PUT response for {1} is:{2}{3}", DateTime.Now, resourceName, Environment.NewLine, resourceResponse.ToString()), this.GetType().Name); // Directly return a json string to avoid use of MediaTypeFormatters HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(resourceResponse.ToString()); response.Content.Headers.ContentType = new MediaTypeHeaderValue(JsonSerializer.JsonMediaType); return response; } catch (Exception exception) { Trace.WriteLine(String.Format("{0:T} - Exception executing PUT for resource {1}{2}:{3}", DateTime.Now, resourceName, Environment.NewLine, exception.ToString()), this.GetType().Name); return Request.CreateResponse(HttpStatusCode.InternalServerError, new resourceResponse { id = correlationId, details = exception.Message }); } }
/// <summary> /// Initialize a test /// </summary> /// <param name="resource"></param> /// <response code="200">Test initialized.</response> public HttpResponseMessage Put(HttpRequestMessage request) { string nameValuePairs = request.Content.ReadAsStringAsync().GetAwaiter().GetResult(); Dictionary<string, string> resourceInfo = JsonSerializer.DeserializeDictionary(nameValuePairs); string resourceName = null; resourceInfo.TryGetValue("name", out resourceName); resource resource = resourceName == null ? null : new resource { name = resourceName }; if (resource == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "resource data { name:'...' } not specified."); } var correlationId = Guid.NewGuid(); try { Debug.WriteLine(String.Format("Received request to create resource: {0}", resource)); var result = ResourceInvoker.DynamicInvokePut(resource); resourceResponse resourceResponse = new resourceResponse { id = correlationId, details = result.ToString() }; Debug.WriteLine(String.Format("Resource creation response is: {0}", resourceResponse.ToString())); // Directly return a json string to avoid use of MediaTypeFormatters HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(resourceResponse.ToString()); response.Content.Headers.ContentType = new MediaTypeHeaderValue(JsonSerializer.JsonMediaType); return response; } catch (Exception exception) { Debug.WriteLine("Exception when trying to create resource : " + resource.name + " : " + exception.Message); return Request.CreateResponse(HttpStatusCode.InternalServerError, new resourceResponse { id = correlationId, details = exception.Message }); } }