This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from aspriddell/add-geolocation-api
Add geolocation api
- Loading branch information
Showing
6 changed files
with
106 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
DragonFruit.Six.Api/Services/Geolocation/GeolocationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Dragon6 API Copyright DragonFruit Network <inbox@dragonfruit.network> | ||
// Licensed under Apache-2. Refer to the LICENSE file for more info | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using DragonFruit.Data; | ||
using DragonFruit.Data.Serializers.Newtonsoft; | ||
using DragonFruit.Six.Api.Services.Geolocation.Requests; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace DragonFruit.Six.Api.Services.Geolocation | ||
{ | ||
public static class GeolocationExtensions | ||
{ | ||
/// <summary> | ||
/// Get the current device location based on a IP Geolocation lookup | ||
/// </summary> | ||
public static Task<Geolocation> GeolocateAsync<T>(this T client) where T : ApiClient<ApiJsonSerializer> | ||
{ | ||
return client.PerformAsync<Geolocation>(new UbisoftSelfGeolocationRequest()); | ||
} | ||
|
||
/// <summary> | ||
/// Get the approximate location and ISP info for a collection of IP addresses | ||
/// </summary> | ||
/// <param name="client">The <see cref="Dragon6Client"/> to use</param> | ||
/// <param name="ips">A collection of IP addresses to lookup</param> | ||
/// <returns>An <see cref="IReadOnlyCollection{T}"/> containing the IP addresses and their approximate location</returns> | ||
public static async Task<IReadOnlyCollection<Geolocation>> GeolocateAsync(this Dragon6Client client, params string[] ips) | ||
{ | ||
var request = new UbisoftGeolocationRequest(ips); | ||
var container = await client.PerformAsync<JObject>(request).ConfigureAwait(false); | ||
|
||
return container["ipLocations"]?.ToObject<Geolocation[]>() ?? Array.Empty<Geolocation>(); | ||
} | ||
|
||
/// <summary> | ||
/// Get the approximate location and ISP info for a collection of IP addresses | ||
/// </summary> | ||
/// <param name="client">The <see cref="Dragon6Client"/> to use</param> | ||
/// <param name="ips">A collection of IP addresses to lookup</param> | ||
/// <returns>An <see cref="IReadOnlyCollection{T}"/> containing the IP addresses and their approximate location</returns> | ||
public static Task<IReadOnlyCollection<Geolocation>> GeolocateAsync(this Dragon6Client client, params IPAddress[] ips) | ||
{ | ||
return GeolocateAsync(client, ips.Select(x => x.ToString()).ToArray()); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
DragonFruit.Six.Api/Services/Geolocation/Requests/UbisoftGeolocationRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Dragon6 API Copyright DragonFruit Network <inbox@dragonfruit.network> | ||
// Licensed under Apache-2. Refer to the LICENSE file for more info | ||
|
||
using System.Collections.Generic; | ||
using DragonFruit.Data; | ||
using DragonFruit.Data.Parameters; | ||
|
||
namespace DragonFruit.Six.Api.Services.Geolocation.Requests | ||
{ | ||
/// <summary> | ||
/// Creates a request to get the approximate location a collection of provided IP addresses | ||
/// </summary> | ||
public class UbisoftGeolocationRequest : UbiApiRequest | ||
{ | ||
public override string Path => $"{Endpoints.BaseEndpoint}/v2/iplocation"; | ||
|
||
public UbisoftGeolocationRequest(IEnumerable<string> addresses) | ||
{ | ||
Addresses = addresses; | ||
} | ||
|
||
[QueryParameter("ips", CollectionConversionMode.Concatenated)] | ||
public IEnumerable<string> Addresses { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters