2
0

GithubMetaService.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Microsoft.Extensions.DependencyInjection;
  2. using Microsoft.Extensions.Logging;
  3. using Microsoft.Extensions.Options;
  4. using System;
  5. using System.Net.Http;
  6. using System.Net.Http.Json;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace FastGithub.Scanner
  10. {
  11. [Service(ServiceLifetime.Singleton)]
  12. sealed class GithubMetaService
  13. {
  14. private readonly IHttpClientFactory httpClientFactory;
  15. private readonly IOptionsMonitor<GithubOptions> options;
  16. private readonly ILogger<GithubMetaService> logger;
  17. public GithubMetaService(
  18. IHttpClientFactory httpClientFactory,
  19. IOptionsMonitor<GithubOptions> options,
  20. ILogger<GithubMetaService> logger)
  21. {
  22. this.httpClientFactory = httpClientFactory;
  23. this.options = options;
  24. this.logger = logger;
  25. }
  26. public async Task<Meta?> GetMetaAsync(CancellationToken cancellationToken = default)
  27. {
  28. try
  29. {
  30. var httpClient = this.httpClientFactory.CreateClient();
  31. return await httpClient.GetFromJsonAsync<Meta>(this.options.CurrentValue.MetaUri, cancellationToken);
  32. }
  33. catch (Exception ex)
  34. {
  35. this.logger.LogError(ex, "获取meta.json文件失败");
  36. return default;
  37. }
  38. }
  39. }
  40. }