|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Net;
- using System.Text;
- using System.IO;
- using Newtonsoft.Json;
- namespace Leadercraft.Server
- {
- internal class LeadercraftApi
- {
- private readonly ulong _userId;
-
- private readonly string _tokenUrl;
-
- private readonly string _criteriaUrl;
-
- public LeadercraftApi(ulong userId, string tokenUrl, string criteriaUrl)
- {
- this._userId = userId;
- this._tokenUrl = tokenUrl;
- this._criteriaUrl = criteriaUrl;
- }
-
- 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);
- 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
- 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);
- }
-
- 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";
- 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();
- byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))];
- body.Read(respBodyBytes, 0, respBodyBytes.Length);
- response.Close();
- return new LeadercraftResult<string>(respBodyBytes, (int)response.StatusCode);
- }
- }
- }
|