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.

99 lines
3.3KB

  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. byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(reqBodyObj));
  23. // Request
  24. WebRequest request = WebRequest.Create(_tokenUrl);
  25. request.Method = "POST";
  26. request.ContentLength = reqBodyBytes.Length;
  27. request.ContentType = "application/json";
  28. Stream body;
  29. try
  30. {
  31. body = request.GetRequestStream();
  32. body.Write(reqBodyBytes, 0, reqBodyBytes.Length);
  33. body.Close();
  34. }
  35. catch (WebException e)
  36. {
  37. return new LeadercraftResult<KeyStruct>(new byte[] { }, (int)e.Status);
  38. }
  39. // Response
  40. HttpWebResponse response = null;
  41. try
  42. {
  43. response = (HttpWebResponse)request.GetResponse();
  44. }
  45. catch (WebException e)
  46. {
  47. return new LeadercraftResult<KeyStruct>(new byte[] { }, (int)e.Status);
  48. }
  49. body = response.GetResponseStream();
  50. byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))];
  51. body.Read(respBodyBytes, 0, respBodyBytes.Length);
  52. response.Close();
  53. return new LeadercraftResult<KeyStruct>(respBodyBytes, (int)response.StatusCode);
  54. }
  55. public LeadercraftResult<string> RequestPOSTCriteria(CriteriaStruct criteria, string token)
  56. {
  57. criteria.PlayerID = _userId;
  58. byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(criteria));
  59. // Request
  60. WebRequest request = WebRequest.Create(_criteriaUrl);
  61. request.Method = "POST";
  62. request.ContentLength = reqBodyBytes.Length;
  63. request.ContentType = "application/json";
  64. request.Headers.Add(HttpRequestHeader.Authorization, "leadercraft "+token);
  65. Stream body;
  66. try
  67. {
  68. body = request.GetRequestStream();
  69. body.Write(reqBodyBytes, 0, reqBodyBytes.Length);
  70. body.Close();
  71. }
  72. catch (WebException e)
  73. {
  74. return new LeadercraftResult<string>(new byte[] { }, (int)e.Status);
  75. }
  76. // Response
  77. HttpWebResponse response = null;
  78. try
  79. {
  80. response = (HttpWebResponse)request.GetResponse();
  81. }
  82. catch (WebException e)
  83. {
  84. return new LeadercraftResult<string>(new byte[] { }, (int)e.Status);
  85. }
  86. body = response.GetResponseStream();
  87. byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))];
  88. body.Read(respBodyBytes, 0, respBodyBytes.Length);
  89. response.Close();
  90. return new LeadercraftResult<string>(respBodyBytes, (int)response.StatusCode);
  91. }
  92. }
  93. }