Follow the leader
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

109 lines
3.7KB

  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.IO;
  5. using Newtonsoft.Json;
  6. namespace Leadercraft.Server
  7. {
  8. internal class LeadercraftApi
  9. {
  10. private readonly ulong _userId;
  11. private readonly string _tokenUrl;
  12. private readonly string _criteriaUrl;
  13. public LeadercraftApi(ulong userId, string tokenUrl, string criteriaUrl)
  14. {
  15. this._userId = userId;
  16. this._tokenUrl = tokenUrl;
  17. this._criteriaUrl = criteriaUrl;
  18. }
  19. public LeadercraftResult<KeyStruct> RequestPOSTToken(string playerName = "???")
  20. {
  21. NewKeyStruct reqBodyObj = new NewKeyStruct{ PlayerID = _userId, PlayerName = playerName };
  22. string reqBodyStr = JsonConvert.SerializeObject(reqBodyObj);
  23. // Request
  24. WebRequest request = WebRequest.Create(_tokenUrl);
  25. request.Method = "POST";
  26. request.ContentLength = reqBodyStr.Length;
  27. request.ContentType = "application/json";
  28. StreamWriter body;
  29. try
  30. {
  31. body = new StreamWriter(request.GetRequestStream());
  32. body.Write(reqBodyStr);
  33. body.Close();
  34. }
  35. catch (WebException e)
  36. {
  37. int status = (int) e.Status;
  38. if (e.Response != null && e.Response is HttpWebResponse webResponse)
  39. status = (int)webResponse.StatusCode;
  40. return new LeadercraftResult<KeyStruct>(new byte[] { }, status);
  41. }
  42. // Response
  43. HttpWebResponse response = null;
  44. try
  45. {
  46. response = (HttpWebResponse)request.GetResponse();
  47. }
  48. catch (WebException e)
  49. {
  50. int status = (int) e.Status;
  51. if (e.Response != null && e.Response is HttpWebResponse webResponse)
  52. status = (int)webResponse.StatusCode;
  53. return new LeadercraftResult<KeyStruct>(new byte[] { }, status);
  54. }
  55. StreamReader respBody = new StreamReader(response.GetResponseStream());
  56. string respBodyStr = respBody.ReadToEnd();
  57. response.Close();
  58. return new LeadercraftResult<KeyStruct>(respBodyStr, (int)response.StatusCode);
  59. }
  60. public LeadercraftResult<string> RequestPOSTCriteria(CriteriaStruct criteria, string token)
  61. {
  62. criteria.PlayerID = _userId;
  63. string reqBodyStr = JsonConvert.SerializeObject(criteria);
  64. // Request
  65. WebRequest request = WebRequest.Create(_criteriaUrl);
  66. request.Method = "POST";
  67. request.ContentLength = reqBodyStr.Length;
  68. request.ContentType = "application/json";
  69. request.Headers.Add(HttpRequestHeader.Authorization, "leadercraft "+token);
  70. StreamWriter body;
  71. try
  72. {
  73. body = new StreamWriter(request.GetRequestStream());
  74. body.Write(reqBodyStr);
  75. body.Close();
  76. }
  77. catch (WebException e)
  78. {
  79. int status = (int) e.Status;
  80. if (e.Response != null && e.Response is HttpWebResponse webResponse)
  81. status = (int)webResponse.StatusCode;
  82. return new LeadercraftResult<string>(new byte[] { }, status);
  83. }
  84. // Response
  85. HttpWebResponse response = null;
  86. try
  87. {
  88. response = (HttpWebResponse)request.GetResponse();
  89. }
  90. catch (WebException e)
  91. {
  92. int status = (int) e.Status;
  93. if (e.Response != null && e.Response is HttpWebResponse webResponse)
  94. status = (int)webResponse.StatusCode;
  95. return new LeadercraftResult<string>(new byte[] { }, status);
  96. }
  97. StreamReader respBody = new StreamReader(response.GetResponseStream());
  98. string respBodyStr = respBody.ReadToEnd();
  99. response.Close();
  100. return new LeadercraftResult<string>(respBodyStr, (int)response.StatusCode);
  101. }
  102. }
  103. }