-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGimmeProxyClient.cs
More file actions
79 lines (68 loc) · 3.33 KB
/
GimmeProxyClient.cs
File metadata and controls
79 lines (68 loc) · 3.33 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
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace GimmeProxy;
/// <summary>
/// A GimmeProxy API client.
/// </summary>
public static class GimmeProxyClient
{
private static readonly HttpClient defaultHttpClient = new();
/// <summary>
/// Returns one random proxy with no specific options.
/// </summary>
/// <exception cref="HttpRequestException">Can be thrown if the request was not successful.</exception>
/// <param name="cancellationToken">(Optional) A token that allows processing to be cancelled.</param>
/// <returns>
/// Random proxy details.
/// </returns>
public static Task<GimmeProxyResponse?> GetRandomProxyAsync(CancellationToken cancellationToken = default)
=> GetRandomProxyAsync(defaultHttpClient, new(), cancellationToken);
/// <summary>
/// Returns one random proxy with additional filter parameters.
/// </summary>
/// <exception cref="HttpRequestException">Can be thrown if the request was not successful.</exception>
/// <param name="proxyOptions">Options for filtering proxy result.</param>
/// <param name="cancellationToken">(Optional) A token that allows processing to be cancelled.</param>
/// <returns>
/// Random proxy details.
/// </returns>
public static Task<GimmeProxyResponse?> GetRandomProxyAsync(GimmeProxyRequest proxyOptions, CancellationToken cancellationToken = default)
=> GetRandomProxyAsync(defaultHttpClient, proxyOptions, cancellationToken);
/// <summary>
/// Returns one random proxy with additional filter parameters.
/// </summary>
/// <exception cref="HttpRequestException">Can be thrown if the request was not successful.</exception>
/// <param name="httpClient">Provide your own HTTP client to make the request.</param>
/// <param name="proxyOptions">Options for filtering proxy result.</param>
/// <param name="cancellationToken">(Optional) A token that allows processing to be cancelled.</param>
/// <returns>
/// Random proxy details.
/// </returns>
public static async Task<GimmeProxyResponse?> GetRandomProxyAsync(HttpClient httpClient, GimmeProxyRequest proxyOptions, CancellationToken cancellationToken = default)
{
var url = proxyOptions.ToString();
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
#if NET47 || NET471 || NET472 || NET48 || NET481
var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var cleanJson = json.Replace("<br>", string.Empty)
.Replace("<BR>", string.Empty)
.Replace("{},", "[],");
#else
var json = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
// Some data comes back with <br> tags, this is just a sweeping replace cleanup.
// OtherProtocols also comes back as an object instead of an empty array.
var cleanJson = json.Replace("<br>", string.Empty, StringComparison.OrdinalIgnoreCase)
.Replace("{},", "[],");
#endif
var error = JsonConvert.DeserializeObject<GimmeProxyErrorResponse>(cleanJson);
if (error is not null && !string.IsNullOrEmpty(error.ErrorMessage))
{
throw new GimmeProxyException(error.ErrorMessage!);
}
return JsonConvert.DeserializeObject<GimmeProxyResponse>(cleanJson);
}
}