public static Object Extract( HttpContent content, Type commandType ) { var read = content.ReadAsAsync( commandType ); read.Wait(); //reset the internal stream position to allow the WebAPI pipeline to read it again. content.ReadAsStreamAsync() .ContinueWith( t => { if( t.Result.CanSeek ) { t.Result.Seek( 0, SeekOrigin.Begin ); } } ) .Wait(); return read.Result; }
public async Task ReadAsAsyncOfT_ReadsFromContent_ThenInvokesFormattersReadFromStreamMethod() { Stream contentStream = null; string value = "42"; var contentMock = new Mock <TestableHttpContent> { CallBase = true }; contentMock .Setup( c => c.SerializeToStreamAsyncPublic( It.IsAny <Stream>(), It.IsAny <TransportContext>() ) ) .Returns(TaskHelpers.Completed) .Callback((Stream s, TransportContext _) => contentStream = s) .Verifiable(); HttpContent content = contentMock.Object; content.Headers.ContentType = _mediaType; _formatterMock .Setup( f => f.ReadFromStreamAsync( typeof(string), It.IsAny <Stream>(), It.IsAny <HttpContent>(), It.IsAny <IFormatterLogger>() ) ) .Returns(Task.FromResult <object>(value)); _formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true); var resultValue = await content.ReadAsAsync <string>(_formatters); Assert.Same(value, resultValue); contentMock.Verify(); _formatterMock.Verify( f => f.ReadFromStreamAsync(typeof(string), contentStream, content, null), Times.Once() ); }
//This code is tightly coupled to Templeton. If parsing fails, we capture the full json payload, the error //then log it upstream. I've left the constants here, since this is A) The only place they are used and B) there are a lot of them. //In the future, if we see this being something that is reused, they could be moved. //For a sample response see the large comment at the end of this file. internal async Task<JobDetails> GetJobDetailsFromServerResponse(HttpContent content) { const string userArgsSection = "userargs"; const string defineSection = "define"; const string statusSection = "status"; const string jobNameKey = "hdInsightJobName="; const string statusDirectory = "statusdir"; const string exitCodeValue = "exitValue"; const string startTimeValue = "startTime"; const string jobStatusValue = "runState"; const string hiveQueryValue = "execute"; const string outputFile = "/stdout"; const string errorFile = "/stderr"; Contract.AssertArgNotNull(content, "content"); JObject result = null; try { result = await content.ReadAsAsync<JObject>(); Contract.Assert(result != null); var outputAsvPath = (string)result[userArgsSection][statusDirectory]; var outputFolderUri = GetOutputFolderUri(outputAsvPath); var defines = result[userArgsSection][defineSection].ToArray(); var jobNameItem = (string)defines.First(s => ((string)s).Contains(jobNameKey)); var jobName = jobNameItem.Split('=')[1]; var details = new JobDetails { ExitCode = (int)result[exitCodeValue], SubmissionTime = result[statusSection][startTimeValue].ToString(), Name = jobName, StatusCode = (JobStatusCode)Enum.Parse(typeof(JobStatusCode), result[statusSection][jobStatusValue].ToString()), PhysicalOutputPath = new Uri(outputFolderUri + outputFile), LogicalOutputPath = outputAsvPath + outputFile, ErrorOutputPath = outputFolderUri + errorFile, Query = (string)result[userArgsSection][hiveQueryValue], }; return details; } catch (Exception ex) { var rawJson = string.Empty; if(result != null) { rawJson = result.ToString(); if (rawJson.Length > 4000) { //truncating the response if its large then 4000 char, in order to prevent large data in the logs rawJson = rawJson.Substring(0, 4000); } } throw new HttpParseException(string.Format(JobSubmissionConstants.UnableToParseJobDetailsLogMessage, ex.Message, rawJson)); } }
//This code is tightly coupled to Templeton. If parsing fails, we capture the full json payload, the error //then log it upstream. internal async Task<string> GetJobIdFromServerResponse(HttpContent content) { Contract.AssertArgNotNull(content,"content"); try { var result = await content.ReadAsAsync<JObject>(); Contract.Assert(result != null); JToken jobId; Contract.Assert(result.TryGetValue(JobSubmissionConstants.JobIdPropertyName,out jobId)); Contract.Assert(jobId != null); return jobId.ToString(); } catch (Exception ex) { throw new HttpParseException(ex.Message); } }
//This code is tightly coupled to Templeton. If parsing fails, we capture the full json payload, the error //then log it upstream. internal async Task<List<string>> GetJobIdListFromServerResponse(HttpContent content) { Contract.AssertArgNotNull(content, "content"); try { var result = await content.ReadAsAsync<JArray>(); if (result == null || !result.HasValues) { return new List<string>(); } var ret = result.Values<string>(); return ret.ToList(); } catch (Exception ex) { throw new HttpParseException(ex.Message); } }
/// <summary> /// Returns a <see cref="Task" /> that will yield an object of the specified <paramref name="type" /> /// from the <paramref name="content" /> instance. /// </summary> /// <remarks>This override use the built-in collection of formatters.</remarks> /// <param name="content">The <see cref="HttpContent" /> instance from which to read.</param> /// <param name="type">The type of the object to read.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A task object representing reading the content as an object of the specified type.</returns> public static Task <object?> ReadAsAsync(this HttpContent content, Type type, CancellationToken cancellationToken) { return(content.ReadAsAsync(type, MediaTypeFormatterCollection.Default, cancellationToken)); }
/// <summary> /// Returns a <see cref="Task" /> that will yield an object of the specified /// type <typeparamref name="T" /> from the <paramref name="content" /> instance. /// </summary> /// <remarks>This override use the built-in collection of formatters.</remarks> /// <typeparam name="T">The type of the object to read.</typeparam> /// <param name="content">The <see cref="HttpContent" /> instance from which to read.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A task object representing reading the content as an object of the specified type.</returns> public static Task <T> ReadAsAsync <T>(this HttpContent content, CancellationToken cancellationToken) { return(content.ReadAsAsync <T>(MediaTypeFormatterCollection.Default, cancellationToken)); }
/// <summary> /// Returns a <see cref="Task" /> that will yield an object of the specified /// type <typeparamref name="T" /> from the <paramref name="content" /> instance. /// </summary> /// <remarks>This override use the built-in collection of formatters.</remarks> /// <typeparam name="T">The type of the object to read.</typeparam> /// <param name="content">The <see cref="HttpContent" /> instance from which to read.</param> /// <returns>A task object representing reading the content as an object of the specified type.</returns> public static Task <T> ReadAsAsync <T>(this HttpContent content) { return(content.ReadAsAsync <T>(MediaTypeFormatterCollection.Default)); }
/// <summary> /// Returns a <see cref="Task"/> that will yield an object of the specified <paramref name="type"/> /// from the <paramref name="content"/> instance. /// </summary> /// <remarks>This override use the built-in collection of formatters.</remarks> /// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param> /// <param name="type">The type of the object to read.</param> /// <returns>A task object representing reading the content as an object of the specified type.</returns> public static Task <object> ReadAsAsync(this HttpContent content, Type type) { return(content.ReadAsAsync(type, DefaultMediaTypeFormatterCollection)); }
private static async Task <NameValueCollection> ReadAsAsyncCore(HttpContent content, MediaTypeFormatter[] formatters) { FormDataCollection formData = await content.ReadAsAsync <FormDataCollection>(formatters); return(formData == null ? null : formData.ReadAsNameValueCollection()); }
private static async Task<NameValueCollection> ReadAsAsyncCore(HttpContent content, MediaTypeFormatter[] formatters, CancellationToken cancellationToken) { FormDataCollection formData = await content.ReadAsAsync<FormDataCollection>(formatters, cancellationToken); return formData == null ? null : formData.ReadAsNameValueCollection(); }
public override async Task <object> DecodeAsync(System.Net.Http.HttpContent httpContent, ActionContext actionContext) { return(await httpContent.ReadAsAsync(actionContext.ReturnTypeDescription.TargetObjectType, new[] { actionContext.HttpClientSettings.JsonMediaTypeFormatter }).ConfigureAwait(false)); }