Esempio n. 1
0
        public async Task <FilteredProjectList> ListProjects(
            ProjectFilter filter,
            int?maxResults,
            CancellationToken cancellationToken)
        {
            using (ApplicationTraceSources.Default.TraceMethod().WithParameters(filter))
            {
                var request = new ProjectsResource.ListRequest(this.service)
                {
                    Filter   = filter?.ToString(),
                    PageSize = maxResults
                };

                IList <Project> projects;
                bool            truncated;
                if (maxResults.HasValue)
                {
                    // Read single page.
                    var response = await request
                                   .ExecuteAsync(cancellationToken)
                                   .ConfigureAwait(false);

                    truncated = response.NextPageToken != null;
                    projects  = response.Projects;
                }
                else
                {
                    // Read all pages.
                    truncated = false;
                    projects  = await new PageStreamer <
                        Project,
                        ProjectsResource.ListRequest,
                        ListProjectsResponse,
                        string>(
                        (req, token) => req.PageToken = token,
                        response => response.NextPageToken,
                        response => response.Projects)
                                .FetchAllAsync(request, cancellationToken)
                                .ConfigureAwait(false);
                }

                // Filter projects in deleted/pending delete state.
                var activeProjects = projects
                                     .EnsureNotNull()
                                     .Where(p => p.LifecycleState == "ACTIVE");

                ApplicationTraceSources.Default.TraceVerbose(
                    "Found {0} projects", activeProjects.Count());

                return(new FilteredProjectList(activeProjects, truncated));
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Returns the complete list of projects for the current credentials.
 /// It always return empty list if no item is found, caller can safely assume there is no null return.
 /// </summary>
 public Task <IList <Project> > GetProjectsListAsync()
 {
     return(LoadPagedListAsync(
                (token) =>
     {
         if (string.IsNullOrEmpty(token))
         {
             Debug.WriteLine("Fetching first page.");
             return Service.Projects.List().ExecuteAsync();
         }
         else
         {
             Debug.WriteLine($"Fetching page: {token}");
             ProjectsResource.ListRequest request = Service.Projects.List();
             request.PageToken = token;
             return request.ExecuteAsync();
         }
     },
                x => x.Projects,
                x => x.NextPageToken));
 }