-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTodoistRestClient.cs
141 lines (120 loc) · 4.54 KB
/
TodoistRestClient.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace Todoist.Net
{
internal sealed class TodoistRestClient : ITodoistRestClient
{
private readonly HttpClient _httpClient;
private readonly bool _disposeHttpClient;
public TodoistRestClient() : this(null, (IWebProxy)null)
{
}
public TodoistRestClient(string token) : this(token, (IWebProxy)null)
{
}
public TodoistRestClient(IWebProxy proxy) : this(null, proxy)
{
}
public TodoistRestClient(string token, IWebProxy proxy)
{
var httpClientHandler = new HttpClientHandler();
if (proxy != null)
{
httpClientHandler.Proxy = proxy;
httpClientHandler.UseProxy = true;
}
// ReSharper disable once ExceptionNotDocumented
_httpClient = new HttpClient(httpClientHandler)
{
BaseAddress = new Uri("https://api.todoist.com/sync/v9/")
};
if (!string.IsNullOrEmpty(token))
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
_disposeHttpClient = true;
}
public TodoistRestClient(string token, HttpClient httpClient)
{
_httpClient = httpClient;
_httpClient.BaseAddress = new Uri("https://api.todoist.com/sync/v9/");
if (!string.IsNullOrEmpty(token))
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
}
public void Dispose()
{
if (_disposeHttpClient)
{
_httpClient?.Dispose();
}
}
/// <inheritdoc/>
public async Task<HttpResponseMessage> GetAsync(
string resource,
IEnumerable<KeyValuePair<string, string>> parameters,
CancellationToken cancellationToken = default)
{
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}
if (string.IsNullOrEmpty(resource))
{
throw new ArgumentException("Value cannot be null or empty.", nameof(resource));
}
var requestUri = string.Empty;
using (var content = new FormUrlEncodedContent(parameters))
{
var query = await content.ReadAsStringAsync().ConfigureAwait(false);
requestUri = $"{resource}?{query}";
}
return await _httpClient.GetAsync(requestUri, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task<HttpResponseMessage> PostAsync(
string resource,
IEnumerable<KeyValuePair<string, string>> parameters,
CancellationToken cancellationToken = default)
{
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}
if (string.IsNullOrEmpty(resource))
{
throw new ArgumentException("Value cannot be null or empty.", nameof(resource));
}
using (var content = new FormUrlEncodedContent(parameters))
{
return await _httpClient.PostAsync(resource, content, cancellationToken).ConfigureAwait(false);
}
}
/// <inheritdoc/>
public async Task<HttpResponseMessage> PostFormAsync(
string resource,
IEnumerable<KeyValuePair<string, string>> parameters,
IEnumerable<ByteArrayContent> files,
CancellationToken cancellationToken = default)
{
using (var multipartFormDataContent = new MultipartFormDataContent())
{
foreach (var keyValuePair in parameters)
{
multipartFormDataContent.Add(new StringContent(keyValuePair.Value), $"\"{keyValuePair.Key}\"");
}
foreach (var file in files)
{
multipartFormDataContent.Add(file, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
}
return await _httpClient.PostAsync(resource, multipartFormDataContent, cancellationToken).ConfigureAwait(false);
}
}
}
}