Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="restPipe">Rest pipe.</param>
        /// <param name="httpMethod">Http method.</param>
        /// <param name="onResponse">On answer.</param>
        private static async Task <RestPipeResponse> ExecuteAsync(this RestPipeRequest restPipe, HttpMethod httpMethod, Action <RestPipeResponse> onResponse = null)
        {
            restPipe.Config.HttpMethod = httpMethod;
            var result = await restPipe.ExecuteAsync().ConfigureAwait(false);

            onResponse?.Invoke(result);
            return(new RestPipeResponse(result));
        }
Esempio n. 2
0
        /// <summary>
        /// Executes the async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="restPipe">Rest pipe.</param>
        /// <param name="httpMethod">Http method.</param>
        /// <param name="onResponse">On response.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        /// <exception cref="T:System.NotImplementedException"></exception>
        private static async Task <RestPipeResponse <T> > ExecuteAsync <T>(this RestPipeRequest restPipe, HttpMethod httpMethod, Action <RestPipeResponse <T>, T> onResponse = null)
        {
            restPipe.Config.HttpMethod = httpMethod;
            var result = new RestPipeResponse <T>(await restPipe.ExecuteAsync().ConfigureAwait(false));

            result.Data = default(T);
            if (result.Config.HttpResponseMessage != null && result.Config.HttpResponseMessage.IsSuccessStatusCode)
            {
                #region convert To T
                try
                {
                    var mediaTye = restPipe.Config.HttpResponseMessage.Content.Headers.ContentType.MediaType;
                    if (RestMimeTypeConvertJson.Contains(mediaTye))
                    {
                        result.Data = JsonConvert.DeserializeObject <T>(await restPipe.Config.HttpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false));
                    }
                    else if (RestMimeTypeConvertText.Contains(mediaTye))
                    {
                        if (typeof(string).GetTypeInfo().IsAssignableFrom(typeof(T)))
                        {
                            result.Data = (T)(object)(await restPipe.Config.HttpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false));
                        }
                    }
                    else if (RestMimeTypeConvertRaw.Contains(mediaTye))
                    {
                        if (typeof(byte[]).GetTypeInfo().IsAssignableFrom(typeof(T)))
                        {
                            result.Data = (T)(object)(await restPipe.Config.HttpResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
                        }
                        else if (typeof(Stream).GetTypeInfo().IsAssignableFrom(typeof(T)))
                        {
                            result.Data = (T)(object)(await restPipe.Config.HttpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false));
                        }
                    }
                    else
                    {
                    }
                }
                catch (Exception ex)
                {
                    result.Config.Exception = ex;
                }
                #endregion
            }
            onResponse?.Invoke(result, result.Data);
            return(result);
        }
Esempio n. 3
0
 /// <summary>
 /// Traces the value async.
 /// </summary>
 /// <returns>The value async.</returns>
 /// <param name="restPipe">Rest pipe.</param>
 /// <param name="onResponse">On response.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static async Task <T> TraceValueAsync <T>(this RestPipeRequest restPipe, Action <RestPipeResponse <T>, T> onResponse = null)
 {
     return((await restPipe.ExecuteAsync <T>(HttpMethod.Trace, onResponse).ConfigureAwait(false)).Data);
 }
Esempio n. 4
0
 /// <summary>
 /// Traces the async.
 /// </summary>
 /// <returns>The async.</returns>
 /// <param name="restPipe">Rest pipe.</param>
 /// <param name="onResponse">On response.</param>
 public static async Task <RestPipeResponse> TraceAsync(this RestPipeRequest restPipe, Action <RestPipeResponse> onResponse = null)
 {
     return(await restPipe.ExecuteAsync(HttpMethod.Trace, onResponse).ConfigureAwait(false));
 }
Esempio n. 5
0
 /// <summary>
 /// Optionses the async.
 /// </summary>
 /// <returns>The async.</returns>
 /// <param name="restPipe">Rest pipe.</param>
 /// <param name="onResponse">On response.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static async Task <RestPipeResponse <T> > OptionsResponseAsync <T>(this RestPipeRequest restPipe, Action <RestPipeResponse <T>, T> onResponse = null)
 {
     return(await restPipe.ExecuteAsync <T>(HttpMethod.Options, onResponse).ConfigureAwait(false));
 }