forked from linvi/tweetinvi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpClientWebHelper.cs
50 lines (43 loc) · 1.7 KB
/
HttpClientWebHelper.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
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Tweetinvi.Core.Helpers;
using Tweetinvi.Core.Web;
using Tweetinvi.Models;
using HttpMethod = System.Net.Http.HttpMethod;
namespace Tweetinvi.WebLogic
{
public class HttpClientWebHelper : IHttpClientWebHelper
{
public async Task<HttpResponseMessage> GetHttpResponse(ITwitterQuery twitterQuery, ITwitterClientHandler handler = null)
{
using (var client = GetHttpClient(twitterQuery, handler))
{
client.Timeout = twitterQuery.Timeout;
var httpMethod = new HttpMethod(twitterQuery.HttpMethod.ToString());
if (twitterQuery.HttpContent == null)
{
return await client.SendAsync(new HttpRequestMessage(httpMethod, twitterQuery.QueryURL)).ConfigureAwait(false);
}
else
{
if (httpMethod != HttpMethod.Post)
{
throw new ArgumentException("Cannot send HttpContent in a WebRequest that is not POST.");
}
return await client.PostAsync(twitterQuery.QueryURL, twitterQuery.HttpContent).ConfigureAwait(false);
}
}
}
public HttpClient GetHttpClient(ITwitterQuery twitterQuery, ITwitterClientHandler twitterHandler = null)
{
var handler = (twitterHandler as TwitterClientHandler) ?? new TwitterClientHandler();
handler.TwitterQuery = twitterQuery;
var client = new HttpClient(handler)
{
Timeout = twitterQuery.Timeout,
};
return client;
}
}
}