| 12345678910111213141516171819202122232425262728293031323334353637 |
- using System.Net.Http.Headers;
- using Microsoft.Extensions.Logging;
- namespace Infrastructure.StockData;
- /// <summary>KRX OpenAPI(data-dbg.krx.co.kr) 호출 공통 — AUTH_KEY 헤더 인증 + 간단 3회 재시도 (2s/4s 백오프)</summary>
- internal static class KrxCoKrHttp
- {
- public const string ClientName = "KRXCoKr";
- /// <summary>KRX OpenAPI 인증 헤더명 (라이브 검증: 헤더 있으면 "Unauthorized API Call", 없으면 "Unauthorized Key")</summary>
- public const string AuthHeader = "AUTH_KEY";
- private const int MaxAttempts = 3;
- public static async Task<string> GetStringWithRetryAsync(HttpClient client, string url, string apiKey, ILogger logger, CancellationToken ct)
- {
- for (var attempt = 1; ; attempt++)
- {
- try
- {
- using var request = new HttpRequestMessage(HttpMethod.Get, url);
- request.Headers.TryAddWithoutValidation(AuthHeader, apiKey);
- request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- using var response = await client.SendAsync(request, ct);
- response.EnsureSuccessStatusCode();
- return await response.Content.ReadAsStringAsync(ct);
- }
- catch (Exception ex) when (ex is not OperationCanceledException && attempt < MaxAttempts)
- {
- logger.LogWarning(ex, "[KRXCoKr] HTTP 실패 — 재시도 {Attempt}/{Max}", attempt, MaxAttempts);
- await Task.Delay(TimeSpan.FromSeconds(2 * attempt), ct);
- }
- }
- }
- }
|