示例#1
0
        public async Task Should_Get_X_Users_From_Offset_Y()
        {
            var result = await redmineManager.GetPaginatedObjectsAsync <User>(new NameValueCollection()
            {
                { RedmineKeys.INCLUDE, "groups, memberships" },
                { RedmineKeys.LIMIT, limit },
                { RedmineKeys.OFFSET, offset }
            });

            Assert.IsNotNull(result);
            CollectionAssert.AllItemsAreNotNull(result.Objects, "contains null user!");
            CollectionAssert.AllItemsAreUnique(result.Objects, "users not unique!");
        }
        /// <summary>
        ///     Gets the objects asynchronous.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redmineManager">The redmine manager.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns></returns>
        public static async Task <List <T> > GetObjectsAsync <T>(this RedmineManager redmineManager, NameValueCollection parameters)
            where T : class, new()
        {
            int      totalCount = 0, pageSize, offset;
            List <T> resultList = null;

            if (parameters == null)
            {
                parameters = new NameValueCollection();
            }

            int.TryParse(parameters[RedmineKeys.LIMIT], out pageSize);
            int.TryParse(parameters[RedmineKeys.OFFSET], out offset);
            if (pageSize == default(int))
            {
                pageSize = redmineManager.PageSize > 0
                    ? redmineManager.PageSize
                    : RedmineManager.DEFAULT_PAGE_SIZE_VALUE;
                parameters.Set(RedmineKeys.LIMIT, pageSize.ToString(CultureInfo.InvariantCulture));
            }
            try
            {
                do
                {
                    parameters.Set(RedmineKeys.OFFSET, offset.ToString(CultureInfo.InvariantCulture));
                    var tempResult = await redmineManager.GetPaginatedObjectsAsync <T>(parameters).ConfigureAwait(false);

                    if (tempResult != null)
                    {
                        if (resultList == null)
                        {
                            resultList = new List <T>(tempResult.Items);
                            totalCount = tempResult.TotalItems;
                        }
                        else
                        {
                            resultList.AddRange(tempResult.Items);
                        }
                    }
                    offset += pageSize;
                } while (offset < totalCount);
            }
            catch (WebException wex)
            {
                wex.HandleWebException(redmineManager.Serializer);
            }
            return(resultList);
        }
        public static Task <List <T> > GetObjectsAsync <T>(this RedmineManager redmineManager, NameValueCollection parameters) where T : class, new()
        {
            var task = Task.Factory.StartNew(() =>
            {
                int totalCount      = 0, pageSize;
                List <T> resultList = null;
                if (parameters == null)
                {
                    parameters = new NameValueCollection();
                }
                int offset = 0;
                int.TryParse(parameters[RedmineKeys.LIMIT], out pageSize);
                if (pageSize == default(int))
                {
                    pageSize = redmineManager.PageSize > 0 ? redmineManager.PageSize : 25;
                    parameters.Set(RedmineKeys.LIMIT, pageSize.ToString(CultureInfo.InvariantCulture));
                }
                do
                {
                    parameters.Set(RedmineKeys.OFFSET, offset.ToString(CultureInfo.InvariantCulture));
                    var requestTask = redmineManager.GetPaginatedObjectsAsync <T>(parameters).ContinueWith(t =>
                    {
                        if (t.Result != null)
                        {
                            if (resultList == null)
                            {
                                resultList = t.Result.Objects;
                                totalCount = t.Result.TotalCount;
                            }
                            else
                            {
                                resultList.AddRange(t.Result.Objects);
                            }
                        }
                        offset += pageSize;
                    });
                    requestTask.Wait(TimeSpan.FromMilliseconds(5000));
                } while (offset < totalCount);
                return(resultList);
            });

            return(task);
        }