Beispiel #1
0
        /// <summary>
        /// Triggers the specified sync and async event handlers, usually defined on
        /// </summary>
        public static Task RaiseEventAsync(HttpRequestMessage request, FlurlEventType eventType)
        {
            var call = HttpCall.Get(request);

            if (call == null)
            {
                return(NoOpTask.Instance);
            }

            var settings = call.Settings;

            if (settings == null)
            {
                return(NoOpTask.Instance);
            }

            switch (eventType)
            {
            case FlurlEventType.BeforeCall:
                return(HandleEventAsync(settings.BeforeCall, settings.BeforeCallAsync, call));

            case FlurlEventType.AfterCall:
                return(HandleEventAsync(settings.AfterCall, settings.AfterCallAsync, call));

            case FlurlEventType.OnError:
                return(HandleEventAsync(settings.OnError, settings.OnErrorAsync, call));

            default:
                return(NoOpTask.Instance);
            }
        }
        /// <summary>
        /// Deserializes JSON-formatted HTTP response body to object of type T. Intended to chain off an async HTTP.
        /// </summary>
        /// <typeparam name="T">A type whose structure matches the expected JSON response.</typeparam>
        /// <returns>A Task whose result is an object containing data in the response body.</returns>
        /// <example>x = await url.PostAsync(data).ReceiveJson&lt;T&gt;()</example>
        public static async Task <T> ReceiveJson <T>(this Task <HttpResponseMessage> response)
        {
            var resp = await response.ConfigureAwait(false);

            var call = HttpCall.Get(resp.RequestMessage);

            try {
                using (var stream = await resp.Content.ReadAsStreamAsync().ConfigureAwait(false))
                    return(call.Settings.JsonSerializer.Deserialize <T>(stream));
            }
            catch (Exception ex) {
                call.Exception = ex;
                throw new FlurlHttpException(call);
            }
        }
        /// <summary>
        /// Deserializes JSON-formatted HTTP response body to object of type T. Intended to chain off an async HTTP.
        /// </summary>
        /// <typeparam name="T">A type whose structure matches the expected JSON response.</typeparam>
        /// <returns>A Task whose result is an object containing data in the response body.</returns>
        /// <example>x = await url.PostAsync(data).ReceiveJson&lt;T&gt;()</example>
        /// <exception cref="FlurlHttpException">Condition.</exception>
        public static async Task <T> ReceiveJson <T>(this Task <HttpResponseMessage> response)
        {
            var resp = await response.ConfigureAwait(false);

            if (resp == null)
            {
                return(default(T));
            }

            var call = HttpCall.Get(resp.RequestMessage);

            using (var stream = await resp.Content.ReadAsStreamAsync().ConfigureAwait(false)) {
                try {
                    return(call.FlurlRequest.Settings.JsonSerializer.Deserialize <T>(stream));
                }
                catch (Exception ex) {
                    call.Exception = new FlurlParsingException(call, "JSON", ex);
                    await FlurlRequest.HandleExceptionAsync(call, call.Exception, CancellationToken.None).ConfigureAwait(false);

                    return(default(T));
                }
            }
        }