-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherLinkService.cs
50 lines (40 loc) · 2.11 KB
/
WeatherLinkService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using WeatherLink.Api_Model;
using WeatherLink.Facade;
namespace WeatherLink
{
public class WeatherLinkService
{
private readonly ApiFacade api;
private readonly string key;
private readonly string secret;
private readonly DateTime expiration;
private readonly long timestamp;
#region Constructor:
public WeatherLinkService(string key, string secret, DateTime expiration)
{
this.key = key;
this.secret = secret;
this.expiration = expiration;
api = new ApiFacade(new HttpClient());
timestamp = (long)expiration.Subtract(DateTime.UnixEpoch).TotalSeconds;
}
#endregion
public async Task<GetStationModel> GetStations()
{
var token = api.GenerateToken(secret, $"api-key{key}t{timestamp}", expiration);
return await api.GetAsync<GetStationModel>($@"https://api.weatherlink.com/v2/stations?api-key={key}&api-signature={token.signature}&t={timestamp}");
}
public async Task<GetCurrentConditionModel> GetCurrentConditions(string station)
{
var token = api.GenerateToken(secret, $"api-key{key}station-id{station}t{timestamp}", expiration);
return await api.GetAsync<GetCurrentConditionModel>($@"https://api.weatherlink.com/v2/current/{station}?api-key={key}&api-signature={token.signature}&t={timestamp}");
}
public async Task<GetHistoricConditionModel> GetHistoricConditions(string station, DateTime start, DateTime end)
{
var startUnix = (long)start.Subtract(DateTime.UnixEpoch).TotalSeconds;
var endUnix = (long)end.Subtract(DateTime.UnixEpoch).TotalSeconds;
var token = api.GenerateToken(secret, $"api-key{key}end-timestamp{endUnix}start-timestamp{startUnix}station-id{station}t{timestamp}", expiration);
return await api.GetAsync<GetHistoricConditionModel>($@"https://api.weatherlink.com/v2/historic/{station}?api-key={key}&t={timestamp}&end-timestamp={endUnix}&start-timestamp={startUnix}&api-signature={token.signature}");
}
}
}