Пример #1
0
        private async Task <IEnumerable <Document> > GetReadmesFromItems(IEnumerable <Item> readmeItems, Guid repositoryId, GitHubMetadata metadata)
        {
            List <Task <Document> > readmeTasks = new List <Task <Document> >();

            foreach (var item in readmeItems)
            {
                readmeTasks.Add(GetReadmeDocumentFromURL(item, repositoryId, metadata));
            }

            await Task.WhenAll(readmeTasks.ToArray());

            List <Document> readmeDocuments = new List <Document>();

            foreach (var task in readmeTasks)
            {
                readmeDocuments.Add(task.Result);
            }
            return(readmeDocuments);
        }
Пример #2
0
        private async Task <Document> GetReadmeDocumentFromURL(Item item, Guid repositoryId, GitHubMetadata metadata)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("product", "1"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization",
                                                                                               $"token {metadata.PersonalAccessToken}");

                    using (HttpResponseMessage response = await client.GetAsync($"{item.url}"))
                    {
                        response.EnsureSuccessStatusCode();

                        string responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        var readmeResponse = JsonConvert.DeserializeObject <ReadmeResponse>(responseBody);

                        return(DecodeReadme(readmeResponse, repositoryId));
                    }
                }
            }
            catch (Exception ex)
            {
                //TODO: handle exceptions
                Console.WriteLine(ex);
                return(null);
            }
        }
Пример #3
0
        private async Task <IEnumerable <Item> > GetReadmeItems(Repository repository, GitHubMetadata metadata)
        {
            List <Item> readmeItems = new List <Item>();

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("product", "1"));
                    client.DefaultRequestHeaders.Add("Authorization", $"token {metadata.PersonalAccessToken}");
                    using (HttpResponseMessage response = await client.GetAsync(
                               $"https://api.github.com/search/code?q=org:{repository.Name}+filename:readme").ConfigureAwait(false))
                    {
                        response.EnsureSuccessStatusCode();

                        string responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        var itemsResponse = JsonConvert.DeserializeObject <ItemsResponse>(responseBody);
                        foreach (var item in itemsResponse.items)
                        {
                            readmeItems.Add(item);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //TODO: handle exceptions
                Console.WriteLine(ex);
            }
            return(readmeItems);
        }