/// <summary>
        /// Executes a <see cref="HttpMethod.Get"/> request returning the type specified by <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">The type of the <see cref="CloudFlareResponse{T}.Result"/>.</typeparam>
        public static async Task <T> GetCloudFlareResultAsync <T>(
            this HttpClient client,
            Uri uri,
            CloudFlareAuth auth,
            CancellationToken cancellationToken)
            where T : class
        {
            CloudFlareResponse <T> cloudFlareResponse =
                await client.GetCloudFlareResponseAsync <T>(uri, auth, cancellationToken).ConfigureAwait(false);

            return(cloudFlareResponse.Result);
        }
Example #2
0
        private static async Task <IEnumerable <TResult> > GetAllPagedResultsAsync <TResult, TParams, TOrder>(
            Func <CancellationToken, CloudFlareAuth, TParams, Task <CloudFlareResponse <IReadOnlyList <TResult> > > > request,
            CancellationToken cancellationToken,
            CloudFlareAuth auth,
            int maxPerPage,
            TParams parameters = null)
            where TResult : class
            where TParams : PagedParameters <TOrder>
            where TOrder : struct
        {
            IEnumerable <TResult> result = Enumerable.Empty <TResult>();
            int page = 0;
            CloudFlareResultInfo resultInfo;

            do
            {
                ++page;

                // Create the paged parameters;
                JObject jsonParams = JObject.FromObject(new { page, per_page = maxPerPage });

                // If parameters have been supplied use them, but override the paged parameters into them.
                if (parameters != null)
                {
                    JObject mergedParams = JObject.FromObject(parameters);
                    mergedParams.Merge(jsonParams);
                    jsonParams = mergedParams;
                }

                TParams pagedParams = jsonParams.ToObject <TParams>();
                CloudFlareResponse <IReadOnlyList <TResult> > response =
                    await request(
                        cancellationToken,
                        auth,
                        pagedParams).ConfigureAwait(false);

                result     = result.Concat(response.Result);
                resultInfo = response.ResultInfo;
            }while (resultInfo.Page < resultInfo.TotalPages);

            return(result);
        }
        /// <summary>
        /// Executes a <see cref="HttpMethod.Get"/> request returning the <see cref="CloudFlareResponse{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of the <see cref="CloudFlareResponse{T}.Result"/>.</typeparam>
        public static async Task <CloudFlareResponse <T> > GetCloudFlareResponseAsync <T>(
            this HttpClient client,
            Uri uri,
            CloudFlareAuth auth,
            CancellationToken cancellationToken)
            where T : class
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }
            if (auth == null)
            {
                throw new ArgumentNullException(nameof(auth));
            }

            using (var request = new HttpRequestMessage(HttpMethod.Get, uri))
            {
                request.AddAuth(auth);

                HttpResponseMessage response =
                    await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                    .ConfigureAwait(false);

                using (response)
                {
                    CloudFlareResponse <T> cloudFlareResponse =
                        await response.ReadCloudFlareResponseAsync <T>(cancellationToken).ConfigureAwait(false);

                    return(cloudFlareResponse);
                }
            }
        }