This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
DarkSkyService.cs
195 lines (167 loc) · 7.42 KB
/
DarkSkyService.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
namespace DarkSky.Services
{
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using static System.FormattableString;
/// <summary>
/// Wrapper class for interacting with the Dark Sky API.
/// </summary>
public class DarkSkyService : IDisposable
{
private readonly string apiKey;
private readonly Uri baseUri;
private readonly IHttpClient httpClient;
private readonly IJsonSerializerService jsonSerializerService;
/// <summary>
/// Initializes a new instance of the <see cref="DarkSkyService" /> class. A wrapper for the
/// Dark Sky API.
/// </summary>
/// <param name="apiKey">Your API key for the Dark Sky API.</param>
/// <param name="baseUri">The base URI for the Dark Sky API. Defaults to https://api.darksky.net/ .</param>
/// <param name="httpClient">
/// An optional HTTP client to contact an API with (useful for mocking data for testing).
/// </param>
/// <param name="jsonSerializerService">
/// An optional JSON Serializer to handle converting the response string to an object.
/// </param>
public DarkSkyService(string apiKey, Uri baseUri = null, IHttpClient httpClient = null, IJsonSerializerService jsonSerializerService = null)
{
if (string.IsNullOrWhiteSpace(apiKey))
{
throw new ArgumentException($"{nameof(apiKey)} cannot be empty.");
}
this.apiKey = apiKey;
this.baseUri = baseUri ?? new Uri("https://api.darksky.net/");
this.httpClient = httpClient ?? new ZipHttpClient();
this.jsonSerializerService = jsonSerializerService
?? new JsonNetJsonSerializerService();
}
/// <summary>
/// Destructor
/// </summary>
~DarkSkyService()
{
Dispose(false);
}
/// <summary>
/// Make a request to get forecast data.
/// </summary>
/// <param name="latitude">Latitude to request data for in decimal degrees.</param>
/// <param name="longitude">Longitude to request data for in decimal degrees.</param>
/// <param name="parameters">The OptionalParameters to use for the request.</param>
/// <returns>A DarkSkyResponse with the API headers and data.</returns>
public async Task<DarkSkyResponse> GetForecast(double latitude, double longitude,
OptionalParameters parameters = null)
{
var requestString = BuildRequestUri(latitude, longitude, parameters);
var response = await httpClient.HttpRequestAsync($"{baseUri}{requestString}").ConfigureAwait(false);
var responseContent = response.Content?.ReadAsStringAsync();
var darkSkyResponse = new DarkSkyResponse
{
IsSuccessStatus = response.IsSuccessStatusCode, ResponseReasonPhrase = response.ReasonPhrase
};
if (!darkSkyResponse.IsSuccessStatus)
{
return darkSkyResponse;
}
try
{
darkSkyResponse.Response =
await jsonSerializerService.DeserializeJsonAsync<Forecast>(responseContent).ConfigureAwait(false);
}
catch (FormatException e)
{
darkSkyResponse.Response = null;
darkSkyResponse.IsSuccessStatus = false;
darkSkyResponse.ResponseReasonPhrase = $"Error parsing results: {e?.InnerException?.Message ?? e.Message}";
}
response.Headers.TryGetValues("X-Forecast-API-Calls", out var apiCallsHeader);
response.Headers.TryGetValues("X-Response-Time", out var responseTimeHeader);
darkSkyResponse.Headers = new ResponseHeaders
{
CacheControl = response.Headers.CacheControl,
ApiCalls = long.TryParse(apiCallsHeader?.FirstOrDefault(), out var callsParsed)
? (long?)callsParsed
: null,
ResponseTime = responseTimeHeader?.FirstOrDefault()
};
if (darkSkyResponse.Response == null)
{
return darkSkyResponse;
}
if (darkSkyResponse.Response.Currently != null)
{
darkSkyResponse.Response.Currently.TimeZone = darkSkyResponse.Response.TimeZone;
}
darkSkyResponse.Response.Alerts?.ForEach(a => a.TimeZone = darkSkyResponse.Response.TimeZone);
darkSkyResponse.Response.Daily?.Data?.ForEach(d => d.TimeZone = darkSkyResponse.Response.TimeZone);
darkSkyResponse.Response.Hourly?.Data?.ForEach(h => h.TimeZone = darkSkyResponse.Response.TimeZone);
darkSkyResponse.Response.Minutely?.Data?.ForEach(
m => m.TimeZone = darkSkyResponse.Response.TimeZone);
return darkSkyResponse;
}
private string BuildRequestUri(double latitude, double longitude, OptionalParameters parameters)
{
var queryString = new StringBuilder(Invariant($"forecast/{apiKey}/{latitude:N4},{longitude:N4}"));
if (parameters?.ForecastDateTime != null)
{
queryString.Append(
$",{parameters.ForecastDateTime.Value.ToString("yyyy-MM-ddTHH:mm:ssK", CultureInfo.InvariantCulture)}");
}
if (parameters == null)
{
return queryString.ToString();
}
queryString.Append("?");
if (parameters.DataBlocksToExclude != null)
{
queryString.Append(
$"&exclude={string.Join(",", parameters.DataBlocksToExclude.Select(x => x.ToString().ToLowerInvariant()))}");
}
if (parameters.ExtendHourly != null && parameters.ExtendHourly.Value)
{
queryString.Append("&extend=hourly");
}
if (!string.IsNullOrWhiteSpace(parameters.LanguageCode))
{
queryString.Append($"&lang={parameters.LanguageCode}");
}
if (!string.IsNullOrWhiteSpace(parameters.MeasurementUnits))
{
queryString.Append($"&units={parameters.MeasurementUnits}");
}
return queryString.ToString();
}
#region IDisposable Support
private bool disposedValue; // To detect redundant calls
/// <summary>
/// Dispose of resources used by the class.
/// </summary>
/// <param name="disposing">If the class is disposing managed resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposedValue)
{
return;
}
if (disposing)
{
httpClient.Dispose();
}
disposedValue = true;
}
/// <summary>
/// Public access to start disposing of the class instance.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}