-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIClient.cs
109 lines (91 loc) · 3.88 KB
/
APIClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using Microsoft.Extensions.Caching.Memory;
using System.Text.Json;
using System.Text;
namespace ePortfolio
{
public class APIClient(IMemoryCache cache, Settings settings) {
private IMemoryCache Cache = cache;
private readonly Settings _settings = settings;
public async Task OnGet() {
if (!Cache.TryGetValue("GitHubRepos", out List<Repository>? _)) {
await FetchReposGraphQL();
}
}
private async Task FetchReposGraphQL() {
string url = "https://api.github.com/graphql";
// Your GitHub Personal Access Token
string? token = _settings.Token;
// GraphQL query to get user's repositories
string query = @"
{
viewer {
repositories(first: 20) {
nodes {
name
description
url
repositoryTopics(first: 15) {
nodes {
topic {
name
}
}
}
}
}
}
}";
// Create the GraphQL request
var requestPayload = new { query };
var requestJson = JsonSerializer.Serialize(requestPayload);
var content = new StringContent(requestJson, Encoding.UTF8, "application/json");
// Create and configure the HttpClient
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
client.DefaultRequestHeaders.Add("User-Agent", "request");
// Make the GraphQL request
var response = await client.PostAsync(url, content); // Bottleneck
if (response.IsSuccessStatusCode) {
var responseData = await response.Content.ReadAsStringAsync();
var jsonDocument = JsonDocument.Parse(responseData);
ParseRepositories(jsonDocument);
}
}
private void ParseRepositories(JsonDocument jsonDocument) {
List<Repository> repositories = [];
HashSet<string> filters = [
"web-development",
"back-end-development",
"data-science",
"cloud"
];
var repoNodes = jsonDocument.RootElement.GetProperty("data").GetProperty("viewer").GetProperty("repositories").GetProperty("nodes");
// Iterate over API response
foreach (var node in repoNodes.EnumerateArray()) {
var topicNodes = node.GetProperty("repositoryTopics").GetProperty("nodes");
// Build repositories
foreach (var topic in topicNodes.EnumerateArray()) {
if (filters.Contains(topic.GetProperty("topic").GetProperty("name").ToString())) {
var repo = new Repository {
Name = node.GetProperty("name").ToString(),
Description = node.GetProperty("description").ToString(),
Url = node.GetProperty("url").ToString(),
};
repo.ProcessName();
repositories.Add(repo);
break;
}
}
}
Cache.Set("GitHubRepos", repositories, TimeSpan.FromMinutes(20));
}
}
public class Repository {
public required string Name { get; set; }
public required string Url { get; set; }
public required string Description { get; set; }
public void ProcessName() {
Name = Name.Replace('-', ' ');
}
}
}