HttpJsonClient.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using InABox.Core;
  2. using JetBrains.Annotations;
  3. using RestSharp;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.Net;
  8. using System.Text;
  9. namespace InABox.Client.Remote.Json
  10. {
  11. public class HttpJsonClient
  12. {
  13. public string Protocol { get; set; }
  14. public string URL { get; set; }
  15. public int Port { get; set; }
  16. public HttpJsonClient(string protocol, string url, int port)
  17. {
  18. Protocol = protocol;
  19. URL = url;
  20. Port = port;
  21. }
  22. public bool TrySendRequest<T>(RestRequest request, out T result, [NotNullWhen(false)] out string? error)
  23. {
  24. var uri = new Uri($"{Protocol}://{URL}:{Port}");
  25. var cli = new RestClient(uri);
  26. result = default;
  27. error = null;
  28. try
  29. {
  30. var res = cli.Execute(request);
  31. if (res.ErrorException == null)
  32. {
  33. if (res.StatusCode != HttpStatusCode.OK)
  34. throw new Exception($"HTTP Request returns {((int)res.StatusCode)} {CoreUtils.SplitCamelCase(res.StatusCode.ToString())}");
  35. try
  36. {
  37. result = Serialization.Deserialize<T>(res.Content, true);
  38. return true;
  39. }
  40. catch (Exception eDeserialize)
  41. {
  42. error = $"Unable to deserialize response!\n\n{eDeserialize.Message}\n\n{res.Content}";
  43. }
  44. }
  45. else
  46. {
  47. error = "Could not connect to the server.";
  48. }
  49. }
  50. catch (Exception err)
  51. {
  52. error = $"Could not connect to the server: {err.Message}";
  53. }
  54. return false;
  55. }
  56. public T SendRequest<T>(RestRequest request)
  57. {
  58. if (TrySendRequest<T>(request, out var result, out var error))
  59. {
  60. return result;
  61. }
  62. throw new Exception(error);
  63. }
  64. public bool Ping()
  65. {
  66. var req = new RestRequest("", Method.GET)
  67. {
  68. Timeout = 10 * 1000
  69. };
  70. var uri = new Uri($"{Protocol}://{URL}:{Port}");
  71. var cli = new RestClient(uri);
  72. try
  73. {
  74. var res = cli.Execute(req);
  75. if (res.ErrorException == null)
  76. {
  77. return true;
  78. }
  79. }
  80. catch (Exception)
  81. {
  82. }
  83. return false;
  84. }
  85. public bool TryGetRequest<T>(string command, out T result, [NotNullWhen(false)] out string? error)
  86. {
  87. var req = new RestRequest(command, Method.GET)
  88. {
  89. Timeout = 60 * 1000
  90. };
  91. req.RequestFormat = DataFormat.Json;
  92. return TrySendRequest<T>(req, out result, out error);
  93. }
  94. public T GetRequest<T>(string command)
  95. {
  96. if (TryGetRequest<T>(command, out var result, out var error))
  97. {
  98. return result;
  99. }
  100. throw new Exception(error);
  101. }
  102. public T PostRequest<T>(object request, string command)
  103. {
  104. var json = Serialization.Serialize(request);
  105. var req = new RestRequest(command, Method.POST)
  106. {
  107. Timeout = 60 * 1000
  108. };
  109. req.AddOrUpdateParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
  110. req.RequestFormat = DataFormat.Json;
  111. return SendRequest<T>(req);
  112. }
  113. }
  114. }