UcdApiClient.cs 1.6 KB

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