-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTenorClient.cs
468 lines (407 loc) · 11.8 KB
/
TenorClient.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using System.Globalization;
using System.Net.Http.Json;
using System.Text;
using Valour.TenorTwo.Models;
using Valour.TenorTwo.Responses;
namespace Valour.TenorTwo;
/// <summary>
/// The client for interacting with the Tenor API
/// </summary>
public class TenorClient
{
/// <summary>
/// Base address used to reach Tenor API
/// </summary>
private const string BaseAddress = "https://tenor.googleapis.com/v2/";
/// <summary>
/// Internal store of API key
/// </summary>
private readonly string _apiKey;
/// <summary>
/// The ISO 3166-1 country code
/// </summary>
private readonly string _country;
/// <summary>
/// The default language to interpret the search string (xx_YY).
/// xx is the language's ISO 639-1 language code, while the optional
/// _YY value is the two-letter ISO 3166-1 country code.
/// </summary>
private readonly string _locale;
/// <summary>
/// Internal Http client
/// </summary>
private readonly HttpClient _http;
/// <summary>
/// A value used to differentiate requests between integrations
/// </summary>
private readonly string _clientKey;
public TenorClient(string key, string clientKey = null, string country = null, string locale = null, HttpClient http = null)
{
_apiKey = key;
_clientKey = clientKey;
if (http is not null)
{
_http = http;
}
else
{
_http = new HttpClient();
_http.BaseAddress = new Uri(BaseAddress);
}
var culture = CultureInfo.CurrentCulture;
try
{
_country = country ?? (new RegionInfo(culture.Name)).TwoLetterISORegionName;
}
catch
{
_country = "US";
}
try
{
_locale = locale ?? $"{culture.TwoLetterISOLanguageName}_{_country}";
}
catch
{
_locale = "en_US";
}
}
/// <summary>
/// Returns the media for the given ids using the Tenor API
/// </summary>
public async Task<MediaResponse> Posts(
IEnumerable<string> ids,
int limit = 20,
string position = null,
List<MediaFormatType> formatFilter = null)
{
var sb = new StringBuilder(64);
// Address and endpoint
sb.Append(BaseAddress);
sb.Append(Endpoints.Search);
// Key
sb.Append("?key=");
sb.Append(_apiKey);
// Query
sb.Append("&ids=");
sb.Append(string.Join(',', ids));
// Country
sb.Append("&country=");
sb.Append(_country);
// Locale
sb.Append("&locale=");
sb.Append(_locale);
// Limit
sb.Append("&limit=");
sb.Append(limit);
if (_clientKey is not null)
{
sb.Append("&client_key=");
sb.Append(_clientKey);
}
if (formatFilter is not null)
{
sb.Append("&media_filter=");
sb.Append(string.Join(',', formatFilter));
}
if (position is not null)
{
sb.Append("&pos=");
sb.Append(position);
}
return await _http.GetFromJsonAsync<MediaResponse>(sb.ToString());
}
/// <summary>
/// Used to search for media via the Tenor API
/// </summary>
public async Task<MediaResponse> Search(
string query,
int limit = 20,
string position = null,
List<MediaFormatType> formatFilter = null,
ContentFilter? contentFilter = null,
SearchFilter searchFilter = null,
AspectRatio? aspectRatio = null,
bool? random = null)
{
var sb = new StringBuilder(64);
// Address and endpoint
sb.Append(BaseAddress);
sb.Append(Endpoints.Search);
// Key
sb.Append("?key=");
sb.Append(_apiKey);
// Query
sb.Append("&q=");
sb.Append(query);
// Country
sb.Append("&country=");
sb.Append(_country);
// Locale
sb.Append("&locale=");
sb.Append(_locale);
// Limit
sb.Append("&limit=");
sb.Append(limit);
if (_clientKey is not null)
{
sb.Append("&client_key=");
sb.Append(_clientKey);
}
if (searchFilter is not null)
{
sb.Append("&searchfilter=");
sb.Append(searchFilter);
}
if (contentFilter is not null)
{
sb.Append("&contentfilter=");
sb.Append(contentFilter);
}
if (formatFilter is not null)
{
sb.Append("&media_filter=");
sb.Append(string.Join(',', formatFilter));
}
if (aspectRatio is not null)
{
sb.Append("&ar_range=");
sb.Append(aspectRatio);
}
if (random is not null)
{
sb.Append("&random=");
sb.Append(random);
}
if (position is not null)
{
sb.Append("&pos=");
sb.Append(position);
}
return await _http.GetFromJsonAsync<MediaResponse>(sb.ToString());
}
/// <summary>
/// Returns the trending media in the Tenor API
/// </summary>
public async Task<FeaturedResponse> Featured(
int limit = 20,
string position = null,
List<MediaFormatType> formatFilter = null,
ContentFilter? contentFilter = null,
SearchFilter searchFilter = null,
AspectRatio? aspectRatio = null,
bool? random = null)
{
var sb = new StringBuilder(64);
// Address and endpoint
sb.Append(BaseAddress);
sb.Append(Endpoints.Featured);
// Key
sb.Append("?key=");
sb.Append(_apiKey);
// Country
sb.Append("&country=");
sb.Append(_country);
// Locale
sb.Append("&locale=");
sb.Append(_locale);
// Limit
sb.Append("&limit=");
sb.Append(limit);
if (_clientKey is not null)
{
sb.Append("&client_key=");
sb.Append(_clientKey);
}
if (searchFilter is not null)
{
sb.Append("&searchfilter=");
sb.Append(searchFilter);
}
if (contentFilter is not null)
{
sb.Append("&contentfilter=");
sb.Append(contentFilter);
}
if (formatFilter is not null)
{
sb.Append("&media_filter=");
sb.Append(string.Join(',', formatFilter));
}
if (aspectRatio is not null)
{
sb.Append("&ar_range=");
sb.Append(aspectRatio);
}
if (random is not null)
{
sb.Append("&random=");
sb.Append(random);
}
if (position is not null)
{
sb.Append("&pos=");
sb.Append(position);
}
return await _http.GetFromJsonAsync<FeaturedResponse>(sb.ToString());
}
/// <summary>
/// Returns the categories in the Tenor API
/// </summary>
public async Task<CategoriesResponse> Categories(
CategoryType type = CategoryType.Featured,
ContentFilter? contentFilter = null)
{
var sb = new StringBuilder(64);
// Address and endpoint
sb.Append(BaseAddress);
sb.Append(Endpoints.Categories);
// Key
sb.Append("?key=");
sb.Append(_apiKey);
// Country
sb.Append("&country=");
sb.Append(_country);
// Locale
sb.Append("&locale=");
sb.Append(_locale);
// Type
sb.Append("&type=");
sb.Append(type);
if (_clientKey is not null)
{
sb.Append("&client_key=");
sb.Append(_clientKey);
}
if (contentFilter is not null)
{
sb.Append("&contentfilter=");
sb.Append(contentFilter);
}
return await _http.GetFromJsonAsync<CategoriesResponse>(sb.ToString());
}
/// <summary>
/// Returns search suggestions for the given query using the Tenor API
/// </summary>
public async Task<SearchSuggestionsResponse> SearchSuggestions(string query, int limit = 20)
{
var sb = new StringBuilder(64);
// Address and endpoint
sb.Append(BaseAddress);
sb.Append(Endpoints.Suggestions);
// Key
sb.Append("?key=");
sb.Append(_apiKey);
// Country
sb.Append("&country=");
sb.Append(_country);
// Locale
sb.Append("&locale=");
sb.Append(_locale);
// Query
sb.Append("&q=");
sb.Append(query);
// Limit
sb.Append("&limit=");
sb.Append(limit);
if (_clientKey is not null)
{
sb.Append("&client_key=");
sb.Append(_clientKey);
}
return await _http.GetFromJsonAsync<SearchSuggestionsResponse>(sb.ToString());
}
/// <summary>
/// Returns autocomplete options for the given query using the Tenor API
/// </summary>
public async Task<AutocompleteResponse> Autocomplete(string query, int limit = 20)
{
var sb = new StringBuilder(64);
// Address and endpoint
sb.Append(BaseAddress);
sb.Append(Endpoints.Suggestions);
// Key
sb.Append("?key=");
sb.Append(_apiKey);
// Country
sb.Append("&country=");
sb.Append(_country);
// Locale
sb.Append("&locale=");
sb.Append(_locale);
// Query
sb.Append("&q=");
sb.Append(query);
// Limit
sb.Append("&limit=");
sb.Append(limit);
if (_clientKey is not null)
{
sb.Append("&client_key=");
sb.Append(_clientKey);
}
return await _http.GetFromJsonAsync<AutocompleteResponse>(sb.ToString());
}
/// <summary>
/// Returns currently trending terms using the Tenor API
/// </summary>
public async Task<TrendingTermsResponse> TrendingTerms(int limit = 20)
{
var sb = new StringBuilder(64);
// Address and endpoint
sb.Append(BaseAddress);
sb.Append(Endpoints.Suggestions);
// Key
sb.Append("?key=");
sb.Append(_apiKey);
// Country
sb.Append("&country=");
sb.Append(_country);
// Locale
sb.Append("&locale=");
sb.Append(_locale);
// Limit
sb.Append("&limit=");
sb.Append(limit);
if (_clientKey is not null)
{
sb.Append("&client_key=");
sb.Append(_clientKey);
}
return await _http.GetFromJsonAsync<TrendingTermsResponse>(sb.ToString());
}
/// <summary>
/// Registers a share for the given media id
/// Query should be the search term that led to the share
/// </summary>
public async Task RegisterShare(string id, string query = null)
{
var sb = new StringBuilder(64);
// Address and endpoint
sb.Append(BaseAddress);
sb.Append(Endpoints.RegisterShare);
// Key
sb.Append("?key=");
sb.Append(_apiKey);
// Country
sb.Append("&country=");
sb.Append(_country);
// Locale
sb.Append("&locale=");
sb.Append(_locale);
// Id
sb.Append("&id=");
sb.Append(id);
if (_clientKey is not null)
{
sb.Append("&client_key=");
sb.Append(_clientKey);
}
if (query is not null)
{
sb.Append("&q=");
sb.Append(query);
}
await _http.GetAsync(sb.ToString());
}
}