/// <summary> /// Invoke a CaaS API operation using a HTTP POST request. /// </summary> /// <typeparam name="TResult"> /// The XML-serialisable data contract type into which the response will be deserialised. /// </typeparam> /// <param name="relativeOperationUri"> /// The operation URI (relative to the CaaS API's base URI). /// </param> /// <param name="content"> /// The content that will be deserialised and passed in the body of the POST request. /// </param> /// <returns> /// The operation result. /// </returns> public async Task <TResult> PostAsync <TResult>(Uri relativeOperationUri, string content) { var textformatter = new TextMediaTypeFormatter(); var objectContent = new ObjectContent <string>( content, textformatter, "application/x-www-form-urlencoded"); try { using ( HttpResponseMessage response = await _httpClient.PostAsync(relativeOperationUri, objectContent)) { if (!response.IsSuccessStatusCode) { await HandleApiRequestErrors(response); } return(await ReadResponseAsync <TResult>(response.Content)); } } catch (HttpRequestException ex) { throw new ComputeApiHttpException(new Uri(_httpClient.BaseAddress, relativeOperationUri), HttpMethod.Post, ex); } }
public void TextMediaTypeFormatter_CannotWriteAnyType() { var mediaTypeFormatter = new TextMediaTypeFormatter(); Assert.False(mediaTypeFormatter.CanWriteType(typeof(string))); Assert.False(mediaTypeFormatter.CanWriteType(typeof(int))); }
public void TextMediaTypeFormatter_ReadsTypesTextAndNotOtherTypes() { var mediaTypeFormatter = new TextMediaTypeFormatter(); Assert.True(mediaTypeFormatter.CanReadType(typeof(string))); Assert.False(mediaTypeFormatter.CanReadType(typeof(int))); }
public void TextMediaTypeFormatter_AddsSupportForPlainText() { var mediaTypeFormatter = new TextMediaTypeFormatter(); Assert.NotNull(mediaTypeFormatter.SupportedMediaTypes.FirstOrDefault(x => x.MediaType == "text/xml")); Assert.NotNull(mediaTypeFormatter.SupportedMediaTypes.FirstOrDefault(x => x.MediaType == "text/plain")); Assert.NotNull(mediaTypeFormatter.SupportedMediaTypes.FirstOrDefault(x => x.MediaType == "text/javascript")); }
public void TextMediaTypeFormatter_CanReadTextStream(string encoding) { var mediaTypeFormatter = new TextMediaTypeFormatter(); var expected = "this is my text £!"; HttpContent content = new StringContent(expected, Encoding.GetEncoding(encoding)); var formatterLogger = new Mock <IFormatterLogger>(); var result = mediaTypeFormatter.ReadFromStreamAsync(typeof(string), content.ReadAsStreamAsync().Result, content, formatterLogger.Object); Assert.Equal(result.Result.ToString(), expected); }
/// <summary> /// Invoke a CaaS API operation using a HTTP POST request. /// </summary> /// <typeparam name="TResult"> /// The XML-serialisable data contract type into which the response will be deserialised. /// </typeparam> /// <param name="relativeOperationUri"> /// The operation URI (relative to the CaaS API's base URI). /// </param> /// <param name="content"> /// The content that will be deserialised and passed in the body of the POST request. /// </param> /// <returns> /// The operation result. /// </returns> public async Task <TResult> PostAsync <TResult>(Uri relativeOperationUri, string content) { var textformatter = new TextMediaTypeFormatter(); var objectContent = new ObjectContent <string>( content, textformatter, "application/x-www-form-urlencoded"); using ( HttpResponseMessage response = await _httpClient.PostAsync(relativeOperationUri, objectContent)) { if (!response.IsSuccessStatusCode) { await HandleApiRequestErrors(response, relativeOperationUri); } return(await response.Content.ReadAsAsync <TResult>(_mediaTypeFormatters)); } }
private void Listen(object state) { AutoResetEvent are = (AutoResetEvent)state; OnMessage += (o, a) => { MediaTypeFormatter formatter = null; if (a.ContentType == "application/octet-stream") { formatter = new BinaryMediaTypeFormatter(); } else if (a.ContentType == "text/plain") { formatter = new TextMediaTypeFormatter(); } else if (a.ContentType == "application/xml" || a.ContentType == "text/xml") { formatter = new XmlMediaTypeFormatter(); } else if (a.ContentType == "application/json" || a.ContentType == "text/json") { formatter = new JsonMediaTypeFormatter(); } else { throw new SkunkLab.Protocols.Coap.UnsupportedMediaTypeException("Media type formatter not available."); } if (a.ContentType != "application/octet-stream") { response = Request.CreateResponse <string>(HttpStatusCode.OK, Encoding.UTF8.GetString(a.Message), formatter); } else { response = Request.CreateResponse <byte[]>(HttpStatusCode.OK, a.Message, formatter); } response.Headers.Add("x-sl-resource", a.ResourceUriString); are.Set(); }; }