Esempio n. 1
0
        /// <summary>
        /// Creates requests for each pageToken executes all in a <see cref="MultiRequest"/> then orders the result
        /// </summary>
        private async Task <IEnumerable <TResponseItem> > CreateAndExecuteMultiRequest(
            IEnumerator <PageTokenObject> pageTokens, CancellationToken cancellationToken = default(CancellationToken))
        {
            var results      = new ConcurrentDictionary <int, IEnumerable <TResponseItem> >();
            var multiRequest = new MultiRequest()
            {
                RequiresAuth = true
            };

            var callback = new MultiRequest.OnResponse <TResponse>(
                (content, error, index, message) =>
            {
                if (error == null)
                {
                    results.TryAdd(index, content.GetResponseItems <TResponseItem>());
                }
                //TODO: allow user to access the error infomation
            });

            while (pageTokens.MoveNext())
            {
                this.ListRequestBuilder.PageToken = pageTokens.Current.PageToken;
                multiRequest.Queue(ListRequestBuilder.CreateRequest(), callback);
            }

            await multiRequest.ExecuteAsync(cancellationToken).ConfigureAwait(false);

            return(results.OrderBy(k => k.Key).SelectMany(x => x.Value));
        }
Esempio n. 2
0
        /// <summary>
        /// Executes multiple requests relating to a single type of request, useful for when more than 50 items items will be requested.
        /// <example>
        /// Example: When requesting more than 50 videos using <see cref="VideosResource.ListRequest"/> <see cref="items"/> will be the ids for the videos and <see cref="parameterName"/> will be "Id", <see cref="VideosResource.ListRequest.Id"/>
        /// </example>
        /// </summary>
        /// <param name="items">The list of items to request</param>
        /// <param name="parameterName">name of request Property that the items are associated with</param>
        /// <param name="cancellationToken"></param>
        /// <exception cref="ArgumentException"></exception>
        /// <returns></returns>
        public async Task <IEnumerable <TResponseItem> > ExecuteConcurrentFromParameters(IEnumerable <string> items,
                                                                                         string parameterName, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (ListRequestBuilder.GetType().GetProperty(parameterName) == null)
            {
                throw new ArgumentException(parameterName + " is not a valid parameterName for " + typeof(TRequest), nameof(parameterName));
            }

            var results      = new ConcurrentDictionary <int, IEnumerable <TResponseItem> >();
            var multiRequest = new MultiRequest()
            {
                RequiresAuth = true
            };

            var callback = new MultiRequest.OnResponse <TResponse>(
                (content, error, index, message) =>
            {
                if (error == null)
                {
                    results.TryAdd(index, content.GetResponseItems <TResponseItem>());
                }
            });

            foreach (var value in items.Chunk(50).Select(chunk => string.Join(",", chunk))) //each 50 items as csv
            {
                ListRequestBuilder.GetType().GetProperty(parameterName).SetValue(ListRequestBuilder, value);
                multiRequest.Queue(ListRequestBuilder.CreateRequest(), callback);
            }

            await multiRequest.ExecuteAsync(cancellationToken).ConfigureAwait(false);

            return(results.OrderBy(k => k.Key).SelectMany(x => x.Value));
        }