예제 #1
0
        /// <summary>
        /// Executes a request and returns the given type. Throws an exception when the response is invalid.
        /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class.
        /// </summary>
        /// <param name="baseClient">
        /// The request to be executed. Note that this request will be automatically disposed when the method returns.
        /// </param>
        /// <param name="method">
        /// HTTP method to be used by the request.
        /// </param>
        /// <param name="baseContent">
        /// Content that gets appended to the request body. Can be null. In most cases, you'll want to use <see cref="JsonContent"/>.
        /// Note that the content will be automatically disposed when the method returns.
        /// </param>
        /// <remarks>
        /// This method will automatically dispose the <paramref name="baseClient"/> and <paramref name="baseContent" /> when finished.
        /// </remarks>
        protected async Task <T> ExecuteRequestAsync <T>(IFlurlClient baseClient, HttpMethod method, JsonContent baseContent = null, string rootElement = null) where T : new()
        {
            var policyResult = await _ExecutionPolicy.Run <T>(baseClient, baseContent, async (client, content) =>
            {
                var request   = client.SendAsync(method, content);
                var response  = await request;
                var rawResult = await request.ReceiveString();

                //Check for and throw exception when necessary.
                CheckResponseExceptions(response, rawResult);

                // This method may fail when the method was Delete, which is intendend.
                // Delete methods should not be parsing the response JSON and should instead
                // be using the non-generic ExecuteRequestAsync.
                var reader = new JsonTextReader(new StringReader(rawResult));
                var data   = _Serializer.Deserialize <JObject>(reader).SelectToken(rootElement);
                var result = data.ToObject <T>();
                return(new RequestResult <T>(response, result, rawResult));
            });

            baseClient.Dispose();
            baseContent?.Dispose();

            return(policyResult);
        }
예제 #2
0
        /// <summary>
        /// Executes a request and returns a JToken for querying. Throws an exception when the response is invalid.
        /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class.
        /// </summary>
        /// <remarks>
        /// This method will automatically dispose the <paramref name="baseClient"/> and <paramref name="baseContent" /> when finished.
        /// </remarks>
        protected async Task <JToken> ExecuteRequestAsync(IFlurlClient baseClient, HttpMethod method, JsonContent baseContent = null)
        {
            var policyResult = await _ExecutionPolicy.Run(baseClient, baseContent, async (client, content) =>
            {
                var request   = client.SendAsync(method, content);
                var response  = await request;
                var rawResult = await request.ReceiveString();

                //Check for and throw exception when necessary.
                CheckResponseExceptions(response, rawResult);

                JToken jtoken = null;

                // Don't parse the result when the request was Delete.
                if (method != HttpMethod.Delete)
                {
                    jtoken = JToken.Parse(rawResult);
                }

                return(new RequestResult <JToken>(response, jtoken, rawResult));
            });

            baseClient.Dispose();
            baseContent?.Dispose();

            return(policyResult);
        }