/
Switch.cs
219 lines (187 loc) · 7.35 KB
/
Switch.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Switch
{
public class SwitchAPI
{
public string APIEndpoint = "https://tr02.switchapi.com";
public SwitchAuth ReadAccess { get; set; }
public SwitchAuth WriteAccess { get; set; }
public SwitchAuth ModifyAccess { get; set; }
public SwitchAuth DeleteAccess { get; set; }
public SwitchAuth AdministratorAccess { get; set; }
public void Initialize(DateTime ExpireTime)
{
if (ReadAccess != null)
{
ReadAccess.AccessToken = GetToken(ReadAccess, ExpireTime);
}
if (WriteAccess != null)
{
WriteAccess.AccessToken = GetToken(WriteAccess, ExpireTime);
}
if (ModifyAccess != null)
{
ModifyAccess.AccessToken = GetToken(ModifyAccess, ExpireTime);
}
if (DeleteAccess != null)
{
DeleteAccess.AccessToken = GetToken(DeleteAccess, ExpireTime);
}
if (AdministratorAccess != null)
{
AdministratorAccess.AccessToken = GetToken(AdministratorAccess, ExpireTime);
}
}
public string GetToken(SwitchAuth auth, DateTime ExpireTime)
{
WebClient wc = new WebClient();
wc.Headers.Add("APIKey", auth.APIKey);
wc.Headers.Add("Signature", md5(string.Format("{0}{1}", auth.APISecret, GetUnixTimestampMillis(ExpireTime))));
wc.Headers.Add("Expire", GetUnixTimestampMillis(ExpireTime).ToString());
wc.Encoding = Encoding.UTF8;
string response = wc.DownloadString(string.Format("{0}/Token", APIEndpoint));
try
{
dynamic tokenResponse = JsonConvert.DeserializeObject(response);
return tokenResponse.AccessToken.Value;
}
catch (Exception ex)
{
// TODO : Exception handle
return string.Empty;
}
}
public string[] Lists(SwitchAuth auth)
{
WebClient wc = new WebClient();
wc.Headers.Add("APIKey", auth.APIKey);
wc.Headers.Add("AccessToken", ReadAccess.AccessToken);
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
wc.Encoding = Encoding.UTF8;
string response = wc.DownloadString(string.Format("{0}/Lists", APIEndpoint));
try
{
dynamic listsResponse = JsonConvert.DeserializeObject(response);
return listsResponse.Response.Value;
}
catch (Exception ex)
{
// TODO : Exception handle
return null;
}
}
public string List(string ListName, string Query)
{
WebClient wc = new WebClient();
wc.Headers.Add("APIKey", ReadAccess.APIKey);
wc.Headers.Add("AccessToken", ReadAccess.AccessToken);
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
wc.Encoding = Encoding.UTF8;
return wc.UploadString(string.Format("{0}/List", APIEndpoint), Query);
}
public string Add(string ListName, string JsonData)
{
WebClient wc = new WebClient();
wc.Headers.Add("APIKey", WriteAccess.APIKey);
wc.Headers.Add("AccessToken", WriteAccess.AccessToken);
wc.Headers.Add("List", ListName);
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
wc.Encoding = Encoding.UTF8;
return wc.UploadString(string.Format("{0}/Add", APIEndpoint), JsonData);
}
public string Update(string ListName, string ListItemId, string JsonData)
{
WebClient wc = new WebClient();
wc.Headers.Add("APIKey", ModifyAccess.APIKey);
wc.Headers.Add("AccessToken", ModifyAccess.AccessToken);
wc.Headers.Add("List", ListName);
wc.Headers.Add("ListItemId", ListItemId);
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
wc.Encoding = Encoding.UTF8;
return wc.UploadString(string.Format("{0}/Set", APIEndpoint), JsonData);
}
public string Delete(string ListName, string ListItemId)
{
WebClient wc = new WebClient();
wc.Headers.Add("APIKey", DeleteAccess.APIKey);
wc.Headers.Add("AccessToken", DeleteAccess.AccessToken);
wc.Headers.Add("List", ListName);
wc.Headers.Add("ListItemId", ListItemId);
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
wc.Encoding = Encoding.UTF8;
return wc.UploadString(string.Format("{0}/Set", APIEndpoint), "DELETE", string.Empty);
}
public string SendGridSend(SendGridMail EMail)
{
WebClient wc = new WebClient();
wc.Headers.Add("APIKey", WriteAccess.APIKey);
wc.Headers.Add("AccessToken", WriteAccess.AccessToken);
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
wc.Encoding = Encoding.UTF8;
return wc.UploadString(string.Format("{0}/SendGrid/Send", APIEndpoint), "POST", JsonConvert.SerializeObject(EMail));
}
#region Switch Helpers
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long GetUnixTimestampMillis(DateTime dateTime)
{
return (long)(dateTime - UnixEpoch).TotalMilliseconds;
}
public static long GetCurrentUnixTimestampMillis()
{
return (long)(DateTime.UtcNow - UnixEpoch).TotalMilliseconds;
}
public static string md5(string plainText)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] btr = Encoding.UTF8.GetBytes(plainText);
btr = md5.ComputeHash(btr);
StringBuilder sb = new StringBuilder();
foreach (byte ba in btr)
{
sb.Append(ba.ToString("x2").ToLower());
}
return sb.ToString();
}
public class SwitchAuth
{
public string APIKey { get; set; }
public string APISecret { get; set; }
public string AccessToken { get; set; }
}
#endregion
#region SendGrid Mail Objects
public class To
{
public string email { get; set; }
}
public class Personalization
{
public List<To> to { get; set; }
public string subject { get; set; }
}
public class From
{
public string email { get; set; }
public string name { get; set; }
}
public class Content
{
public string type { get; set; }
public string value { get; set; }
}
public class SendGridMail
{
public List<Personalization> personalizations { get; set; }
public From from { get; set; }
public List<Content> content { get; set; }
}
#endregion
}
}