Skip to content

Commit a3c1bea

Browse files
authored
Add lookup with multiple fields
1 parent 2031997 commit a3c1bea

21 files changed

Lines changed: 1964 additions & 1842 deletions

File tree

.github/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
NuGet package install using package manager:
2727

2828
```bash
29-
Install-Package IpData -Version 2.0.0
29+
Install-Package IpData -Version 2.0.1
3030
```
3131

3232
NuGet package install using .NET CLI:
3333

3434
```bash
35-
dotnet add package IpData --version 2.0.0
35+
dotnet add package IpData --version 2.0.1
3636
```
3737

3838
## Lookup
@@ -63,6 +63,10 @@ Console.WriteLine($"Localized country name for {myIpInfoLocalized.Ip} is {ipInfo
6363
// Get single field from IP
6464
var countryName = await client.Lookup("8.8.8.8", x => x.CountryName);
6565
Console.WriteLine($"Country name for 8.8.8.8 is {countryName}");
66+
67+
// Get multiple fields from IP
68+
var geolocation = await client.Lookup("8.8.8.8", x => x.Latitude, x => x.Longitude);
69+
Console.WriteLine($"Geolocation for 8.8.8.8 is lat: {geolocation.Latitude} long: {geolocation.Longitude}");
6670
```
6771

6872
### Bulk

build/cake/build.ps1

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ try {
5959
# Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
6060
# exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
6161
# installed (.NET 4.5 is an in-place upgrade).
62-
[System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
62+
# PowerShell Core already has support for TLS 1.2 so we can skip this if running in that.
63+
if (-not $IsCoreCLR) {
64+
[System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192 -bor 48
65+
}
6366
} catch {
6467
Write-Output 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to upgrade to .NET Framework 4.5+ and PowerShell v3'
6568
}
@@ -118,7 +121,7 @@ $MODULES_PACKAGES_CONFIG = Join-Path $MODULES_DIR "packages.config"
118121
# Make sure tools folder exists
119122
if ((Test-Path $PSScriptRoot) -and !(Test-Path $TOOLS_DIR)) {
120123
Write-Verbose -Message "Creating tools directory..."
121-
New-Item -Path $TOOLS_DIR -Type directory | out-null
124+
New-Item -Path $TOOLS_DIR -Type Directory | Out-Null
122125
}
123126

124127
# Make sure that packages.config exist.
@@ -155,24 +158,30 @@ if (!(Test-Path $NUGET_EXE)) {
155158
}
156159

157160
# Save nuget.exe path to environment to be available to child processed
158-
$ENV:NUGET_EXE = $NUGET_EXE
161+
$env:NUGET_EXE = $NUGET_EXE
162+
$env:NUGET_EXE_INVOCATION = if ($IsLinux -or $IsMacOS) {
163+
"mono `"$NUGET_EXE`""
164+
} else {
165+
"`"$NUGET_EXE`""
166+
}
159167

160168
# Restore tools from NuGet?
161169
if(-Not $SkipToolPackageRestore.IsPresent) {
162170
Push-Location
163171
Set-Location $TOOLS_DIR
164172

165173
# Check for changes in packages.config and remove installed tools if true.
166-
[string] $md5Hash = MD5HashFile($PACKAGES_CONFIG)
174+
[string] $md5Hash = MD5HashFile $PACKAGES_CONFIG
167175
if((!(Test-Path $PACKAGES_CONFIG_MD5)) -Or
168-
($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
176+
($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
169177
Write-Verbose -Message "Missing or changed package.config hash..."
170178
Get-ChildItem -Exclude packages.config,nuget.exe,Cake.Bakery |
171-
Remove-Item -Recurse
179+
Remove-Item -Recurse -Force
172180
}
173181

174182
Write-Verbose -Message "Restoring tools from NuGet..."
175-
$NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$TOOLS_DIR`""
183+
184+
$NuGetOutput = Invoke-Expression "& $env:NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `"$TOOLS_DIR`""
176185

177186
if ($LASTEXITCODE -ne 0) {
178187
Throw "An error occurred while restoring NuGet tools."
@@ -181,7 +190,7 @@ if(-Not $SkipToolPackageRestore.IsPresent) {
181190
{
182191
$md5Hash | Out-File $PACKAGES_CONFIG_MD5 -Encoding "ASCII"
183192
}
184-
Write-Verbose -Message ($NuGetOutput | out-string)
193+
Write-Verbose -Message ($NuGetOutput | Out-String)
185194

186195
Pop-Location
187196
}
@@ -192,13 +201,13 @@ if (Test-Path $ADDINS_PACKAGES_CONFIG) {
192201
Set-Location $ADDINS_DIR
193202

194203
Write-Verbose -Message "Restoring addins from NuGet..."
195-
$NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$ADDINS_DIR`""
204+
$NuGetOutput = Invoke-Expression "& $env:NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `"$ADDINS_DIR`""
196205

197206
if ($LASTEXITCODE -ne 0) {
198207
Throw "An error occurred while restoring NuGet addins."
199208
}
200209

201-
Write-Verbose -Message ($NuGetOutput | out-string)
210+
Write-Verbose -Message ($NuGetOutput | Out-String)
202211

203212
Pop-Location
204213
}
@@ -209,13 +218,13 @@ if (Test-Path $MODULES_PACKAGES_CONFIG) {
209218
Set-Location $MODULES_DIR
210219

211220
Write-Verbose -Message "Restoring modules from NuGet..."
212-
$NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$MODULES_DIR`""
221+
$NuGetOutput = Invoke-Expression "& $env:NUGET_EXE_INVOCATION install -ExcludeVersion -OutputDirectory `"$MODULES_DIR`""
213222

214223
if ($LASTEXITCODE -ne 0) {
215224
Throw "An error occurred while restoring NuGet modules."
216225
}
217226

218-
Write-Verbose -Message ($NuGetOutput | out-string)
227+
Write-Verbose -Message ($NuGetOutput | Out-String)
219228

220229
Pop-Location
221230
}
@@ -225,11 +234,16 @@ if (!(Test-Path $CAKE_EXE)) {
225234
Throw "Could not find Cake.exe at $CAKE_EXE"
226235
}
227236

237+
$CAKE_EXE_INVOCATION = if ($IsLinux -or $IsMacOS) {
238+
"mono `"$CAKE_EXE`""
239+
} else {
240+
"`"$CAKE_EXE`""
241+
}
228242

229-
230-
# Build Cake arguments
231-
$cakeArguments = @("$Script");
232-
if ($Target) { $cakeArguments += "-target=$Target" }
243+
# Build an array (not a string) of Cake arguments to be joined later
244+
$cakeArguments = @()
245+
if ($Script) { $cakeArguments += "`"$Script`"" }
246+
if ($Target) { $cakeArguments += "-target=`"$Target`"" }
233247
if ($Configuration) { $cakeArguments += "-configuration=$Configuration" }
234248
if ($Verbosity) { $cakeArguments += "-verbosity=$Verbosity" }
235249
if ($ShowDescription) { $cakeArguments += "-showdescription" }
@@ -238,5 +252,5 @@ $cakeArguments += $ScriptArgs
238252

239253
# Start Cake
240254
Write-Host "Running build script..."
241-
&$CAKE_EXE $cakeArguments
242-
exit $LASTEXITCODE
255+
Invoke-Expression "& $CAKE_EXE_INVOCATION $($cakeArguments -join " ")"
256+
exit $LASTEXITCODE

build/cake/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ if [ ! -f "$CAKE_EXE" ]; then
114114
fi
115115

116116
# Start Cake
117-
exec mono "$CAKE_EXE" $SCRIPT "${CAKE_ARGUMENTS[@]}"
117+
exec mono "$CAKE_EXE" $SCRIPT "${CAKE_ARGUMENTS[@]}"

build/cake/tools/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Cake" version="0.34.1" />
3+
<package id="Cake" version="0.37.0" />
44
</packages>

samples/IpData.Lookup/IpData.Lookup.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
66
<ApplicationIcon />
77
<StartupObject />
88
<LangVersion>latest</LangVersion>

samples/IpData.Lookup/Program.cs

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
1-
using System;
2-
using System.Globalization;
3-
using System.Threading.Tasks;
4-
5-
namespace IpData.Lookup
6-
{
7-
public static class Program
8-
{
9-
public static async Task Main()
10-
{
11-
var client = new IpDataClient("ea1d25cb36a3c66b3fdd02c33129e508106835d8a102b8ed65d8eb82");
12-
13-
// Get IP data from my IP
14-
var myIpInfo = await client.Lookup("1.1.1.1");
15-
Console.WriteLine($"Country name for {myIpInfo.Ip} is {myIpInfo.CountryName}");
16-
17-
// Get localized IP data from my IP
18-
var myIpInfoLocalized = await client.Lookup(CultureInfo.GetCultureInfo("zh-CN"));
19-
Console.WriteLine($"Localized country name for {myIpInfoLocalized.Ip} is {myIpInfoLocalized.CountryName}");
20-
21-
// Get IP data from IP
22-
var ipInfo = await client.Lookup("8.8.8.8");
23-
Console.WriteLine($"Country name for {ipInfo.Ip} is {ipInfo.CountryName}");
24-
25-
// Get localized IP data from IP
26-
var ipInfoLocalized = await client.Lookup("8.8.8.8", CultureInfo.GetCultureInfo("zh-CN"));
27-
Console.WriteLine($"Localized country name for {myIpInfoLocalized.Ip} is {ipInfoLocalized.CountryName}");
28-
29-
// Get single field from IP
30-
var countryName = await client.Lookup("8.8.8.8", x => x.CountryName);
31-
Console.WriteLine($"Country name for 8.8.8.8 is {countryName}");
32-
33-
// Get carrier info from IP
34-
var carrierInfo = await client.Carrier("69.78.70.144");
35-
Console.WriteLine($"Carrier name: {carrierInfo.Name}");
36-
37-
// Get ASN info from IP
38-
var asnInfo = await client.Asn("69.78.70.144");
39-
Console.WriteLine($"ASN name: {asnInfo.Name}");
40-
41-
// Get timezone info from IP
42-
var timezoneInfo = await client.TimeZone("69.78.70.144");
43-
Console.WriteLine($"TimeZone name: {timezoneInfo.Name}");
44-
45-
// Get currency info from IP
46-
var currencyInfo = await client.Currency("69.78.70.144");
47-
Console.WriteLine($"Currency name: {currencyInfo.Name}");
48-
49-
// Get threat info from IP
50-
var threatInfo = await client.Threat("69.78.70.144");
51-
Console.WriteLine($"Threat is Tor: {threatInfo.IsTor}");
52-
53-
// Get IP data for multiple IPs
54-
var ipInfoList = await client.Lookup(new string[] { "1.1.1.1", "2.2.2.2", "3.3.3.3" });
55-
foreach (var info in ipInfoList)
56-
{
57-
Console.WriteLine($"Country name for {info.Ip} is {info.CountryName}");
58-
}
59-
}
60-
}
61-
}
1+
using System;
2+
using System.Globalization;
3+
using System.Threading.Tasks;
4+
5+
namespace IpData.Lookup
6+
{
7+
public static class Program
8+
{
9+
public static async Task Main()
10+
{
11+
var client = new IpDataClient("API_KEY");
12+
13+
// Get IP data from my IP
14+
var myIpInfo = await client.Lookup();
15+
Console.WriteLine($"Country name for {myIpInfo.Ip} is {myIpInfo.CountryName}");
16+
17+
// Get localized IP data from my IP
18+
var myIpInfoLocalized = await client.Lookup(CultureInfo.GetCultureInfo("zh-CN"));
19+
Console.WriteLine($"Localized country name for {myIpInfoLocalized.Ip} is {myIpInfoLocalized.CountryName}");
20+
21+
// Get IP data from IP
22+
var ipInfo = await client.Lookup("8.8.8.8");
23+
Console.WriteLine($"Country name for {ipInfo.Ip} is {ipInfo.CountryName}");
24+
25+
// Get localized IP data from IP
26+
var ipInfoLocalized = await client.Lookup("8.8.8.8", CultureInfo.GetCultureInfo("zh-CN"));
27+
Console.WriteLine($"Localized country name for {myIpInfoLocalized.Ip} is {ipInfoLocalized.CountryName}");
28+
29+
// Get single field from IP
30+
var countryName = await client.Lookup("8.8.8.8", x => x.CountryName);
31+
Console.WriteLine($"Country name for 8.8.8.8 is {countryName}");
32+
33+
// Get multiple fields from IP
34+
var geolocation = await client.Lookup("8.8.8.8", x => x.Latitude, x => x.Longitude);
35+
Console.WriteLine($"Geolocation for 8.8.8.8 is lat: {geolocation.Latitude} long: {geolocation.Longitude}");
36+
37+
// Get carrier info from IP
38+
var carrierInfo = await client.Carrier("69.78.70.144");
39+
Console.WriteLine($"Carrier name: {carrierInfo.Name}");
40+
41+
// Get ASN info from IP
42+
var asnInfo = await client.Asn("69.78.70.144");
43+
Console.WriteLine($"ASN name: {asnInfo.Name}");
44+
45+
// Get timezone info from IP
46+
var timezoneInfo = await client.TimeZone("69.78.70.144");
47+
Console.WriteLine($"TimeZone name: {timezoneInfo.Name}");
48+
49+
// Get currency info from IP
50+
var currencyInfo = await client.Currency("69.78.70.144");
51+
Console.WriteLine($"Currency name: {currencyInfo.Name}");
52+
53+
// Get threat info from IP
54+
var threatInfo = await client.Threat("69.78.70.144");
55+
Console.WriteLine($"Threat is Tor: {threatInfo.IsTor}");
56+
57+
// Get IP data for multiple IPs
58+
var ipInfoList = await client.Lookup(new[] { "1.1.1.1", "2.2.2.2", "3.3.3.3" });
59+
foreach (var info in ipInfoList)
60+
{
61+
Console.WriteLine($"Country name for {info.Ip} is {info.CountryName}");
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)