public static async Task <CommentListResponse> ExecuteAllAsync(this CommentsResource.ListRequest request, CancellationToken ct = default(CancellationToken)) { request.MaxResults = request.MaxResults ?? 100; var response = await request.ExecuteAsync(ct); if (!response.Items.Any()) { return(response); } var collection = response.Items.ToList(); while (!ct.IsCancellationRequested) { if (string.IsNullOrWhiteSpace(response.NextPageToken)) { break; } request.PageToken = response.NextPageToken; response = await request.ExecuteAsync(ct); if (response.Items.Any()) { collection.AddRange(response.Items); } } response.Items = collection; return(response); }
/// <summary> /// Gets the comments for the specified comment thread. /// </summary> /// <param name="commentThread">The comment thread to get comments for</param> /// <param name="maxResults">The maximum results to return</param> /// <returns>The list of comments</returns> public async Task <IEnumerable <Comment> > GetCommentsForCommentThread(CommentThread commentThread, int maxResults = 1) { Validator.ValidateVariable(commentThread, "commentThread"); return(await this.YouTubeServiceWrapper(async() => { List <Comment> results = new List <Comment>(); string pageToken = null; do { CommentsResource.ListRequest request = this.connection.GoogleYouTubeService.Comments.List("snippet"); request.ParentId = commentThread.Id; request.MaxResults = Math.Min(maxResults, 50); request.PageToken = pageToken; CommentListResponse response = await request.ExecuteAsync(); results.AddRange(response.Items); maxResults -= response.Items.Count; pageToken = response.NextPageToken; } while (maxResults > 0 && !string.IsNullOrEmpty(pageToken)); return results; })); }