Esempio n. 1
0
        /// <summary>
        ///     This method is responsible for retrieving the readme from a repository.
        /// </summary>
        /// <param name="user">
        ///     This parameter represents the owners of the repository. This is in most cases the name of the user
        ///     or in some other case the name of the organization.
        /// </param>
        /// <param name="repository">This parameter represents the name of the repository.</param>
        /// <param name="accessToken">
        ///     This parameter is the access token, which is used to retrieve a readme from a private
        ///     repository. In case of a public repository, the default value is null.
        /// </param>
        /// <returns>This method returns the content of the readme.</returns>
        /// <exception cref="ExternalException">
        ///     This method could throw an external exception whenever the status code is not
        ///     successful.
        /// </exception>
        public async Task <string> FetchReadme(string user, string repository, string accessToken = null)
        {
            IRestClient  client  = restClientFactory.Create(new Uri(BaseUrl));
            IRestRequest request = new RestRequest($"repos/{user}/{repository}/readme");

            if (accessToken != null)
            {
                request.AddHeaders(new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("Authorization", $"Bearer {accessToken}"),
                    new KeyValuePair <string, string>("accept", "application/vnd.github.v3+json")
                });
            }

            IRestResponse response = await client.ExecuteAsync(request);

            if (!response.IsSuccessful)
            {
                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }
                throw new ExternalException(response.ErrorMessage);
            }
            if (string.IsNullOrEmpty(response.Content))
            {
                return(null);
            }
            GithubDataSourceReadmeResourceResult resourceResult =
                JsonConvert.DeserializeObject <GithubDataSourceReadmeResourceResult>(response.Content);

            return(await FetchReadmeContent(resourceResult.DownloadUrl));
        }
        public async Task FetchReadme_GoodFlow(
            [GithubDataSourceReadmeResourceResultDataSource] GithubDataSourceReadmeResourceResult resourceResult)
        {
            // Arrange
            MockRestClient(resourceResult, HttpStatusCode.OK);
            DataSourceAdaptee = new GithubDataSourceAdaptee(ConfigurationMock, ClientFactoryMock.Object, Mapper);

            // Act
            Action act = () => DataSourceAdaptee.FetchReadme(It.IsAny <string>(), It.IsAny <string>());
            string retrievedResourceResult = await DataSourceAdaptee.FetchReadme(It.IsAny <string>(), It.IsAny <string>());

            // Assert
            act.Should().NotThrow();
            retrievedResourceResult.Should().BeEquivalentTo(JsonConvert.SerializeObject(resourceResult));
        }