leadercraft/Leadercraft/Server/LeadercraftApi.cs

99 lines
3.3 KiB
C#
Raw Normal View History

using System;
using System.Net;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace Leadercraft.Server
{
internal class LeadercraftApi
{
2020-05-14 23:09:39 -04:00
private readonly ulong _userId;
2020-05-14 23:09:39 -04:00
private readonly string _tokenUrl;
2020-05-14 23:09:39 -04:00
private readonly string _criteriaUrl;
2020-05-14 23:09:39 -04:00
public LeadercraftApi(ulong userId, string tokenUrl, string criteriaUrl)
{
2020-05-14 23:09:39 -04:00
this._userId = userId;
this._tokenUrl = tokenUrl;
this._criteriaUrl = criteriaUrl;
}
2020-05-14 23:09:39 -04:00
public LeadercraftResult<KeyStruct> RequestPOSTToken(string playerName = "???")
{
NewKeyStruct reqBodyObj = new NewKeyStruct{ PlayerID = _userId, PlayerName = playerName };
byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(reqBodyObj));
// Request
WebRequest request = WebRequest.Create(_tokenUrl);
2020-05-14 23:09:39 -04:00
request.Method = "POST";
request.ContentLength = reqBodyBytes.Length;
request.ContentType = "application/json";
Stream body;
try
{
body = request.GetRequestStream();
body.Write(reqBodyBytes, 0, reqBodyBytes.Length);
body.Close();
}
catch (WebException e)
{
return new LeadercraftResult<KeyStruct>(new byte[] { }, (int)e.Status);
}
// Response
2020-05-14 23:09:39 -04:00
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
return new LeadercraftResult<KeyStruct>(new byte[] { }, (int)e.Status);
}
body = response.GetResponseStream();
byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))];
body.Read(respBodyBytes, 0, respBodyBytes.Length);
response.Close();
return new LeadercraftResult<KeyStruct>(respBodyBytes, (int)response.StatusCode);
}
2020-05-14 23:09:39 -04:00
public LeadercraftResult<string> RequestPOSTCriteria(CriteriaStruct criteria, string token)
{
criteria.PlayerID = _userId;
byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(criteria));
// Request
WebRequest request = WebRequest.Create(_criteriaUrl);
request.Method = "POST";
request.ContentLength = reqBodyBytes.Length;
request.ContentType = "application/json";
2020-05-14 23:09:39 -04:00
request.Headers.Add(HttpRequestHeader.Authorization, "leadercraft "+token);
Stream body;
try
{
body = request.GetRequestStream();
body.Write(reqBodyBytes, 0, reqBodyBytes.Length);
body.Close();
}
catch (WebException e)
{
return new LeadercraftResult<string>(new byte[] { }, (int)e.Status);
}
// Response
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
return new LeadercraftResult<string>(new byte[] { }, (int)e.Status);
}
body = response.GetResponseStream();
2020-05-14 23:09:39 -04:00
byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))];
body.Read(respBodyBytes, 0, respBodyBytes.Length);
response.Close();
2020-05-14 23:09:39 -04:00
return new LeadercraftResult<string>(respBodyBytes, (int)response.StatusCode);
}
}
}