diff --git a/OpenMeteo.Tests/UnitTest1.cs b/OpenMeteo.Tests/UnitTest1.cs
index 83dca76..6b794da 100644
--- a/OpenMeteo.Tests/UnitTest1.cs
+++ b/OpenMeteo.Tests/UnitTest1.cs
@@ -1,10 +1,15 @@
namespace OpenMeteo.Tests;
+using OpenMeteo;
public class UnitTest1
{
[Fact]
- public void Test1()
+ public async void Test1()
{
+ var client = new OpenMeteoClient();
+ var uri = new Uri("https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m&format=flatbuffers");
+ var results = await client.GetWeather(uri);
+ Console.WriteLine(results[0].Latitude);
}
}
\ No newline at end of file
diff --git a/OpenMeteo/OpenMeteo.cs b/OpenMeteo/OpenMeteo.cs
index 9e5357a..d5fdc99 100644
--- a/OpenMeteo/OpenMeteo.cs
+++ b/OpenMeteo/OpenMeteo.cs
@@ -1,31 +1,46 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
+using System.Collections.Generic;
+using System;
using openmeteo_sdk;
using Google.FlatBuffers;
namespace OpenMeteo
{
- public class OpenMeteo
+ public class OpenMeteoClient
{
private HttpClient Client;
- public OpenMeteo(HttpClient client)
+ public OpenMeteoClient(HttpClient client)
{
this.Client = client;
}
- public OpenMeteo()
+ public OpenMeteoClient()
{
this.Client = new HttpClient(new RetryHandler(new HttpClientHandler()));
}
+ ///
+ /// Fetch weather data from an Open-Meteo API endpoint and decode messages using FlatBuffers
+ ///
+ ///
+ ///
+ public async Task GetWeather(Uri uri)
+ {
+ var response = await Client.GetAsync(uri);
+ response.EnsureSuccessStatusCode();
+ var bytes = await response.Content.ReadAsByteArrayAsync();
+ return DecodeWeatherResponses(bytes);
+ }
+
///
/// Convert an array of bytes to an array of FlatBuffer responses
///
///
///
- public static WeatherApiResponse[] decodeWeatherResponse(byte[] bytes)
+ public static WeatherApiResponse[] DecodeWeatherResponses(byte[] bytes)
{
var buffer = new ByteBuffer(bytes);
@@ -62,6 +77,9 @@ public static WeatherApiResponse[] decodeWeatherResponse(byte[] bytes)
public class RetryHandler : DelegatingHandler
{
private const int MaxRetries = 3;
+ private const double BackoffFactor = 0.5;
+ private const int BackoffMaxSeconds = 2;
+
public RetryHandler(HttpMessageHandler innerHandler)
: base(innerHandler)
@@ -80,8 +98,9 @@ protected override async Task SendAsync(
{
return response;
}
+ int waitMs = (int)Math.Min(BackoffFactor * Math.Pow(2, i), BackoffMaxSeconds) * 1000;
+ await Task.Delay(waitMs);
}
-
return response;
}
}