-
Notifications
You must be signed in to change notification settings - Fork 177
/
ZoomClient.cs
69 lines (59 loc) · 2.65 KB
/
ZoomClient.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
using System.Threading;
using System.Threading.Tasks;
using Bot.Builder.Community.Adapters.Zoom.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using RestSharp;
using RestSharp.Authenticators;
namespace Bot.Builder.Community.Adapters.Zoom
{
public class ZoomClient
{
private string _baseUrl = "https://api.zoom.us";
private readonly string _clientId;
private readonly string _clientSecret;
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
};
public ZoomClient(string clientId, string clientSecret)
{
_clientId = clientId;
_clientSecret = clientSecret;
}
public async Task<IRestResponse> SendMessageAsync(ChatResponse response, CancellationToken cancellationToken)
{
var body = JsonConvert.SerializeObject(response, JsonSerializerSettings);
return await SendPostRequest("/v2/im/chat/messages", body, cancellationToken);
}
private async Task<IRestResponse> SendPostRequest(string path, string body, CancellationToken cancellationToken)
{
var client = new RestClient($"{_baseUrl}{path}")
{
Timeout = -1,
};
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", $"Bearer {await GetAccessToken(cancellationToken)}");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", body, ParameterType.RequestBody);
return await client.ExecuteAsync(request, cancellationToken);
}
private async Task<string> GetAccessToken(CancellationToken cancellationToken)
{
var client = new RestClient($"{_baseUrl}/oauth/token?grant_type=client_credentials")
{
Timeout = -1,
Authenticator = new HttpBasicAuthenticator(_clientId, _clientSecret)
};
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "", ParameterType.RequestBody);
var response = await client.ExecuteAsync(request, cancellationToken);
var tokenObject = JObject.Parse(response.Content);
return tokenObject["access_token"].ToString();
}
}
}