Repository navigation
Expand file tree
/
Copy pathReloopClient.cs
More file actions
215 lines (183 loc) · 6.84 KB
/
Copy pathReloopClient.cs
File metadata and controls
215 lines (183 loc) · 6.84 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System.Collections;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Reloop.Exceptions;
using Reloop.Services;
namespace Reloop;
public class ReloopClient : IDisposable
{
private const string DefaultBaseUrl = "https://reloop.sh";
private readonly string _apiKey;
private readonly string _baseUrl;
private readonly HttpClient _httpClient;
private readonly bool _ownsHttpClient;
private readonly JsonSerializerOptions _jsonOptions;
private readonly JsonSerializerOptions _jsonOptionsIncludeNull;
public ApiKeyService ApiKey { get; }
public ContactsService Contacts { get; }
public DomainService Domain { get; }
public MailService Mail { get; }
public WebhookService Webhook { get; }
public InboxService Inbox { get; }
public ReloopClient(string apiKey, string baseUrl = DefaultBaseUrl, HttpClient? httpClient = null)
{
if (string.IsNullOrWhiteSpace(apiKey))
{
throw new ArgumentException("Reloop SDK requires an apiKey.");
}
_apiKey = apiKey.Trim();
_baseUrl = NormalizeBaseUrl(baseUrl);
_ownsHttpClient = httpClient == null;
_httpClient = httpClient ?? new HttpClient();
_jsonOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true,
};
_jsonOptionsIncludeNull = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
ApiKey = new ApiKeyService(this);
Contacts = new ContactsService(this);
Domain = new DomainService(this);
Mail = new MailService(this);
Webhook = new WebhookService(this);
Inbox = new InboxService(this);
}
public string BaseUrl => _baseUrl;
internal JsonSerializerOptions JsonOptions => _jsonOptions;
public Task<T?> FetchAsync<T>(HttpMethod method, string path, object? body = null)
{
return FetchAsync<T>(method, path, body, null);
}
public async Task<T?> FetchAsync<T>(
HttpMethod method,
string path,
object? body,
Dictionary<string, string?>? query)
{
try
{
var requestUri = BuildRequestUri(path, query);
using var request = new HttpRequestMessage(method, requestUri);
request.Headers.TryAddWithoutValidation("x-api-key", _apiKey);
request.Headers.TryAddWithoutValidation("User-Agent", "reloop-dotnet/0.1.0");
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (body != null)
{
var options = ShouldIncludeNulls(body) ? _jsonOptionsIncludeNull : _jsonOptions;
var json = JsonSerializer.Serialize(body, options);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
using var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
var errorText = await response.Content.ReadAsStringAsync();
var errBody = new ApiErrorBody();
try
{
if (!string.IsNullOrWhiteSpace(errorText))
{
errBody = JsonSerializer.Deserialize<ApiErrorBody>(errorText, _jsonOptions)
?? new ApiErrorBody();
}
}
catch (JsonException)
{
errBody.Message = errorText;
}
throw new ReloopApiException(
(int)response.StatusCode,
response.ReasonPhrase ?? ((int)response.StatusCode).ToString(),
errBody);
}
if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
{
return default;
}
var responseText = await response.Content.ReadAsStringAsync();
if (string.IsNullOrWhiteSpace(responseText))
{
return default;
}
try
{
return JsonSerializer.Deserialize<T>(responseText, _jsonOptions);
}
catch (JsonException ex)
{
throw new ReloopApiException("Reloop response parsing error: " + ex.Message, ex);
}
}
catch (ReloopApiException)
{
throw;
}
catch (HttpRequestException ex)
{
throw new ReloopApiException("Reloop network error: " + ex.Message, ex);
}
}
internal static string NormalizeBaseUrl(string? baseUrl)
{
var trimmed = string.IsNullOrWhiteSpace(baseUrl) ? DefaultBaseUrl : baseUrl!.Trim();
while (trimmed.EndsWith("/", StringComparison.Ordinal))
{
trimmed = trimmed[..^1];
}
return string.IsNullOrEmpty(trimmed) ? DefaultBaseUrl : trimmed;
}
private Uri BuildRequestUri(string path, Dictionary<string, string?>? query)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException("Request path is required.", nameof(path));
}
// Never attach the API key to a caller-supplied absolute URL (credential exfiltration).
// Check the raw path before query append; avoid Uri.TryCreate Absolute on "/..." which
// resolves as file:// on Unix and would incorrectly reject relative API paths.
if (path.IndexOf("://", StringComparison.Ordinal) >= 0
|| path.StartsWith("//", StringComparison.Ordinal))
{
throw new ArgumentException("Request paths must be relative.", nameof(path));
}
var pathWithQuery = AppendQuery(path, query);
if (!pathWithQuery.StartsWith("/", StringComparison.Ordinal))
{
pathWithQuery = "/" + pathWithQuery;
}
return new Uri(_baseUrl + pathWithQuery);
}
private static string AppendQuery(string path, Dictionary<string, string?>? query)
{
if (query == null || query.Count == 0)
{
return path;
}
var parts = query
.Where(entry => entry.Value != null)
.Select(entry => Uri.EscapeDataString(entry.Key) + "=" + Uri.EscapeDataString(entry.Value!))
.ToList();
if (parts.Count == 0)
{
return path;
}
var separator = path.IndexOf('?') >= 0 ? "&" : "?";
return path + separator + string.Join("&", parts);
}
private static bool ShouldIncludeNulls(object body)
{
return body is IDictionary;
}
public void Dispose()
{
if (_ownsHttpClient)
{
_httpClient.Dispose();
}
}
}