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()); + } +}