| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- using Microsoft.Extensions.Logging;
- namespace Infrastructure.StockData;
- /// <summary>
- /// Yahoo Finance v8 chart 무인증 JSON 호출 공통 — User-Agent 헤더는 DI 에서 기본 부여(Yahoo 는 UA 없으면 거부).
- /// 간단 3회 재시도(2s/4s 백오프). 404(심볼 없음)는 재시도 없이 null 반환 → 호출측이 해당 심볼 skip.
- /// Stooq 봇차단(JS PoW) 도입으로 세계지수·종목 수집 소스를 Yahoo v8 로 전환(2026-07-09).
- /// </summary>
- internal static class YahooFinanceHttp
- {
- public const string ClientName = "YahooFinance";
- private const int MaxAttempts = 3;
- /// <summary>성공 시 응답 본문(JSON), 404 면 null. 그 외 실패는 재시도 후 예외.</summary>
- public static async Task<string?> GetStringWithRetryAsync(HttpClient client, string url, ILogger logger, CancellationToken ct)
- {
- for (var attempt = 1; ; attempt++)
- {
- try
- {
- using var response = await client.GetAsync(url, ct);
- if ((int)response.StatusCode == 404)
- {
- return null;
- }
- response.EnsureSuccessStatusCode();
- return await response.Content.ReadAsStringAsync(ct);
- }
- catch (Exception ex) when (ex is not OperationCanceledException && attempt < MaxAttempts)
- {
- logger.LogWarning(ex, "[Yahoo] HTTP 실패 — 재시도 {Attempt}/{Max}", attempt, MaxAttempts);
- await Task.Delay(TimeSpan.FromSeconds(2 * attempt), ct);
- }
- }
- }
- }
|