Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/tuyanet.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2021-11-30 06:43:07 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2021-11-30 06:43:07 +0300
commit22cfc78fcda6b8a0691f2f99928a1b8a44b6f879 (patch)
treeabc2ffd89b27b0ae58d66ea59d67601d61ce57b3
parent49a1fc96d134ab95efbd5705a50ffc6d6617ec93 (diff)
Support for "POST", "PUT", "DELETE" methods.
-rw-r--r--TuyaApi.cs49
1 files changed, 35 insertions, 14 deletions
diff --git a/TuyaApi.cs b/TuyaApi.cs
index 90fbc27..2756311 100644
--- a/TuyaApi.cs
+++ b/TuyaApi.cs
@@ -65,6 +65,17 @@ namespace com.clusterrr.TuyaNet
India
}
+ /// <summary>
+ /// Request method.
+ /// </summary>
+ public enum Method
+ {
+ GET,
+ POST,
+ PUT,
+ DELETE
+ }
+
private static string RegionToHost(Region region)
{
string urlHost = null;
@@ -100,10 +111,11 @@ namespace com.clusterrr.TuyaNet
/// <param name="headers">Additional headers.</param>
/// <param name="noToken">Execute query without token.</param>
/// <returns>JSON string with response.</returns>
- public async Task<string> RequestAsync(string uri, string body = null, Dictionary<string, string> headers = null, bool noToken = false)
+ public async Task<string> RequestAsync(Method method, string uri, string body = null, Dictionary<string, string> headers = null, bool noToken = false)
{
+ while (uri.StartsWith("/")) uri = uri.Substring(1);
var urlHost = RegionToHost(region);
- var url = new Uri($"https://{urlHost}/v1.0/{uri}");
+ var url = new Uri($"https://{urlHost}/{uri}");
var now = (DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds.ToString("0");
string headersStr = "";
if (headers == null)
@@ -130,10 +142,10 @@ namespace com.clusterrr.TuyaNet
using (var sha256 = SHA256.Create())
{
- payload += "GET\n" +
- string.Concat(sha256.ComputeHash(Encoding.UTF8.GetBytes(body ?? "")).Select(b => $"{b:x2}")) + '\n' +
- headersStr + '\n' +
- url.PathAndQuery;
+ payload += $"{method}\n" +
+ string.Concat(sha256.ComputeHash(Encoding.UTF8.GetBytes(body ?? "")).Select(b => $"{b:x2}")) + '\n' +
+ headersStr + '\n' +
+ url.PathAndQuery;
}
string signature;
@@ -151,11 +163,20 @@ namespace com.clusterrr.TuyaNet
var httpRequestMessage = new HttpRequestMessage
{
- Method = HttpMethod.Get,
- RequestUri = url
+ Method = method switch
+ {
+ Method.GET => HttpMethod.Get,
+ Method.POST => HttpMethod.Post,
+ Method.PUT => HttpMethod.Delete,
+ Method.DELETE => HttpMethod.Delete,
+ _ => throw new NotSupportedException($"Unknow method - {method}")
+ },
+ RequestUri = url,
};
foreach (var h in headers)
httpRequestMessage.Headers.Add(h.Key, h.Value);
+ if (body != null)
+ httpRequestMessage.Content = new StringContent(body, Encoding.UTF8, "application/json");
using (var response = await httpClient.SendAsync(httpRequestMessage).ConfigureAwait(false))
{
@@ -174,8 +195,8 @@ namespace com.clusterrr.TuyaNet
/// <returns>Access token.</returns>
private async Task<TuyaToken> GetAccessTokenAsync()
{
- var uri = "token?grant_type=1";
- var response = await RequestAsync(uri, noToken: true);
+ var uri = "v1.0/token?grant_type=1";
+ var response = await RequestAsync(Method.GET, uri, noToken: true);
var token = JsonConvert.DeserializeObject<TuyaToken>(response);
return token;
}
@@ -199,8 +220,8 @@ namespace com.clusterrr.TuyaNet
/// <returns>Device info.</returns>
public async Task<TuyaDeviceApiInfo> GetDeviceInfoAsync(string deviceId)
{
- var uri = $"devices/{deviceId}";
- var response = await RequestAsync(uri);
+ var uri = $"v1.0/devices/{deviceId}";
+ var response = await RequestAsync(Method.GET, uri);
var device = JsonConvert.DeserializeObject<TuyaDeviceApiInfo>(response);
return device;
}
@@ -213,8 +234,8 @@ namespace com.clusterrr.TuyaNet
public async Task<TuyaDeviceApiInfo[]> GetAllDevicesInfoAsync(string anyDeviceId)
{
var userId = (await GetDeviceInfoAsync(anyDeviceId)).UserId;
- var uri = $"users/{userId}/devices";
- var response = await RequestAsync(uri);
+ var uri = $"v1.0/users/{userId}/devices";
+ var response = await RequestAsync(Method.GET, uri);
var devices = JsonConvert.DeserializeObject<TuyaDeviceApiInfo[]>(response);
return devices;
}