public async Task <EwelinkDtoDeviceList> GetDevicesAsync() { return(await _tokenCacheManager.RetryWithTokenAsync(async (accessToken, _) => { var urlBuilder = new UriBuilder(_httpClient.BaseAddress) { Path = $"api/user/device" }; var queryParams = new Dictionary <string, string> { ["lang"] = EwelinkHelper.Constants.LANG_EN, ["appid"] = _config.AppId, ["nonce"] = EwelinkHelper.GenerateNonce(), ["ts"] = EwelinkHelper.GenerateTimestamp(), ["version"] = EwelinkHelper.Constants.VERSION, ["getTags"] = EwelinkHelper.Constants.GET_TAGS_OFF, }; var url = QueryHelpers.AddQueryString(urlBuilder.ToString(), queryParams); var request = new HttpRequestMessage(HttpMethod.Get, url); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var response = await _httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); var responseContent = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize <EwelinkDtoDeviceList>(responseContent); return result; })); }
public async Task SetDeviceParamsAsync(string deviceId, object parameters) { await _tokenCacheManager.RetryWithTokenAsync(async (accessToken, _) => { var urlBuilder = new UriBuilder(_httpClient.BaseAddress) { Path = $"api/user/device/status" }; var body = JsonSerializer.Serialize(new { deviceid = deviceId, @params = parameters, appid = _config.AppId, nonce = EwelinkHelper.GenerateNonce(), ts = EwelinkHelper.GenerateTimestamp(), version = EwelinkHelper.Constants.VERSION }); var url = urlBuilder.ToString(); var request = new HttpRequestMessage(HttpMethod.Post, url); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); request.Content = new StringContent(body, Encoding.UTF8, "application/json"); var response = await _httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); var responseContent = await response.Content.ReadAsStringAsync(); }); }
public async Task Open(string accessToken, string apiKey) { await _webSocket.ConnectAsync(new Uri(_config.WebSocketUrl), _cts.Token); _webSocketListener.Start(); var seq = EwelinkHelper.GenerateSequence(); var credentialsMsg = JsonSerializer.Serialize(new { action = "userOnline", at = accessToken, apikey = apiKey, appid = _config.AppId, nonce = EwelinkHelper.GenerateNonce(), ts = seq.timestamp, userAgent = EwelinkHelper.Constants.USER_AGENT, sequence = seq.sequence, version = EwelinkHelper.Constants.VERSION, }); await SendTextAsync(credentialsMsg); }
public async Task LoginAsync(string email, string password) { var urlBuilder = new UriBuilder(_httpClient.BaseAddress) { Path = $"api/user/login" }; var body = JsonSerializer.Serialize(new { email, password, appid = _config.AppId, nonce = EwelinkHelper.GenerateNonce(), ts = EwelinkHelper.GenerateTimestamp(), version = EwelinkHelper.Constants.VERSION, }); var url = urlBuilder.ToString(); var signature = _cryptoService.HmacSha256Base64(body, _config.AppSecret); var request = new HttpRequestMessage(HttpMethod.Post, url); request.Headers.Authorization = new AuthenticationHeaderValue("Sign", signature); request.Content = new StringContent(body, Encoding.UTF8, "application/json"); var response = await _httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); var responseContent = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize <EwelinkDtoLoginResult>(responseContent); var token = new AuthToken(result.at, result.rt, ("apiKey", result.apikey)); _tokenCacheManager.SetAuthToken(token); }
async Task <AuthToken> IAuthTokenProvider.RefreshAuthTokenAsync(string refreshToken) { var urlBuilder = new UriBuilder(_httpClient.BaseAddress) { Path = $"api/user/refresh" }; var queryParams = new Dictionary <string, string> { ["rt"] = refreshToken, ["grantType"] = EwelinkHelper.Constants.GRANT_TYPE_REFRESH, ["appid"] = _config.AppId, ["nonce"] = EwelinkHelper.GenerateNonce(), ["ts"] = EwelinkHelper.GenerateTimestamp(), ["version"] = EwelinkHelper.Constants.VERSION, }; var url = QueryHelpers.AddQueryString(urlBuilder.ToString(), queryParams); var signatureBase = string.Join('&', queryParams.OrderBy(x => x.Key).Select(x => $"{x.Key}={x.Value}")); var signature = _cryptoService.HmacSha256Base64(signatureBase, _config.AppSecret); var request = new HttpRequestMessage(HttpMethod.Get, url); request.Headers.Authorization = new AuthenticationHeaderValue("Sign", signature); var response = await _httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); var responseContent = await response.Content.ReadAsStringAsync(); var result = JsonSerializer.Deserialize <EwelinkDtoLoginResult>(responseContent); return(new AuthToken(result.at, result.rt, ("apiKey", result.apikey))); }