Skip to content

Commit

Permalink
first working example
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-zippenfenig committed Mar 14, 2024
1 parent cb26df5 commit d6b42ee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
7 changes: 6 additions & 1 deletion OpenMeteo.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
29 changes: 24 additions & 5 deletions OpenMeteo/OpenMeteo.cs
Original file line number Diff line number Diff line change
@@ -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()));
}

/// <summary>
/// Fetch weather data from an Open-Meteo API endpoint and decode messages using FlatBuffers
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public async Task<WeatherApiResponse[]> GetWeather(Uri uri)
{
var response = await Client.GetAsync(uri);
response.EnsureSuccessStatusCode();
var bytes = await response.Content.ReadAsByteArrayAsync();
return DecodeWeatherResponses(bytes);
}

/// <summary>
/// Convert an array of bytes to an array of FlatBuffer responses
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static WeatherApiResponse[] decodeWeatherResponse(byte[] bytes)
public static WeatherApiResponse[] DecodeWeatherResponses(byte[] bytes)
{
var buffer = new ByteBuffer(bytes);

Expand Down Expand Up @@ -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)
Expand All @@ -80,8 +98,9 @@ protected override async Task<HttpResponseMessage> SendAsync(
{
return response;
}
int waitMs = (int)Math.Min(BackoffFactor * Math.Pow(2, i), BackoffMaxSeconds) * 1000;
await Task.Delay(waitMs);
}

return response;

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
}
}
Expand Down

0 comments on commit d6b42ee

Please sign in to comment.