From 08b12621b40558117aa83b00ac658ee4931535c2 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 28 Oct 2019 23:53:39 +0200 Subject: [PATCH] get public ip --- NDB.Infrastructure.PublicIP/Class1.cs | 8 ---- .../DependencyInjectionExtensions.cs | 13 ++++++ .../Entities/IPInfo.cs | 7 +++ .../NDB.Infrastructure.PublicIP.csproj | 6 +++ .../Services/IPublicIPService.cs | 10 +++++ .../Services/PublicIPService.cs | 44 +++++++++++++++++++ 6 files changed, 80 insertions(+), 8 deletions(-) delete mode 100644 NDB.Infrastructure.PublicIP/Class1.cs create mode 100644 NDB.Infrastructure.PublicIP/DependencyInjectionExtensions.cs create mode 100644 NDB.Infrastructure.PublicIP/Entities/IPInfo.cs create mode 100644 NDB.Infrastructure.PublicIP/Services/IPublicIPService.cs create mode 100644 NDB.Infrastructure.PublicIP/Services/PublicIPService.cs diff --git a/NDB.Infrastructure.PublicIP/Class1.cs b/NDB.Infrastructure.PublicIP/Class1.cs deleted file mode 100644 index ce2e6df..0000000 --- a/NDB.Infrastructure.PublicIP/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace NDB.Infrastructure.PublicIP -{ - public class Class1 - { - } -} diff --git a/NDB.Infrastructure.PublicIP/DependencyInjectionExtensions.cs b/NDB.Infrastructure.PublicIP/DependencyInjectionExtensions.cs new file mode 100644 index 0000000..33c9d17 --- /dev/null +++ b/NDB.Infrastructure.PublicIP/DependencyInjectionExtensions.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.DependencyInjection; +using NDB.Infrastructure.PublicIP.Services; + +namespace NDB.Infrastructure.PublicIP +{ + public static class DependencyInjectionExtensions + { + public static void AddPublicIPService(this IServiceCollection services) + { + services.AddHttpClient(); + } + } +} diff --git a/NDB.Infrastructure.PublicIP/Entities/IPInfo.cs b/NDB.Infrastructure.PublicIP/Entities/IPInfo.cs new file mode 100644 index 0000000..b0eadd3 --- /dev/null +++ b/NDB.Infrastructure.PublicIP/Entities/IPInfo.cs @@ -0,0 +1,7 @@ +namespace NDB.Infrastructure.PublicIP.Entities +{ + public class IPInfo + { + public string ip { get; set; } + } +} \ No newline at end of file diff --git a/NDB.Infrastructure.PublicIP/NDB.Infrastructure.PublicIP.csproj b/NDB.Infrastructure.PublicIP/NDB.Infrastructure.PublicIP.csproj index 9f5c4f4..a7ae801 100644 --- a/NDB.Infrastructure.PublicIP/NDB.Infrastructure.PublicIP.csproj +++ b/NDB.Infrastructure.PublicIP/NDB.Infrastructure.PublicIP.csproj @@ -4,4 +4,10 @@ netstandard2.0 + + + + + + diff --git a/NDB.Infrastructure.PublicIP/Services/IPublicIPService.cs b/NDB.Infrastructure.PublicIP/Services/IPublicIPService.cs new file mode 100644 index 0000000..0e58c14 --- /dev/null +++ b/NDB.Infrastructure.PublicIP/Services/IPublicIPService.cs @@ -0,0 +1,10 @@ +using NDB.Infrastructure.PublicIP.Entities; +using System.Threading.Tasks; + +namespace NDB.Infrastructure.PublicIP.Services +{ + public interface IPublicIPService + { + Task GetPublicIP(); + } +} \ No newline at end of file diff --git a/NDB.Infrastructure.PublicIP/Services/PublicIPService.cs b/NDB.Infrastructure.PublicIP/Services/PublicIPService.cs new file mode 100644 index 0000000..7f467f5 --- /dev/null +++ b/NDB.Infrastructure.PublicIP/Services/PublicIPService.cs @@ -0,0 +1,44 @@ +using NDB.Infrastructure.PublicIP.Entities; +using Newtonsoft.Json; +using System.Net.Http; +using System.Threading.Tasks; + +namespace NDB.Infrastructure.PublicIP.Services +{ + public class PublicIPService : IPublicIPService + { + private const string _url1 = "https://api.ipify.org/?format=json"; + private const string _url2 = "https://ip.seeip.org/json"; + + private readonly HttpClient _httpClient; + + public PublicIPService(HttpClient httpClient) + { + _httpClient = httpClient; + } + + public async Task GetPublicIP() + { + IPInfo result = null; + + using (var response = await _httpClient.GetAsync(_url1)) + { + if (response.IsSuccessStatusCode) + result = await GetResult(response); + } + + if (result != null) + return result; + + using (var response = await _httpClient.GetAsync(_url2)) + { + if (response.IsSuccessStatusCode) + result = await GetResult(response); + } + + return result; + } + + private async Task GetResult(HttpResponseMessage response) => JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + } +}