-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkLayer.cs
More file actions
321 lines (294 loc) · 13.9 KB
/
Copy pathNetworkLayer.cs
File metadata and controls
321 lines (294 loc) · 13.9 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SnooSharp
{
public interface INetworkLayer : IDisposable
{
Task<string> Get(string url, CancellationToken token, IProgress<float> progress, Dictionary<string, string> body = null);
Task Send(string url, string method, CancellationToken token, Dictionary<string, string> arguments);
Task<string> Post(string url, CancellationToken token, Dictionary<string, string> arguments, IProgress<float> progress);
Task<RedditOAuth> RequestGrantCode(string code, CancellationToken token);
Task DestroyToken(string refreshToken);
string RedditBaseUrl { get; }
INetworkLayer Clone(RedditOAuth credential);
}
class NetworkLayer : INetworkLayer
{
HttpClient _httpClient;
CookieContainer _cookieContainer;
UserState _userState;
private int _failedRequestCount = 0;
static DateTime _priorRequestSet = new DateTime();
static int _requestSetCount = 0;
static DateTime _lastRequestMade = new DateTime();
string _appId;
string _appSecret;
string _redirectUrl;
public NetworkLayer(UserState userState, string appId, string appSecret, string redirectUrl)
{
_userState = userState;
_appId = appId;
_appSecret = appSecret;
_redirectUrl = redirectUrl;
_cookieContainer = new CookieContainer();
var handler = new HttpClientHandler { CookieContainer = _cookieContainer };
if (handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.GZip |
DecompressionMethods.Deflate;
}
_httpClient = new HttpClient(handler);
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("SnooSharp/1.0");
}
public async Task<string> Get(string url, CancellationToken token, IProgress<float> progress, Dictionary<string, string> body)
{
await ThrottleRequests(token);
await EnsureRedditCookie(token);
var requestUri = new Uri(RedditBaseUrl + (url.Contains("://") ? new Uri(url).PathAndQuery : url));
HttpRequestMessage sendMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
if (body != null)
{
sendMessage.Content = new FormUrlEncodedContent(body);
}
var responseMessage = await _httpClient.SendAsync(sendMessage, HttpCompletionOption.ResponseContentRead, token);
if (!responseMessage.IsSuccessStatusCode && sendMessage.RequestUri != requestUri)
{
var result = await Get(sendMessage.RequestUri.PathAndQuery, token, progress, body);
if (body != null)
{
body.Add("redirected-url", sendMessage.RequestUri.PathAndQuery);
}
return result;
}
else
{
var bodyString = ProcessJsonErrors(await responseMessage.Content.ReadAsStringAsync());
if (bodyString.StartsWith("<!doctype html><html><title>") && bodyString.EndsWith("try again and hopefully we will be fast enough this time."))
return await Get(url, token, progress, body);
else if (responseMessage.IsSuccessStatusCode)
{
if (string.IsNullOrWhiteSpace(bodyString) || bodyString == "{}" || bodyString == "\"{}\"")
throw new RedditException("body string was empty but no error code was present");
else
{
_failedRequestCount = 0;
return bodyString;
}
}
else
{
_failedRequestCount++;
switch (responseMessage.StatusCode)
{
case HttpStatusCode.GatewayTimeout:
case HttpStatusCode.RequestTimeout:
case HttpStatusCode.BadGateway:
case HttpStatusCode.BadRequest:
case HttpStatusCode.InternalServerError:
case HttpStatusCode.ServiceUnavailable:
{
if (_failedRequestCount < 5)
return await Get(url, token, progress, body);
else
break;
}
case HttpStatusCode.NotFound:
//reddit likes to return 404 for no apparent reason
if (_failedRequestCount < 2)
return await Get(url, token, progress, body);
else
throw new RedditNotFoundException(url);
case HttpStatusCode.Forbidden:
throw new RedditException(url + " forbidden");
}
responseMessage.EnsureSuccessStatusCode();
return null;
}
}
}
public async Task Send(string url, string method, CancellationToken token, Dictionary<string, string> arguments)
{
await ThrottleRequests(token);
await EnsureRedditCookie(token);
HttpResponseMessage responseMessage = null;
var request = new HttpRequestMessage(new HttpMethod(method), url);
request.Content = new FormUrlEncodedContent(arguments);
responseMessage = await _httpClient.SendAsync(request);
await ProcessJsonErrors(responseMessage);
}
public async Task<string> Post(string url, CancellationToken token, Dictionary<string, string> arguments, IProgress<float> progress)
{
await ThrottleRequests(token);
await EnsureRedditCookie(token);
HttpResponseMessage responseMessage = null;
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new FormUrlEncodedContent(arguments);
responseMessage = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, token);
return ProcessJsonErrors(await responseMessage.Content.ReadAsStringAsync());
}
public string RedditBaseUrl
{
get
{
if (_userState != null && _userState.OAuth != null)
return "https://oauth.reddit.com";
else
return "http://www.reddit.com";
}
}
private string ProcessJsonErrors(string response)
{
string realErrorString = "";
try
{
if (response.Contains("errors"))
{
var jsonErrors = JsonConvert.DeserializeObject<JsonErrorsData>(response);
if (jsonErrors.Errors != null && jsonErrors.Errors.Length > 0)
{
realErrorString = jsonErrors.Errors[0].ToString();
}
}
}
catch
{
}
if (!string.IsNullOrWhiteSpace(realErrorString))
throw new RedditException(realErrorString);
return response;
}
private async Task ProcessJsonErrors(HttpResponseMessage httpResponse)
{
var response = await httpResponse.Content.ReadAsStringAsync();
string realErrorString = "";
try
{
if (response.Contains("errors"))
{
var jsonErrors = JsonConvert.DeserializeObject<JsonErrorsData>(response);
if (jsonErrors.Errors != null && jsonErrors.Errors.Length > 0)
{
realErrorString = jsonErrors.Errors[0].ToString();
}
}
}
catch
{
}
if (!string.IsNullOrWhiteSpace(realErrorString))
throw new RedditException(realErrorString);
}
private async Task EnsureRedditCookie(CancellationToken token)
{
if (_userState.OAuth != null)
{
//see if we need to refresh the token
if (_userState.OAuth.Created.AddSeconds(_userState.OAuth.ExpiresIn) < DateTime.UtcNow)
{
_userState.OAuth = await RefreshToken(_userState.OAuth.RefreshToken, token);
}
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _userState.OAuth.AccessToken);
}
else
{
_httpClient.DefaultRequestHeaders.Authorization = null;
}
}
public async Task<RedditOAuth> RequestGrantCode(string code, CancellationToken token)
{
//we're messing with the headers here so use a different client
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.UTF8Encoding.UTF8.GetBytes(string.Format("{0}:{1}", _appId, _appSecret))));
var result = await httpClient.PostAsync(new Uri("https://ssl.reddit.com/api/v1/access_token"), new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type", "authorization_code"},
{"code", code},
{"redirect_uri", _redirectUrl}, //this is basically just a magic string that needs to match with reddit's app registry
}), token);
var jsonResult = await result.Content.ReadAsStringAsync();
var oAuth = JsonConvert.DeserializeObject<RedditOAuth>(jsonResult);
oAuth.Created = DateTime.UtcNow;
return oAuth;
}
public async Task DestroyToken(string refreshToken)
{
//we're messing with the headers here so use a different client
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.UTF8Encoding.UTF8.GetBytes(string.Format("{0}:{1}", _appId, _appSecret))));
var result = await httpClient.PostAsync(new Uri("https://ssl.reddit.com/api/v1/revoke_token"), new FormUrlEncodedContent(new Dictionary<string, string>
{
{"token", refreshToken},
{"token_type_hint", "refresh_token"},
}));
}
public async Task<RedditOAuth> RefreshToken(string refreshToken, CancellationToken token)
{
//we're messing with the headers here so use a different client
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.UTF8Encoding.UTF8.GetBytes(string.Format("{0}:{1}", _appId, _appSecret))));
var result = await httpClient.PostAsync(new Uri("https://ssl.reddit.com/api/v1/access_token"), new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type", "refresh_token"},
{"refresh_token", refreshToken},
}), token);
var jsonResult = await result.Content.ReadAsStringAsync();
var oAuth = JsonConvert.DeserializeObject<RedditOAuth>(jsonResult);
oAuth.Created = DateTime.UtcNow;
oAuth.RefreshToken = refreshToken; //this is to make life a bit easier, since you would need to keep track of this thing anyway
return oAuth;
}
//dont hammer reddit!
//Make no more than thirty requests per minute. This allows some burstiness to your requests,
//but keep it sane. On average, we should see no more than one request every two seconds from you.
//the above statement is from the reddit api docs, but its not quite true, there are some api's that have logging
//set for 15 requests in 30 seconds, so we can allow some burstiness but it must fit in the 15 requests/30 seconds rule
public static async Task ThrottleRequests(CancellationToken token)
{
var offset = DateTime.Now - _lastRequestMade;
if (offset.TotalMilliseconds < 1000)
{
await Task.Delay(1000 - (int)offset.TotalMilliseconds);
}
if (_requestSetCount > 15)
{
var overallOffset = DateTime.Now - _priorRequestSet;
if (overallOffset.TotalSeconds < 30)
{
var delay = (30 - (int)overallOffset.TotalSeconds) * 1000;
if (delay > 2)
{
for (int i = 0; i < delay; i++)
{
await Task.Delay(1000);
}
}
else
await Task.Delay(delay);
}
_requestSetCount = 0;
_priorRequestSet = DateTime.Now;
}
_requestSetCount++;
_lastRequestMade = DateTime.Now;
}
public INetworkLayer Clone(RedditOAuth credential)
{
return new NetworkLayer(new UserState { OAuth = credential }, _appId, _appSecret, _redirectUrl);
}
public void Dispose()
{
if(_httpClient != null)
_httpClient.Dispose();
_httpClient = null;
}
}
}