forked from dvygolov/FB.QuickCommenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestExecutor.cs
81 lines (77 loc) · 2.78 KB
/
RequestExecutor.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
using FB.QuickCommenter.Helpers;
using FB.QuickCommenter.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace FB.QuickCommenter
{
public class RequestExecutor
{
public string Token { get => Cs.Token; }
private readonly string _fbApiAddress;
public ConnectSettings Cs;
public RequestExecutor(string fbApiAddress, ConnectSettings cs)
{
_fbApiAddress = fbApiAddress;
Cs = cs;
}
public async Task<JObject> ExecuteFbRequestAsync(RestRequest req, bool changeToken = true, bool checkError = true, string apiVersion = "")
{
var resp = await GetFbResponseAsync(req,changeToken,apiVersion);
var respJson = (JObject)JsonConvert.DeserializeObject(resp.Content);
if (checkError)
ErrorChecker.HasErrorsInResponse(respJson, true);
return respJson;
}
private async Task<IRestResponse> GetFbResponseAsync(RestRequest req, bool changeToken = true, string apiVersion = "")
{
var apiAddress = _fbApiAddress;
if (!string.IsNullOrEmpty(apiVersion))
{
apiAddress = Regex.Replace(apiAddress, @"(\d\.\d)", apiVersion);
}
var rc = new RestClient(apiAddress);
if (!string.IsNullOrEmpty(Cs.ProxyAddress))
{
rc.Proxy = new WebProxy()
{
Address = new Uri($"http://{Cs.ProxyAddress}:{Cs.ProxyPort}"),
Credentials = new NetworkCredential()
{
UserName = Cs.ProxyLogin,
Password = Cs.ProxyPassword
}
};
}
if (changeToken)
{
if (req.Method == Method.GET)
{
req.AddQueryParameter("access_token", Cs.Token);
req.AddQueryParameter("locale", "ru_RU");
}
else
{
req.AddParameter("access_token", Cs.Token);
req.AddParameter("locale", "ru_RU");
}
}
var resp = await rc.ExecuteAsync(req);
int tryCount = 0;
while ((resp.StatusCode == 0
|| resp.StatusCode == HttpStatusCode.BadGateway
|| resp.StatusCode == HttpStatusCode.ProxyAuthenticationRequired) && tryCount < 3)
{
rc.Proxy = null;
resp = await rc.ExecuteAsync(req);
await Task.Delay(tryCount * 1000);
tryCount++;
}
return resp;
}
}
}