public static async Task <RepoViewCount> Run(
            [ActivityTrigger] IDurableActivityContext context,
            ILogger log)
        {
            var repoName = context.GetInput <string>();
            var result   = new RepoViewCount
            {
                RepoName = repoName
            };

            var username = Environment.GetEnvironmentVariable("GitHubUserName");
            var password = Environment.GetEnvironmentVariable("GitHubPassword");
            var client   = new HttpClient();

            client.DefaultRequestHeaders.UserAgent.TryParseAdd("C# agent");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")));
            var request = new HttpRequestMessage(HttpMethod.Get, $"https://api.github.com/repos/{username}/{repoName}/traffic/views");

            var response = await client.SendAsync(request);

            response.EnsureSuccessStatusCode();
            var body = await response.Content.ReadAsStringAsync();

            JObject jObject = JsonConvert.DeserializeObject <JObject>(body);

            result.ViewCount = jObject["count"].Value <int>();
            return(result);
        }
예제 #2
0
        public async Task <RepoViewCount> GetRepositoryViewCount(string repoName)
        {
            var result = new RepoViewCount
            {
                RepoName = repoName
            };

            PrepareHttpClient();
            var url      = $"{BaseUrl}/repos/{_username}/{repoName}/traffic/views";
            var request  = new HttpRequestMessage(HttpMethod.Get, url);
            var response = await _httpClient.SendAsync(request);

            response.EnsureSuccessStatusCode();
            var body = await response.Content.ReadAsStringAsync();

            JObject jObject = JsonConvert.DeserializeObject <JObject>(body);

            result.ViewCount = jObject["count"].Value <int>();
            return(result);
        }