-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67bfd6e
commit cb26df5
Showing
1 changed file
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,89 @@ | ||
namespace OpenMeteo { | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using openmeteo_sdk; | ||
using Google.FlatBuffers; | ||
|
||
namespace OpenMeteo | ||
{ | ||
public class OpenMeteo | ||
{ | ||
private HttpClient Client; | ||
|
||
public OpenMeteo(HttpClient client) | ||
{ | ||
this.Client = client; | ||
} | ||
|
||
public OpenMeteo() | ||
{ | ||
this.Client = new HttpClient(new RetryHandler(new HttpClientHandler())); | ||
} | ||
|
||
/// <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) | ||
{ | ||
var buffer = new ByteBuffer(bytes); | ||
|
||
// Get number of total weather response messages | ||
var total = 0; | ||
while (buffer.Position < buffer.Length) | ||
{ | ||
var length = buffer.GetInt(buffer.Position); | ||
buffer.Position += length; | ||
total += 1; | ||
} | ||
|
||
// Prepare output array | ||
var results = new WeatherApiResponse[total]; | ||
var i = 0; | ||
buffer.Position = 0; | ||
|
||
// Fill messages in array | ||
while (buffer.Position < buffer.Length) | ||
{ | ||
var length = buffer.GetInt(buffer.Position); | ||
buffer.Position += 4; | ||
results[i] = WeatherApiResponse.GetRootAsWeatherApiResponse(buffer); | ||
buffer.Position += length; | ||
i += 1; | ||
} | ||
return results; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Retry failed HTTP requests. See: https://stackoverflow.com/a/19650002 | ||
/// </summary> | ||
public class RetryHandler : DelegatingHandler | ||
{ | ||
private const int MaxRetries = 3; | ||
|
||
public RetryHandler(HttpMessageHandler innerHandler) | ||
: base(innerHandler) | ||
{ } | ||
|
||
|
||
protected override async Task<HttpResponseMessage> SendAsync( | ||
HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
HttpResponseMessage response = null; | ||
for (int i = 0; i < MaxRetries; i++) | ||
{ | ||
response = await base.SendAsync(request, cancellationToken); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
return response; | ||
} | ||
} | ||
|
||
} | ||
return response; | ||
} | ||
} | ||
} | ||
|