//---------------------------------------------------------------------------------------------- // // Licensed under the MIT License. See LICENSE.TXT in the project root license information. // //---------------------------------------------------------------------------------------------- using System; using System.IO; #if !WINDOWS_UWP using System.Net.Http; using System.Net.Http.Headers; #endif // !WINDOWS_UWP using System.Runtime.Serialization.Json; using System.Threading.Tasks; #if WINDOWS_UWP using Windows.Foundation; using Windows.Networking; using Windows.Security.Credentials; using Windows.Storage.Streams; using Windows.Web.Http; using Windows.Web.Http.Filters; using Windows.Web.Http.Headers; #endif // WINDOWS_UWP namespace Microsoft.Tools.WindowsDevicePortal { /// /// HTTP PUT Wrapper /// public partial class DevicePortal { /// /// Calls the specified API with the provided body. This signature leaves /// off the optional response so callers who don't need a response body /// don't need to specify a type for it, which also would force them /// to explicitly declare their bodyData type instead of letting it /// be implied implicitly. /// /// The type of the data for the HTTP request body. /// The relative portion of the uri path that specifies the API to call. /// The data to be used for the HTTP request body. /// The query string portion of the uri path that provides the parameterized data. /// Task tracking the PUT completion. public async Task PutAsync( string apiPath, K bodyData, string payload = null) where K : class { await this.PutAsync(apiPath, bodyData, payload); } /// /// Calls the specified API with the provided body. /// /// The type of the data for the HTTP response body (if present). /// The type of the data for the HTTP request body. /// The relative portion of the uri path that specifies the API to call. /// The data to be used for the HTTP request body. /// The query string portion of the uri path that provides the parameterized data. /// Task tracking the PUT completion, optional response body. public async Task PutAsync( string apiPath, K bodyData = null, string payload = null) where T : new() where K : class { Uri uri = Utilities.BuildEndpoint( this.deviceConnection.Connection, apiPath, payload); #if WINDOWS_UWP HttpStreamContent streamContent = null; #else StreamContent streamContent = null; #endif // WINDOWS_UWP if (bodyData != null) { // Serialize the body to a JSON stream DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(K)); Stream stream = new MemoryStream(); serializer.WriteObject(stream, bodyData); stream.Seek(0, SeekOrigin.Begin); #if WINDOWS_UWP streamContent = new HttpStreamContent(stream.AsInputStream()); streamContent.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json"); #else streamContent = new StreamContent(stream); streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); #endif // WINDOWS_UWP } return ReadJsonStream(await this.PutAsync(uri, streamContent)); } } }