-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIPApiPlus.cs
More file actions
117 lines (105 loc) · 4.38 KB
/
IPApiPlus.cs
File metadata and controls
117 lines (105 loc) · 4.38 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
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using IPinfo.Utilities;
using IPinfo.Http.Client;
using IPinfo.Http.Request;
using IPinfo.Models;
using IPinfo.Http.Response;
using IPinfo.Cache;
namespace IPinfo.Apis
{
/// <summary>
/// IPApiPlus.
/// </summary>
public sealed class IPApiPlus : BaseApi
{
/// <summary>
/// Initializes a new instance of the <see cref="IPApiPlus"/> class.
/// </summary>
/// <param name="httpClient"> httpClient. </param>
/// <param name="token"> token. </param>
/// <param name="cacheHandler"> cacheHandler. </param>
internal IPApiPlus(IHttpClient httpClient, string token, CacheHandler cacheHandler)
: base(httpClient, token, cacheHandler)
{
this.BaseUrl = "https://api.ipinfo.io/lookup/";
}
/// <summary>
/// Retrieves details of an IP address.
/// </summary>
/// <param name="ipAddress">The IP address of the user to retrieve details for.</param>
/// <returns>Returns the Models.IPResponsePlus response from the API call.</returns>
public Models.IPResponsePlus GetDetails(
IPAddress ipAddress)
{
string ipString = ipAddress?.ToString();
return this.GetDetails(ipString);
}
/// <summary>
/// Retrieves details of an IP address.
/// </summary>
/// <param name="ipAddress">The IP address of the user to retrieve details for.</param>
/// <returns>Returns the Models.IPResponsePlus response from the API call.</returns>
public Models.IPResponsePlus GetDetails(
string ipAddress = "")
{
Task<Models.IPResponsePlus> t = this.GetDetailsAsync(ipAddress);
ApiHelper.RunTaskSynchronously(t);
return t.Result;
}
/// <summary>
/// Retrieves details of an IP address.
/// </summary>
/// <param name="ipAddress">The IP address of the user to retrieve details for.</param>
/// <param name="cancellationToken">Cancellation token if the request is cancelled. </param>
/// <returns>Returns the Models.IPResponsePlus response from the API call.</returns>
public Task<Models.IPResponsePlus> GetDetailsAsync(
IPAddress ipAddress,
CancellationToken cancellationToken = default)
{
string ipString = ipAddress?.ToString();
return this.GetDetailsAsync(ipString, cancellationToken);
}
/// <summary>
/// Retrieves details of an IP address.
/// </summary>
/// <param name="ipAddress">The IP address of the user to retrieve details for.</param>
/// <param name="cancellationToken">Cancellation token if the request is cancelled. </param>
/// <returns>Returns the Models.IPResponsePlus response from the API call.</returns>
public async Task<Models.IPResponsePlus> GetDetailsAsync(
string ipAddress = "",
CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = "";
}
// first check the data in the cache if cache is available
IPResponsePlus ipResponse = (IPResponsePlus)GetFromCache(ipAddress);
if (ipResponse != null)
{
return ipResponse;
}
if (BogonHelper.IsBogon(ipAddress))
{
ipResponse = new IPResponsePlus()
{
IP = ipAddress,
Bogon = true
};
return ipResponse;
}
// prepare the API call request to fetch the response.
HttpRequest httpRequest = this.CreateGetRequest(this.BaseUrl + ipAddress);
// invoke request and get response.
HttpStringResponse response = await this.GetClientInstance().ExecuteAsStringAsync(httpRequest, cancellationToken).ConfigureAwait(false);
HttpContext context = new HttpContext(httpRequest, response);
// handle errors defined at the API level.
this.ValidateResponse(context);
var responseModel = JsonHelper.ParseIPResponsePlus(response.Body);
SetInCache(ipAddress, responseModel);
return responseModel;
}
}
}