From 359626baae5250bcb929d15a645273c15d0df5d8 Mon Sep 17 00:00:00 2001 From: Henrik Sozzi Date: Mon, 4 Nov 2024 09:36:18 +0100 Subject: [PATCH] Updated dependencies and now compatible from netstandard2.0 to net8.0 (#166) I've updated all dependencies and reverded the base target from netstandard2.1 (introduced in ver 1.2.7) to netstandard2.0 as this keeps it compatible with the widely used Net Framework 4.8. Added net4.8 to tests and adapted appsettings.json code to work on every platform. All tests passed on all targets. --- GoogleMapsApi.Test/GoogleMapsApi.Test.csproj | 20 +++++++++++------ .../IntegrationTests/BaseTestIntegration.cs | 16 ++------------ .../IntegrationTests/DistanceMatrixTests.cs | 4 +--- GoogleMapsApi.Test/Utils/AppSettings.cs | 22 +++++++++++++++++++ GoogleMapsApi/GoogleMapsApi.csproj | 2 +- 5 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 GoogleMapsApi.Test/Utils/AppSettings.cs diff --git a/GoogleMapsApi.Test/GoogleMapsApi.Test.csproj b/GoogleMapsApi.Test/GoogleMapsApi.Test.csproj index 5dcfa70..16dad35 100644 --- a/GoogleMapsApi.Test/GoogleMapsApi.Test.csproj +++ b/GoogleMapsApi.Test/GoogleMapsApi.Test.csproj @@ -1,6 +1,6 @@  - net8.0;net6.0 + net8.0;net6.0;net4.8 latest Library false @@ -9,12 +9,18 @@ - - - - - - + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/GoogleMapsApi.Test/IntegrationTests/BaseTestIntegration.cs b/GoogleMapsApi.Test/IntegrationTests/BaseTestIntegration.cs index 2ce84c8..7ed106e 100644 --- a/GoogleMapsApi.Test/IntegrationTests/BaseTestIntegration.cs +++ b/GoogleMapsApi.Test/IntegrationTests/BaseTestIntegration.cs @@ -1,4 +1,4 @@ -using Microsoft.Extensions.Configuration; +using GoogleMapsApi.Test.Utils; using System.IO; namespace GoogleMapsApi.Test.IntegrationTests @@ -12,24 +12,12 @@ namespace GoogleMapsApi.Test.IntegrationTests public class BaseTestIntegration { const string ApiKeyEnvironmentVariable = "GOOGLE_API_KEY"; - private readonly IConfigurationRoot Configuration; public BaseTestIntegration() { - var builder = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddEnvironmentVariables(); - - string appsettingsPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json"); - if (File.Exists(appsettingsPath)) - { - builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); - } - - Configuration = builder.Build(); } - protected string ApiKey => Configuration.GetValue(ApiKeyEnvironmentVariable) + protected string ApiKey => AppSettings.Load()?.GoogleApiKey ?? Environment.GetEnvironmentVariable(ApiKeyEnvironmentVariable) ?? throw new InvalidOperationException($"API key is not configured. Please set the {ApiKeyEnvironmentVariable} environment variable."); } diff --git a/GoogleMapsApi.Test/IntegrationTests/DistanceMatrixTests.cs b/GoogleMapsApi.Test/IntegrationTests/DistanceMatrixTests.cs index 5858842..3e9ecc6 100644 --- a/GoogleMapsApi.Test/IntegrationTests/DistanceMatrixTests.cs +++ b/GoogleMapsApi.Test/IntegrationTests/DistanceMatrixTests.cs @@ -195,9 +195,7 @@ public async Task ShouldReplaceUriViaOnUriCreated() static Uri onUriCreated(Uri uri) { - var builder = new UriBuilder(uri); - builder.Query = builder.Query.Replace("placeholder", "1,2"); - return builder.Uri; + return new Uri(uri.ToString().Replace("placeholder", "1,2")); } GoogleMaps.DistanceMatrix.OnUriCreated += onUriCreated; diff --git a/GoogleMapsApi.Test/Utils/AppSettings.cs b/GoogleMapsApi.Test/Utils/AppSettings.cs new file mode 100644 index 0000000..9fc2373 --- /dev/null +++ b/GoogleMapsApi.Test/Utils/AppSettings.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GoogleMapsApi.Test.Utils +{ + internal class AppSettings + { + [JsonProperty(PropertyName ="GOOGLE_API_KEY")] + public string? GoogleApiKey { get; set; } + + public static AppSettings? Load() + { + var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json"); + if (!File.Exists(path)) return null; + return JsonConvert.DeserializeObject(File.ReadAllText(path)); + } + } +} diff --git a/GoogleMapsApi/GoogleMapsApi.csproj b/GoogleMapsApi/GoogleMapsApi.csproj index ecacfc8..f1e9086 100644 --- a/GoogleMapsApi/GoogleMapsApi.csproj +++ b/GoogleMapsApi/GoogleMapsApi.csproj @@ -1,6 +1,6 @@  - net8.0;net7.0;net6.0;netstandard2.1 + net8.0;net7.0;net6.0;netstandard2.0 latest 0.0.0 True