UcdApiClient.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// A helper class for accessing the ucdapi.org API.
  5. /// </summary>
  6. internal class UcdApiClient
  7. {
  8. public const string BaseUrl = "https://ucdapi.org/unicode/latest/";
  9. private static readonly HttpClient _httpClient = new ();
  10. public async Task<string> GetChars (string chars)
  11. {
  12. HttpResponseMessage response = await _httpClient.GetAsync ($"{BaseUrl}chars/{Uri.EscapeDataString (chars)}").ConfigureAwait (false);
  13. response.EnsureSuccessStatusCode ();
  14. return await response.Content.ReadAsStringAsync ().ConfigureAwait (false);
  15. }
  16. public async Task<string> GetCharsName (string chars)
  17. {
  18. HttpResponseMessage response =
  19. await _httpClient.GetAsync ($"{BaseUrl}chars/{Uri.EscapeDataString (chars)}/name").ConfigureAwait (false);
  20. response.EnsureSuccessStatusCode ();
  21. return await response.Content.ReadAsStringAsync ().ConfigureAwait (false);
  22. }
  23. public async Task<string> GetCodepointDec (int dec)
  24. {
  25. HttpResponseMessage response = await _httpClient.GetAsync ($"{BaseUrl}codepoint/dec/{dec}").ConfigureAwait (false);
  26. response.EnsureSuccessStatusCode ();
  27. return await response.Content.ReadAsStringAsync ().ConfigureAwait (false);
  28. }
  29. public async Task<string> GetCodepointHex (string hex)
  30. {
  31. HttpResponseMessage response = await _httpClient.GetAsync ($"{BaseUrl}codepoint/hex/{hex}").ConfigureAwait (false);
  32. response.EnsureSuccessStatusCode ();
  33. return await response.Content.ReadAsStringAsync ().ConfigureAwait (false);
  34. }
  35. }