示例#1
0
        /// <inheritdoc />
        public async Task <T> GetJsonAsync <T>()
        {
            if (_streamRead)
            {
                return(_capturedBody is T body ? body : default(T));
            }

            var call = ResponseMessage.RequestMessage.GetFlurlCall();

            _serializer = call.Request.Settings.JsonSerializer;
            using (var stream = await ResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false)) {
                try {
                    _capturedBody = _serializer.Deserialize <T>(stream);
                    _streamRead   = true;
                    return((T)_capturedBody);
                }
                catch (Exception ex) {
                    _serializer   = null;
                    _capturedBody = await ResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

                    _streamRead    = true;
                    call.Exception = new FlurlParsingException(call, "JSON", ex);
                    await FlurlRequest.HandleExceptionAsync(call, call.Exception, CancellationToken.None).ConfigureAwait(false);

                    return(default(T));
                }
            }
        }
示例#2
0
        /// <inheritdoc />
        public async Task <T> GetJsonAsync <T>()
        {
            var call = ResponseMessage.RequestMessage.GetHttpCall();

            using (var stream = await ResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false)) {
                try {
                    return(call.FlurlRequest.Settings.JsonSerializer.Deserialize <T>(stream));
                }
                catch (Exception ex) {
                    var body = await ResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

                    call.Exception = new FlurlParsingException(call, "JSON", body, ex);
                    await FlurlRequest.HandleExceptionAsync(call, call.Exception, CancellationToken.None).ConfigureAwait(false);

                    return(default(T));
                }
            }
        }
        /// <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 = resp.RequestMessage.GetHttpCall();

            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));
                }
            }
        }