-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerisureClient.cs
93 lines (75 loc) · 3.63 KB
/
VerisureClient.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Verisure.GraphQL.Data;
namespace Verisure.GraphQL {
public class VerisureClient : IDisposable {
private bool _disposed = false;
internal readonly Uri ApiUrl = new Uri("https://m-api02.verisure.com");
internal HttpClient httpClient;
internal CookieContainer cookieContainer;
protected string username;
protected SecureString password;
public VerisureClient(string username, string password) : this(username, password.ToSecureString()) { }
public VerisureClient(string username, SecureString password) {
this.username = username;
this.password = password;
cookieContainer = new CookieContainer();
httpClient = new HttpClient(new VerisureRetryHandler(new HttpClientHandler() { CookieContainer = cookieContainer }, this)) {
BaseAddress = ApiUrl
};
httpClient.DefaultRequestHeaders.Add("APPLICATION_ID", "MyPages_via_GraphQL");
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
httpClient.DefaultRequestHeaders.Add("Pragma", "no-cache");
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15");
}
public void Dispose() {
if (_disposed) return;
httpClient?.Dispose();
password?.Dispose();
_disposed = true;
GC.SuppressFinalize(this);
}
public async Task<LoginResponse> LoginAsync() {
using var req = new HttpRequestMessage(HttpMethod.Post, "/auth/login");
req.Headers.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password.ToClearTextString()}"))}");
req.Headers.Add("APPLICATION_ID", "MyPages_Login");
var requestTime = DateTime.Now;
var res = await httpClient.SendAsync(req);
var contentText = await res.Content.ReadAsStringAsync();
var content = JsonSerializer.Deserialize<LoginResponse>(contentText);
content.RequestSent = requestTime;
return content;
}
public async Task<LoginResponse> RefreshTokenAsync() {
using var req = new HttpRequestMessage(HttpMethod.Post, "/auth/token");
req.Headers.Add("APPLICATION_ID", "MyPages_Login");
var requestTime = DateTime.Now;
var res = await httpClient.SendAsync(req);
var contentText = await res.Content.ReadAsStringAsync();
var content = JsonSerializer.Deserialize<LoginResponse>(contentText);
content.RequestSent = requestTime;
return content;
}
public async Task<dynamic> QueryAsync(GraphQLQuery query) {
return await QueryAsync(new[] { query });
}
public async Task<IDictionary<string, dynamic>> QueryAsync(GraphQLQuery[] queries) {
using var req = new HttpRequestMessage(HttpMethod.Post, "/graphql") {
Content = new StringContent(JsonSerializer.Serialize(queries.Select(x => x.ToDictionary())))
};
var res = await httpClient.SendAsync(req);
var contentText = await res.Content.ReadAsStringAsync();
var content = JsonSerializer.Deserialize<JsonElement>(contentText);
var dict = content.CreateDictionary();
return dict;
}
}
}