-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackageApi.cs
52 lines (43 loc) · 2.34 KB
/
PackageApi.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using BlackProxiesSharp.Exceptions;
using BlackProxiesSharp.Models;
using BlackProxiesSharp.Utilities;
namespace BlackProxiesSharp.Api
{
public class PackageApi : ApiBase
{
protected override Dictionary<HttpStatusCode, Exception> StatusCodeExceptions { get; } = new Dictionary<HttpStatusCode, Exception>()
{
{ HttpStatusCode.NotFound, new PackageNotFoundException() }
};
public PackageApi(string id, HttpClient httpClient = null)
{
HttpClient = httpClient ?? new HttpClient();
HttpClient.BaseAddress = new Uri($"https://proxies.black/api/package/{id}/");
}
public async Task<PackageModel> GetAsync()
=> await GetResponseAsync<PackageModel>(new HttpRequestMessage(HttpMethod.Get, string.Empty)).ConfigureAwait(false);
public async Task<PackageModel> StartAsync(IEnumerable<string> ips)
=> await GetResponseAsync<PackageModel>(new HttpRequestMessage(HttpMethod.Post, "start")
{
Content = new JsonContent<IEnumerable<string>>(ips)
}).ConfigureAwait(false);
public async Task<IEnumerable<string>> GetProxiesAsync(string format = "host:port")
=> await GetResponseAsync<IEnumerable<string>>(new HttpRequestMessage(HttpMethod.Get, $"proxies?type=json&format={Uri.EscapeDataString(format)}")).ConfigureAwait(false);
public async Task<PackageModel> PauseAsync()
=> await GetResponseAsync<PackageModel>(new HttpRequestMessage(HttpMethod.Post, "pause")).ConfigureAwait(false);
public async Task<PackageModel> ResumeAsync()
=> await GetResponseAsync<PackageModel>(new HttpRequestMessage(HttpMethod.Post, "resume")).ConfigureAwait(false);
public async Task<PackageModel> RefreshPoolAsync()
=> await GetResponseAsync<PackageModel>(new HttpRequestMessage(HttpMethod.Post, "refresh")).ConfigureAwait(false);
public async Task<PackageModel> UpdateWhitelistedIPsAsync(IEnumerable<string> ips)
=> await GetResponseAsync<PackageModel>(new HttpRequestMessage(HttpMethod.Put, "ips")
{
Content = new JsonContent<IEnumerable<string>>(ips)
}).ConfigureAwait(false);
}
}