-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathGithubClient.cs
More file actions
364 lines (303 loc) · 14.9 KB
/
GithubClient.cs
File metadata and controls
364 lines (303 loc) · 14.9 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using OctoshiftCLI.Contracts;
using OctoshiftCLI.Extensions;
namespace OctoshiftCLI.Services;
public class GithubClient
{
private readonly HttpClient _httpClient;
private readonly OctoLogger _log;
private int _retryDelay;
private readonly RetryPolicy _retryPolicy;
private readonly DateTimeProvider _dateTimeProvider;
private const string DEFAULT_RATE_LIMIT_REMAINING = "5000";
private const int MILLISECONDS_PER_SECOND = 1000;
private const int SECONDARY_RATE_LIMIT_MAX_RETRIES = 3;
private const int SECONDARY_RATE_LIMIT_DEFAULT_DELAY = 60; // 60 seconds default delay
public GithubClient(OctoLogger log, HttpClient httpClient, IVersionProvider versionProvider, RetryPolicy retryPolicy, DateTimeProvider dateTimeProvider, string personalAccessToken)
{
_log = log;
_httpClient = httpClient;
_retryPolicy = retryPolicy;
_dateTimeProvider = dateTimeProvider;
if (_httpClient != null)
{
_httpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
_httpClient.DefaultRequestHeaders.Add("GraphQL-Features", "import_api,mannequin_claiming_emu,org_import_api");
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", personalAccessToken);
_httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("OctoshiftCLI", versionProvider?.GetCurrentVersion()));
if (versionProvider?.GetVersionComments() is { } comments)
{
_httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(comments));
}
}
}
public virtual async Task<string> GetNonSuccessAsync(string url, HttpStatusCode status) => (await GetWithRetry(url, expectedStatus: status)).Content;
public virtual async Task<string> GetAsync(string url, Dictionary<string, string> customHeaders = null) => (await GetWithRetry(url, customHeaders)).Content;
public virtual IAsyncEnumerable<JToken> GetAllAsync(string url, Dictionary<string, string> customHeaders = null) =>
GetAllAsync(url, jToken => (JArray)jToken, customHeaders);
public virtual async IAsyncEnumerable<JToken> GetAllAsync(
string url,
Func<JToken, JArray> resultCollectionSelector,
Dictionary<string, string> customHeaders = null)
{
if (resultCollectionSelector is null)
{
throw new ArgumentNullException(nameof(resultCollectionSelector));
}
var nextUrl = url;
do
{
var (content, headers) = await GetWithRetry(nextUrl, customHeaders: customHeaders);
var jContent = JToken.Parse(content);
foreach (var jToken in resultCollectionSelector(jContent))
{
yield return jToken;
}
nextUrl = GetNextUrl(headers);
} while (nextUrl != null);
}
public virtual async Task<string> PostAsync(string url, object body, Dictionary<string, string> customHeaders = null) =>
(await SendAsync(HttpMethod.Post, url, body, customHeaders: customHeaders)).Content;
public virtual async Task<(string Content, KeyValuePair<string, IEnumerable<string>>[] ResponseHeaders)> PostWithFullResponseAsync(string url, object body, Dictionary<string, string> customHeaders = null) =>
await SendAsync(HttpMethod.Post, url, body, customHeaders: customHeaders);
public virtual async Task<JToken> PostGraphQLAsync(
string url,
object body,
Dictionary<string, string> customHeaders = null)
{
var response = await PostAsync(url, body, customHeaders);
var data = JObject.Parse(response);
EnsureSuccessGraphQLResponse(data);
return data;
}
public virtual async IAsyncEnumerable<JToken> PostGraphQLWithPaginationAsync(
string url,
object body,
Func<JObject, JArray> resultCollectionSelector,
Func<JObject, JObject> pageInfoSelector,
int first = 100,
string after = null,
Dictionary<string, string> customHeaders = null)
{
if (resultCollectionSelector is null)
{
throw new ArgumentNullException(nameof(resultCollectionSelector));
}
if (pageInfoSelector is null)
{
throw new ArgumentNullException(nameof(pageInfoSelector));
}
var jBody = JObject.FromObject(body);
jBody["variables"] ??= new JObject();
jBody["variables"]["first"] = first;
var hasNextPage = true;
while (hasNextPage)
{
jBody["variables"]["after"] = after;
var (content, _) = await SendAsync(HttpMethod.Post, url, jBody, customHeaders: customHeaders);
var jContent = JObject.Parse(content);
foreach (var jResult in resultCollectionSelector(jContent))
{
yield return jResult;
}
var pageInfo = pageInfoSelector(jContent);
if (pageInfo is null)
{
yield break;
}
hasNextPage = pageInfo["hasNextPage"]?.ToObject<bool>() ?? false;
after = pageInfo["endCursor"]?.ToObject<string>();
}
}
public virtual async Task<string> PutAsync(string url, object body, Dictionary<string, string> customHeaders = null) =>
(await SendAsync(HttpMethod.Put, url, body, customHeaders: customHeaders)).Content;
public virtual async Task<string> PatchAsync(string url, object body, Dictionary<string, string> customHeaders = null) =>
(await SendAsync(HttpMethod.Patch, url, body, customHeaders: customHeaders)).Content;
public virtual async Task<(string Content, KeyValuePair<string, IEnumerable<string>>[] ResponseHeaders)> PatchWithFullResponseAsync(string url, object body, Dictionary<string, string> customHeaders = null) =>
await SendAsync(HttpMethod.Patch, url, body, customHeaders: customHeaders);
public virtual async Task<string> DeleteAsync(string url, Dictionary<string, string> customHeaders = null) => (await SendAsync(HttpMethod.Delete, url, customHeaders: customHeaders)).Content;
private async Task<(string Content, KeyValuePair<string, IEnumerable<string>>[] ResponseHeaders)> GetWithRetry(
string url,
Dictionary<string, string> customHeaders = null,
HttpStatusCode expectedStatus = HttpStatusCode.OK) =>
await _retryPolicy.Retry(async () => await SendAsync(HttpMethod.Get, url, customHeaders: customHeaders, expectedStatus: expectedStatus));
private async Task<(string Content, KeyValuePair<string, IEnumerable<string>>[] ResponseHeaders)> SendAsync(
HttpMethod httpMethod,
string url,
object body = null,
HttpStatusCode expectedStatus = HttpStatusCode.OK,
Dictionary<string, string> customHeaders = null,
int retryCount = 0)
{
await ApplyRetryDelayAsync();
_log.LogVerbose($"HTTP {httpMethod}: {url}");
using var request = new HttpRequestMessage(httpMethod, url).AddHeaders(customHeaders);
if (body != null)
{
if (body is HttpContent httpContent)
{
_log.LogVerbose("HTTP BODY: BLOB");
request.Content = httpContent;
}
else
{
var jsonBody = body.ToJson();
_log.LogVerbose($"HTTP BODY: {jsonBody}");
request.Content = jsonBody.ToStringContent();
}
}
using var response = await _httpClient.SendAsync(request);
_log.LogVerbose($"GITHUB REQUEST ID: {ExtractHeaderValue("X-GitHub-Request-Id", response.Headers)}");
var content = await response.Content.ReadAsStringAsync();
_log.LogVerbose($"RESPONSE ({response.StatusCode}): {content}");
var headers = response.Headers.ToArray();
foreach (var header in headers)
{
_log.LogDebug($"RESPONSE HEADER: {header.Key} = {string.Join(",", header.Value)}");
}
if (GetRateLimitRemaining(headers) <= 0 && content.ToUpper().Contains("API RATE LIMIT EXCEEDED"))
{
SetRetryDelay(headers);
}
// Check for secondary rate limits before handling primary rate limits
if (IsSecondaryRateLimit(response.StatusCode, content))
{
return await HandleSecondaryRateLimit(httpMethod, url, body, expectedStatus, customHeaders, headers, retryCount);
}
if (response.StatusCode == HttpStatusCode.Forbidden && _retryDelay > 0)
{
(content, headers) = await SendAsync(httpMethod, url, body, expectedStatus, customHeaders, retryCount);
}
else if (expectedStatus == HttpStatusCode.OK)
{
try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
throw new HttpRequestException($"GitHub API error: {content}", ex, response.StatusCode);
}
}
else if (response.StatusCode != expectedStatus)
{
throw new HttpRequestException($"Expected status code {expectedStatus} but got {response.StatusCode}", null, response.StatusCode);
}
return (content, headers);
}
private async Task ApplyRetryDelayAsync()
{
if (_retryDelay > 0)
{
_log.LogWarning($"GitHub rate limit exceeded. Waiting {_retryDelay} seconds before continuing");
await Task.Delay(_retryDelay * MILLISECONDS_PER_SECOND);
_retryDelay = 0;
}
}
private string GetNextUrl(KeyValuePair<string, IEnumerable<string>>[] headers)
{
var linkHeaderValue = ExtractLinkHeader(headers);
var nextUrl = linkHeaderValue?
.Split(",", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(link =>
{
var rx = new Regex(@"<(?<url>.+)>;\s*rel=""(?<rel>.+)""");
var url = rx.Match(link).Groups["url"].Value;
var rel = rx.Match(link).Groups["rel"].Value; // first, next, last, prev
return (Url: url, Rel: rel);
})
.FirstOrDefault(x => x.Rel == "next").Url;
return nextUrl;
}
private string ExtractLinkHeader(IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers) =>
ExtractHeaderValue("Link", headers);
private int GetRateLimitRemaining(IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers) =>
int.Parse(ExtractHeaderValue("X-RateLimit-Remaining", headers) ?? DEFAULT_RATE_LIMIT_REMAINING);
private long GetRateLimitReset(IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers) =>
long.Parse(ExtractHeaderValue("X-RateLimit-Reset", headers) ?? _dateTimeProvider.CurrentUnixTimeSeconds().ToString());
private string ExtractHeaderValue(string key, IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers) =>
headers.SingleOrDefault(kvp => kvp.Key.Equals(key, StringComparison.OrdinalIgnoreCase)).Value?.FirstOrDefault();
private void SetRetryDelay(IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers)
{
var resetUnixSeconds = GetRateLimitReset(headers);
var currentUnixSeconds = _dateTimeProvider.CurrentUnixTimeSeconds();
_retryDelay = (int)(resetUnixSeconds - currentUnixSeconds);
}
private void EnsureSuccessGraphQLResponse(JObject response)
{
if (response.TryGetValue("errors", out var jErrors) && jErrors is JArray { Count: > 0 } errors)
{
var error = (JObject)errors[0];
var errorMessage = error.TryGetValue("message", out var jMessage) ? (string)jMessage : null;
throw new OctoshiftCliException($"{errorMessage ?? "UNKNOWN"}");
}
}
private bool IsSecondaryRateLimit(HttpStatusCode statusCode, string content)
{
// Secondary rate limits return 403 or 429
if (statusCode is not HttpStatusCode.Forbidden and not HttpStatusCode.TooManyRequests)
{
return false;
}
// Check if this is a primary rate limit (which we handle separately)
if (content.ToUpper().Contains("API RATE LIMIT EXCEEDED"))
{
return false;
}
// Common secondary rate limit error patterns
var contentUpper = content.ToUpper();
return contentUpper.Contains("SECONDARY RATE LIMIT") ||
contentUpper.Contains("ABUSE DETECTION") ||
contentUpper.Contains("YOU HAVE TRIGGERED AN ABUSE DETECTION MECHANISM") ||
statusCode == HttpStatusCode.TooManyRequests;
}
private async Task<(string Content, KeyValuePair<string, IEnumerable<string>>[] ResponseHeaders)> HandleSecondaryRateLimit(
HttpMethod httpMethod,
string url,
object body,
HttpStatusCode expectedStatus,
Dictionary<string, string> customHeaders,
KeyValuePair<string, IEnumerable<string>>[] headers,
int retryCount = 0)
{
if (retryCount >= SECONDARY_RATE_LIMIT_MAX_RETRIES)
{
throw new OctoshiftCliException($"Secondary rate limit exceeded. Maximum retries ({SECONDARY_RATE_LIMIT_MAX_RETRIES}) reached. Please wait before retrying your request.");
}
var delaySeconds = GetSecondaryRateLimitDelay(headers, retryCount);
_log.LogWarning($"Secondary rate limit detected (attempt {retryCount + 1}/{SECONDARY_RATE_LIMIT_MAX_RETRIES}). Waiting {delaySeconds} seconds before retrying...");
await Task.Delay(delaySeconds * MILLISECONDS_PER_SECOND);
return await SendAsync(httpMethod, url, body, expectedStatus, customHeaders, retryCount + 1);
}
private int GetSecondaryRateLimitDelay(KeyValuePair<string, IEnumerable<string>>[] headers, int retryCount)
{
// First check for retry-after header
var retryAfterHeader = ExtractHeaderValue("Retry-After", headers);
if (!string.IsNullOrEmpty(retryAfterHeader) && int.TryParse(retryAfterHeader, out var retryAfterSeconds))
{
return retryAfterSeconds;
}
// Then check if x-ratelimit-remaining is 0 and use x-ratelimit-reset
var rateLimitRemaining = GetRateLimitRemaining(headers);
if (rateLimitRemaining <= 0)
{
var resetUnixSeconds = GetRateLimitReset(headers);
var currentUnixSeconds = _dateTimeProvider.CurrentUnixTimeSeconds();
var delayFromReset = (int)(resetUnixSeconds - currentUnixSeconds);
if (delayFromReset > 0)
{
return delayFromReset;
}
}
// Otherwise use exponential backoff: 1m → 2m → 4m
return SECONDARY_RATE_LIMIT_DEFAULT_DELAY * (int)Math.Pow(2, retryCount);
}
}