-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cs
125 lines (104 loc) · 3.24 KB
/
Client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Screeps.Network.API;
using Screeps.Network.API.Auth;
using Screeps.Network.API.User;
namespace Screeps.Network;
public class Client
{
private const string ContentType = "application/json; charset=utf-8";
private readonly WebClient _web;
public Client()
{
_web = new WebClient();
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
};
}
public Client(string token) : this() => SetToken(token);
public Client(string email, string password) => SetEmailPasswordAuth(email, password);
~Client() => _web.Dispose();
public string Protocol { get; set; } = "https";
public string Host { get; set; } = "screeps.com";
public ServerType ServerType { get; set; }
public string BaseURL => $"{Protocol}://{Host}/{ServerType.GetAPIPath()}/";
public void SetEmailPasswordAuth(string email, string password)
{
var data = System.Text.Encoding.UTF8.GetBytes($"{email}:{password}");
var base64 = Convert.ToBase64String(data);
_web.Headers["Authorization"] = $"Basic {base64}";
}
public void SetToken(string token)
{
_web.Headers["X-Token"] = token;
}
public SignInResponse SignIn(string email, string password)
{
return SendRequest<SignInResponse>("auth/signin", new SignInRequest
{
Email = email,
Password = password
});
}
public AccountResponse GetAccount()
{
return GetResponse<AccountResponse>("auth/me");
}
public string GetUsername()
{
return GetResponse<NameResponse>("user/name").Username;
}
public CodeResponse UploadCode(CodeRequest code)
{
return SendRequest<CodeResponse>("user/code", code);
}
public bool IsValidToken()
{
try
{
GetResponse("user/name");
return true;
}
catch (WebException ex)
{
using var response = ex.Response as HttpWebResponse;
if (response == null) throw;
if (response.StatusCode == HttpStatusCode.Unauthorized)
return false;
throw;
}
}
private T SendRequest<T>(string uri, object data)
{
var rawResponse = SendRequest(uri, JsonConvert.SerializeObject(data));
var responseObject = JsonConvert.DeserializeObject<T>(rawResponse);
return responseObject;
}
private string SendRequest(string uri, string data)
{
ResetWebClient();
return _web.UploadString(uri, data);
}
private T GetResponse<T>(string uri)
{
var rawResponse = GetResponse(uri);
var responseObject = JsonConvert.DeserializeObject<T>(rawResponse);
return responseObject;
}
private string GetResponse(string uri)
{
ResetWebClient();
return _web.DownloadString(uri);
}
private void ResetWebClient()
{
_web.BaseAddress = BaseURL;
_web.Headers[HttpRequestHeader.ContentType] = ContentType;
}
}