-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlueClient.cs
227 lines (181 loc) · 11.4 KB
/
BlueClient.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using MBW.Client.BlueRiiotApi.Builder;
using MBW.Client.BlueRiiotApi.Helpers;
using MBW.Client.BlueRiiotApi.Objects;
using MBW.Client.BlueRiiotApi.Objects.Enums;
using MBW.Client.BlueRiiotApi.RequestsResponses;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HttpMethod = System.Net.Http.HttpMethod;
namespace MBW.Client.BlueRiiotApi
{
public class BlueClient
{
private readonly ILogger<BlueClient> _logger;
private readonly IHttpClientProducer _httpClientProducer;
private readonly IRequestSigner _requestSigner;
private readonly JsonSerializer _serializer;
private readonly Encoding _encoding = new UTF8Encoding(false);
internal BlueClient(ILogger<BlueClient> logger, IHttpClientProducer httpClientProducer, IRequestSigner requestSigner)
{
_logger = logger;
_httpClientProducer = httpClientProducer;
_requestSigner = requestSigner;
_serializer = JsonSerializer.Create(new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
}
private T Parse<T>(Stream stream)
{
using (StreamReader sr = new StreamReader(stream, _encoding, false, 4096, true))
using (JsonTextReader jr = new JsonTextReader(sr))
return _serializer.Deserialize<T>(jr);
}
private Task<TResponse> PerformGet<TResponse>(string path, CancellationToken token) where TResponse : class
{
return PerformHttp<object, TResponse>(path, HttpMethod.Get, null, token);
}
private Task<TResponse> PerformPut<TRequest, TResponse>(string path, TRequest request, CancellationToken token) where TResponse : class
{
return PerformHttp<TRequest, TResponse>(path, HttpMethod.Put, request, token);
}
private Task<TResponse> PerformPost<TRequest, TResponse>(string path, TRequest request, CancellationToken token) where TResponse : class
{
return PerformHttp<TRequest, TResponse>(path, HttpMethod.Post, request, token);
}
private async Task<TResponse> PerformHttp<TRequest, TResponse>(string path, HttpMethod method, TRequest requestBody, CancellationToken token) where TResponse : class
{
HttpClient httpClient = _httpClientProducer.CreateClient();
using HttpRequestMessage req = new HttpRequestMessage(method, path);
if (requestBody != null)
req.Content = new JsonContent<TRequest>(_serializer, requestBody);
// Login if needed, then sign the request
await _requestSigner.LoginIfNeeded(this, token);
await _requestSigner.Sign(httpClient, req, token);
// Issue the request
using HttpResponseMessage resp = await httpClient.SendAsync(req, HttpCompletionOption.ResponseContentRead, token);
if (resp.Content.Headers.ContentType.MediaType != "application/json")
throw new Exception($"API request did not result in a Json object. Path: '{path}'.");
if (_logger.IsEnabled(LogLevel.Trace))
{
// Log request / Response
// Note: It should be fine to read the content twice, as HttpCompletionOption.ResponseContentRead is set
string responseBody = await resp.Content.ReadAsStringAsync();
_logger.LogTrace("Received {Code} for {Path}, with body: {Body}", resp.StatusCode, req.RequestUri, responseBody);
}
JObject obj = Parse<JObject>(await resp.Content.ReadAsStreamAsync());
if (obj.ContainsKey("errorMessage")/* && obj.ContainsKey("errorType")*/)
{
// This is an error, regardless of what the status code is
throw new Exception($"API responded with an error: {obj.Value<string>("errorMessage")}. Path: '{path}'.");
}
if (resp.StatusCode == HttpStatusCode.OK)
return obj.ToObject<TResponse>();
// Something is wrong, try fetching a message
if (obj.TryGetValue("errorMessage", out JToken errorMsg) || obj.TryGetValue("message", out errorMsg))
throw new Exception($"API resulted in a non-success status code. Message: {errorMsg}. Path: '{path}'.");
throw new Exception($"API resulted in a non-success status code. No futher details available. Path: '{path}'.");
}
internal async Task<LoginResponse> LoginWithUsernamePassword(string email, string password, CancellationToken token = default)
{
HttpClient httpClient = _httpClientProducer.CreateClient();
using HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "user/login");
req.Content = new StringContent(JsonConvert.SerializeObject(new
{
email,
password
}));
using HttpResponseMessage resp = await httpClient.SendAsync(req, HttpCompletionOption.ResponseContentRead, token);
resp.EnsureSuccessStatusCode();
LoginResponse loginResponse = Parse<LoginResponse>(await resp.Content.ReadAsStreamAsync());
return loginResponse;
}
public async Task<UserGetResponse> GetUser(CancellationToken token = default)
{
return await PerformGet<UserGetResponse>("user", token);
}
public async Task<BluesGetResponse> GetBlues(HwProductType productType, CancellationToken token = default)
{
if (productType == HwProductType.unknown)
return await PerformGet<BluesGetResponse>("blue", token);
return await PerformGet<BluesGetResponse>($"blue?blueProductType={productType}", token);
}
public async Task<BlueGetResponse> GetBlue(string serial, CancellationToken token = default)
{
return await PerformGet<BlueGetResponse>($"blue/{serial}", token);
}
public async Task<SwimmingPoolLastMeasurementsGetResponse> GetBlueLastMeasurements(string poolId, string serial, string mode = "blue_and_strip", CancellationToken token = default)
{
return await PerformGet<SwimmingPoolLastMeasurementsGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/blue/{HttpUtility.UrlEncode(serial)}/lastMeasurements?mode={HttpUtility.UrlEncode(mode)}", token);
}
public async Task<BlueReleaseLastUnprocessedEventResponse> BlueReleaseLastUnprocessedEvent(string serial, CancellationToken token = default)
{
return await PerformPost<object, BlueReleaseLastUnprocessedEventResponse>($"blue/{serial}/releaseLastUnprocessedEvent", null, token);
}
public async Task<BlueCompatibilityGetResponse> GetBlueCompatibility(string serial, CancellationToken token = default)
{
return await PerformGet<BlueCompatibilityGetResponse>($"blue/{serial}/compatibility", token);
}
public async Task<SwimmingPoolGetResponse> GetSwimmingPools(bool deleted = false, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolGetResponse>("swimming_pool?deleted=" + (deleted ? "true" : "false"), token);
}
public async Task<SwimmingPool> GetSwimmingPool(string poolId, CancellationToken token = default)
{
return await PerformGet<SwimmingPool>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}", token);
}
public async Task<SwimmingPool> PutSwimmingPool(string poolId, SwimmingPool swimmingPool, CancellationToken token = default)
{
return await PerformPut<SwimmingPool, SwimmingPool>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}", swimmingPool, token);
}
public async Task<SwimmingPoolFeedGetResponse> GetSwimmingPoolFeed(string poolId, string lang = null, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolFeedGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/feed?lang={HttpUtility.UrlEncode(lang)}", token);
}
public async Task<SwimmingPoolLastMeasurementsGetResponse> GetSwimmingPoolLastMeasurements(string poolId, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolLastMeasurementsGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/lastMeasurements", token);
}
public async Task<SwimmingPoolStatusGetResponse> GetSwimmingPoolStatus(string poolId, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolStatusGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/status", token);
}
public async Task<SwimmingPoolStatusTaskGetResponse> GetSwimmingPoolStatusTask(string poolId, string taskId, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolStatusTaskGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/status/{HttpUtility.UrlEncode(taskId)}", token);
}
public async Task<SwimmingPoolBlueDevicesGetResponse> GetSwimmingPoolBlueDevices(string poolId, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolBlueDevicesGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/blue", token);
}
public async Task<SwimmingPoolChemistryGetResponse> GetSwimmingPoolChemistry(string poolId, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolChemistryGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/chemistry", token);
}
public async Task<SwimmingPoolGuidanceGetResponse> GetSwimmingPoolGuidance(string poolId, string lang = null, string mode = "interactive_v03", CancellationToken token = default)
{
return await PerformGet<SwimmingPoolGuidanceGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/guidance?lang={HttpUtility.UrlEncode(lang)}&mode={HttpUtility.UrlEncode(mode)}", token);
}
public async Task<SwimmingPoolGuidanceHistoryGetResponse> GetSwimmingPoolGuidanceHistory(string poolId, string lang = null, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolGuidanceHistoryGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/guidance/history?lang={HttpUtility.UrlEncode(lang)}", token);
}
public async Task<SwimmingPoolWeatherGetResponse> GetSwimmingPoolWeather(string poolId, string lang = null, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolWeatherGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/weather?lang={HttpUtility.UrlEncode(lang)}", token);
}
public async Task<SwimmingPoolWeatherForecastGetResponse> GetSwimmingPoolWeatherForecast(string poolId, DateTime startDate, string lang = null, CancellationToken token = default)
{
return await PerformGet<SwimmingPoolWeatherForecastGetResponse>($"swimming_pool/{HttpUtility.UrlEncode(poolId)}/weather/forecast?startDate={startDate.ToUniversalTime():yyyy-MM-dd'T'HH:mm:ss'Z'}&lang={HttpUtility.UrlEncode(lang)}", token);
}
}
}