Skip to content

Commit

Permalink
Fix #19 - Error 400 with decimal seperator (#20)
Browse files Browse the repository at this point in the history
closes #19
  • Loading branch information
sbiaudet authored Feb 23, 2023
1 parent eef8ec4 commit 57adeeb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
25 changes: 5 additions & 20 deletions OpenMeteo/OpenMeteoClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using System.Text.Json;
using System.Globalization;

namespace OpenMeteo
{
Expand All @@ -15,28 +15,13 @@ public class OpenMeteoClient
private readonly string _geocodeApiUrl = "https://geocoding-api.open-meteo.com/v1/search";
private readonly string _airQualityApiUrl = "https://air-quality-api.open-meteo.com/v1/air-quality";
private readonly HttpController httpController;
private readonly System.Globalization.CultureInfo _culture;

/// <summary>
/// Creates a new <seealso cref="OpenMeteoClient"/> object and sets the neccessary variables (httpController, CultureInfo)
/// </summary>
public OpenMeteoClient()
{
httpController = new HttpController();

// Used to parse floats with "." (many countries "," is standard when parsing float to string) when creating query string
_culture = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
}

/// <summary>
/// Restores the original CultureInfo
/// </summary>
~OpenMeteoClient()
{
System.Threading.Thread.CurrentThread.CurrentCulture = _culture;
}

/// <summary>
Expand Down Expand Up @@ -350,14 +335,14 @@ private string MergeUrlWithOptions(string url, WeatherForecastOptions? options)
}

// Add the properties

// Begin with Latitude and Longitude since they're required
if (isFirstParam)
uri.Query += "latitude=" + options.Latitude;
uri.Query += "latitude=" + options.Latitude.ToString(CultureInfo.InvariantCulture);
else
uri.Query += "&latitude=" + options.Latitude;
uri.Query += "&latitude=" + options.Latitude.ToString(CultureInfo.InvariantCulture);

uri.Query += "&longitude=" + options.Longitude;
uri.Query += "&longitude=" + options.Longitude.ToString(CultureInfo.InvariantCulture);

uri.Query += "&temperature_unit=" + options.Temperature_Unit.ToString();
uri.Query += "&windspeed_unit=" + options.Windspeed_Unit.ToString();
Expand Down
27 changes: 23 additions & 4 deletions OpenMeteoTests/WeatherForecastTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenMeteo;

Expand All @@ -24,14 +26,31 @@ public async Task Latitude_Longitude_Test()
{
OpenMeteoClient client = new OpenMeteoClient();

WeatherForecast weatherData = await client.QueryAsync(1f, 2f);
WeatherForecast weatherData = await client.QueryAsync(1.125f, 2.25f);

Assert.IsNotNull(weatherData);
Assert.IsNotNull(weatherData.Longitude);
Assert.IsNotNull(weatherData.Latitude);

Assert.AreEqual(1f, weatherData.Latitude);
Assert.AreEqual(2f, weatherData.Longitude);
Assert.AreEqual(1.125f, weatherData.Latitude);
Assert.AreEqual(2.25f, weatherData.Longitude);
}

[TestMethod]
public async Task Latitude_Longitude_Test_With_French_Culture()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

OpenMeteoClient client = new OpenMeteoClient();

WeatherForecast weatherData = await client.QueryAsync(1.125f, 2.25f);

Assert.IsNotNull(weatherData);
Assert.IsNotNull(weatherData.Longitude);
Assert.IsNotNull(weatherData.Latitude);

Assert.AreEqual(1.125f, weatherData.Latitude);
Assert.AreEqual(2.25f, weatherData.Longitude);
}

[TestMethod]
Expand Down

0 comments on commit 57adeeb

Please sign in to comment.