-
Notifications
You must be signed in to change notification settings - Fork 41
/
DeliveryClient.cs
525 lines (445 loc) · 26 KB
/
DeliveryClient.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using KenticoCloud.Delivery.InlineContentItems;
using Microsoft.Extensions.Options;
namespace KenticoCloud.Delivery
{
/// <summary>
/// Executes requests against the Kentico Cloud Delivery API.
/// </summary>
public sealed class DeliveryClient : IDeliveryClient
{
private readonly DeliveryOptions _deliveryOptions;
private HttpClient _httpClient;
private DeliveryEndpointUrlBuilder _urlBuilder;
private ICodeFirstModelProvider _codeFirstModelProvider;
private IInlineContentItemsProcessor _inlineContentItemsProcessor;
/// <summary>
/// Gets or sets an object that resolves links to content items in Rich text element values.
/// </summary>
public IContentLinkUrlResolver ContentLinkUrlResolver { get; set; }
/// <summary>
/// Gets processor for richtext elements retrieved with this client.
/// </summary>
public IInlineContentItemsProcessor InlineContentItemsProcessor
{
get
{
if (_inlineContentItemsProcessor == null)
{
var unretrievedInlineContentItemsResolver = new ReplaceWithWarningAboutUnretrievedItemResolver();
var defaultInlineContentItemsResolver = new ReplaceWithWarningAboutRegistrationResolver();
_inlineContentItemsProcessor = new InlineContentItemsProcessor(defaultInlineContentItemsResolver, unretrievedInlineContentItemsResolver);
}
return _inlineContentItemsProcessor;
}
private set
{
_inlineContentItemsProcessor = value;
}
}
/// <summary>
/// Gets or sets an object that performs conversion of content items to code-first models.
/// </summary>
public ICodeFirstModelProvider CodeFirstModelProvider
{
get { return _codeFirstModelProvider ?? (_codeFirstModelProvider = new CodeFirstModelProvider(this)); }
set { _codeFirstModelProvider = value; }
}
private DeliveryEndpointUrlBuilder UrlBuilder
{
get { return _urlBuilder ?? (_urlBuilder = new DeliveryEndpointUrlBuilder(_deliveryOptions)); }
}
/// <summary>
/// An instance of <see cref="System.Net.Http.HttpClient"/> for sending requests to KC endpoints.
/// </summary>
public HttpClient HttpClient
{
get
{
if (_httpClient == null)
{
_httpClient = new HttpClient();
}
return _httpClient;
}
set { _httpClient = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref="DeliveryClient"/> class for retrieving content of the specified project.
/// </summary>
/// <param name="deliveryOptions">The settings of the Kentico Cloud project.</param>
public DeliveryClient(DeliveryOptions deliveryOptions)
{
_deliveryOptions = deliveryOptions ?? throw new ArgumentNullException(nameof(deliveryOptions), "The Delivery options object is not specified.");
if (_deliveryOptions.ProjectId == null)
{
throw new ArgumentNullException(nameof(_deliveryOptions.ProjectId), "Kentico Cloud project identifier is not specified.");
}
if (_deliveryOptions.ProjectId == string.Empty)
{
throw new ArgumentException("Kentico Cloud project identifier is not specified.", nameof(_deliveryOptions.ProjectId));
}
if (!Guid.TryParse(_deliveryOptions.ProjectId, out Guid projectIdGuid))
{
throw new ArgumentException("Provided string is not a valid project identifier ({ProjectId}). Haven't you accidentally passed the Preview API key instead of the project identifier?", nameof(_deliveryOptions.ProjectId));
}
if (_deliveryOptions.UsePreviewApi)
{
if (_deliveryOptions.PreviewApiKey == null)
{
throw new ArgumentNullException(nameof(_deliveryOptions.PreviewApiKey), "The Preview API key is not specified.");
}
if (_deliveryOptions.PreviewApiKey == string.Empty)
{
throw new ArgumentException("The Preview API key is not specified.", nameof(_deliveryOptions.PreviewApiKey));
}
}
_deliveryOptions.ProjectId = projectIdGuid.ToString("D");
}
/// <summary>
/// Initializes a new instance of the <see cref="DeliveryClient"/> class for retrieving content of the specified project.
/// </summary>
/// <param name="deliveryOptions">The settings of the Kentico Cloud project.</param>
/// <param name="contentLinkUrlResolver">An instance of an object that can resolve links in rich text elements</param>
/// <param name="contentItemsProcessor">An instance of an object that can resolve modular content in rich text elements</param>
/// <param name="codeFirstModelProvider">An instance of an object that can JSON responses into strongly typed CLR objects</param>
public DeliveryClient(IOptions<DeliveryOptions> deliveryOptions, IContentLinkUrlResolver contentLinkUrlResolver = null, IInlineContentItemsProcessor contentItemsProcessor = null, ICodeFirstModelProvider codeFirstModelProvider = null) : this(deliveryOptions.Value)
{
ContentLinkUrlResolver = contentLinkUrlResolver;
InlineContentItemsProcessor = contentItemsProcessor;
CodeFirstModelProvider = codeFirstModelProvider;
}
/// <summary>
/// Initializes a new instance of the <see cref="DeliveryClient"/> class for the published content of the specified project.
/// </summary>
/// <param name="projectId">The identifier of the Kentico Cloud project.</param>
public DeliveryClient(string projectId) : this(new DeliveryOptions { ProjectId = projectId })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DeliveryClient"/> class for the unpublished content of the specified project.
/// </summary>
/// <param name="projectId">The identifier of the Kentico Cloud project.</param>
/// <param name="previewApiKey">The Preview API key.</param>
public DeliveryClient(string projectId, string previewApiKey) : this(new DeliveryOptions { ProjectId = projectId, PreviewApiKey = previewApiKey, UsePreviewApi = true })
{
}
/// <summary>
/// Returns a content item as JSON data. By default, retrieves one level of modular content.
/// </summary>
/// <param name="codename">The codename of a content item.</param>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for projection or setting the depth of modular content.</param>
/// <returns>The <see cref="JObject"/> instance that represents the content item with the specified codename.</returns>
public async Task<JObject> GetItemJsonAsync(string codename, params string[] parameters)
{
if (codename == null)
{
throw new ArgumentNullException(nameof(codename), "The codename of a content item is not specified.");
}
if (codename == string.Empty)
{
throw new ArgumentException("The codename of a content item is not specified.", nameof(codename));
}
var endpointUrl = UrlBuilder.GetItemUrl(codename, parameters);
return await GetDeliverResponseAsync(endpointUrl);
}
/// <summary>
/// Returns content items as JSON data. By default, retrieves one level of modular content.
/// </summary>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for filtering, ordering, or setting the depth of modular content.</param>
/// <returns>The <see cref="JObject"/> instance that represents the content items. If no query parameters are specified, all content items are returned.</returns>
public async Task<JObject> GetItemsJsonAsync(params string[] parameters)
{
var endpointUrl = UrlBuilder.GetItemsUrl(parameters);
return await GetDeliverResponseAsync(endpointUrl);
}
/// <summary>
/// Returns a content item. By default, retrieves one level of modular content.
/// </summary>
/// <param name="codename">The codename of a content item.</param>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for projection or setting the depth of modular content.</param>
/// <returns>The <see cref="DeliveryItemResponse"/> instance that contains the content item with the specified codename.</returns>
public async Task<DeliveryItemResponse> GetItemAsync(string codename, params IQueryParameter[] parameters)
{
return await GetItemAsync(codename, (IEnumerable<IQueryParameter>)parameters);
}
/// <summary>
/// Gets a strongly typed content item by its codename. By default, retrieves one level of modular content.
/// </summary>
/// <typeparam name="T">Type of the code-first model. (Or <see cref="object"/> if the return type is not yet known.)</typeparam>
/// <param name="codename">The codename of a content item.</param>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for projection or setting the depth of modular content.</param>
/// <returns>The <see cref="DeliveryItemResponse{T}"/> instance that contains the content item with the specified codename.</returns>
public async Task<DeliveryItemResponse<T>> GetItemAsync<T>(string codename, params IQueryParameter[] parameters)
{
return await GetItemAsync<T>(codename, (IEnumerable<IQueryParameter>)parameters);
}
/// <summary>
/// Returns a content item. By default, retrieves one level of modular content.
/// </summary>
/// <param name="codename">The codename of a content item.</param>
/// <param name="parameters">A collection of query parameters, for example, for projection or setting the depth of modular content.</param>
/// <returns>The <see cref="DeliveryItemResponse"/> instance that contains the content item with the specified codename.</returns>
public async Task<DeliveryItemResponse> GetItemAsync(string codename, IEnumerable<IQueryParameter> parameters)
{
if (codename == null)
{
throw new ArgumentNullException(nameof(codename), "The codename of a content item is not specified.");
}
if (codename == string.Empty)
{
throw new ArgumentException("The codename of a content item is not specified.", nameof(codename));
}
var endpointUrl = UrlBuilder.GetItemUrl(codename, parameters);
var response = await GetDeliverResponseAsync(endpointUrl);
return new DeliveryItemResponse(response, this, endpointUrl);
}
/// <summary>
/// Gets a strongly typed content item by its codename. By default, retrieves one level of modular content.
/// </summary>
/// <typeparam name="T">Type of the code-first model. (Or <see cref="object"/> if the return type is not yet known.)</typeparam>
/// <param name="codename">The codename of a content item.</param>
/// <param name="parameters">A collection of query parameters, for example, for projection or setting the depth of modular content.</param>
/// <returns>The <see cref="DeliveryItemResponse{T}"/> instance that contains the content item with the specified codename.</returns>
public async Task<DeliveryItemResponse<T>> GetItemAsync<T>(string codename, IEnumerable<IQueryParameter> parameters = null)
{
if (string.IsNullOrEmpty(codename))
{
throw new ArgumentException("Entered item codename is not valid.", nameof(codename));
}
var endpointUrl = UrlBuilder.GetItemUrl(codename, parameters);
var response = await GetDeliverResponseAsync(endpointUrl);
return new DeliveryItemResponse<T>(response, this, endpointUrl);
}
/// <summary>
/// Returns content items that match the optional filtering parameters. By default, retrieves one level of modular content.
/// </summary>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for filtering, ordering, or setting the depth of modular content.</param>
/// <returns>The <see cref="DeliveryItemListingResponse"/> instance that contains the content items. If no query parameters are specified, all content items are returned.</returns>
public async Task<DeliveryItemListingResponse> GetItemsAsync(params IQueryParameter[] parameters)
{
return await GetItemsAsync((IEnumerable<IQueryParameter>)parameters);
}
/// <summary>
/// Returns content items that match the optional filtering parameters. By default, retrieves one level of modular content.
/// </summary>
/// <param name="parameters">A collection of query parameters, for example, for filtering, ordering, or setting the depth of modular content.</param>
/// <returns>The <see cref="DeliveryItemListingResponse"/> instance that contains the content items. If no query parameters are specified, all content items are returned.</returns>
public async Task<DeliveryItemListingResponse> GetItemsAsync(IEnumerable<IQueryParameter> parameters)
{
var endpointUrl = UrlBuilder.GetItemsUrl(parameters);
var response = await GetDeliverResponseAsync(endpointUrl);
return new DeliveryItemListingResponse(response, this, endpointUrl);
}
/// <summary>
/// Returns strongly typed content items that match the optional filtering parameters. By default, retrieves one level of modular content.
/// </summary>
/// <typeparam name="T">Type of the code-first model. (Or <see cref="object"/> if the return type is not yet known.)</typeparam>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for filtering, ordering, or setting the depth of modular content.</param>
/// <returns>The <see cref="DeliveryItemListingResponse{T}"/> instance that contains the content items. If no query parameters are specified, all content items are returned.</returns>
public async Task<DeliveryItemListingResponse<T>> GetItemsAsync<T>(params IQueryParameter[] parameters)
{
return await GetItemsAsync<T>((IEnumerable<IQueryParameter>)parameters);
}
/// <summary>
/// Returns strongly typed content items that match the optional filtering parameters. By default, retrieves one level of modular content.
/// </summary>
/// <typeparam name="T">Type of the code-first model. (Or <see cref="object"/> if the return type is not yet known.)</typeparam>
/// <param name="parameters">A collection of query parameters, for example, for filtering, ordering, or setting the depth of modular content.</param>
/// <returns>The <see cref="DeliveryItemListingResponse{T}"/> instance that contains the content items. If no query parameters are specified, all content items are returned.</returns>
public async Task<DeliveryItemListingResponse<T>> GetItemsAsync<T>(IEnumerable<IQueryParameter> parameters)
{
var endpointUrl = UrlBuilder.GetItemsUrl(parameters);
var response = await GetDeliverResponseAsync(endpointUrl);
return new DeliveryItemListingResponse<T>(response, this, endpointUrl);
}
/// <summary>
/// Returns a content type as JSON data.
/// </summary>
/// <param name="codename">The codename of a content type.</param>
/// <returns>The <see cref="JObject"/> instance that represents the content type with the specified codename.</returns>
public async Task<JObject> GetTypeJsonAsync(string codename)
{
if (codename == null)
{
throw new ArgumentNullException(nameof(codename), "The codename of a content type is not specified.");
}
if (codename == string.Empty)
{
throw new ArgumentException("The codename of a content type is not specified.", nameof(codename));
}
var endpointUrl = UrlBuilder.GetTypeUrl(codename);
return await GetDeliverResponseAsync(endpointUrl);
}
/// <summary>
/// Returns content types as JSON data.
/// </summary>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for paging.</param>
/// <returns>The <see cref="JObject"/> instance that represents the content types. If no query parameters are specified, all content types are returned.</returns>
public async Task<JObject> GetTypesJsonAsync(params string[] parameters)
{
var endpointUrl = UrlBuilder.GetTypesUrl(parameters);
return await GetDeliverResponseAsync(endpointUrl);
}
/// <summary>
/// Returns a content type.
/// </summary>
/// <param name="codename">The codename of a content type.</param>
/// <returns>The content type with the specified codename.</returns>
public async Task<ContentType> GetTypeAsync(string codename)
{
if (codename == null)
{
throw new ArgumentNullException(nameof(codename), "The codename of a content type is not specified.");
}
if (codename == string.Empty)
{
throw new ArgumentException("The codename of a content type is not specified.", nameof(codename));
}
var endpointUrl = UrlBuilder.GetTypeUrl(codename);
var response = await GetDeliverResponseAsync(endpointUrl);
return new ContentType(response);
}
/// <summary>
/// Returns content types.
/// </summary>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for paging.</param>
/// <returns>The <see cref="DeliveryTypeListingResponse"/> instance that represents the content types. If no query parameters are specified, all content types are returned.</returns>
public async Task<DeliveryTypeListingResponse> GetTypesAsync(params IQueryParameter[] parameters)
{
return await GetTypesAsync((IEnumerable<IQueryParameter>)parameters);
}
/// <summary>
/// Returns content types.
/// </summary>
/// <param name="parameters">A collection of query parameters, for example, for paging.</param>
/// <returns>The <see cref="DeliveryTypeListingResponse"/> instance that represents the content types. If no query parameters are specified, all content types are returned.</returns>
public async Task<DeliveryTypeListingResponse> GetTypesAsync(IEnumerable<IQueryParameter> parameters)
{
var endpointUrl = UrlBuilder.GetTypesUrl(parameters);
var response = await GetDeliverResponseAsync(endpointUrl);
return new DeliveryTypeListingResponse(response, endpointUrl);
}
/// <summary>
/// Returns a content element.
/// </summary>
/// <param name="contentTypeCodename">The codename of the content type.</param>
/// <param name="contentElementCodename">The codename of the content element.</param>
/// <returns>A content element with the specified codename that is a part of a content type with the specified codename.</returns>
public async Task<ContentElement> GetContentElementAsync(string contentTypeCodename, string contentElementCodename)
{
if (contentTypeCodename == null)
{
throw new ArgumentNullException(nameof(contentTypeCodename), "The codename of a content type is not specified.");
}
if (contentTypeCodename == string.Empty)
{
throw new ArgumentException("The codename of a content type is not specified.", nameof(contentTypeCodename));
}
if (contentElementCodename == null)
{
throw new ArgumentNullException(nameof(contentElementCodename), "The codename of a content element is not specified.");
}
if (contentElementCodename == string.Empty)
{
throw new ArgumentException("The codename of a content element is not specified.", nameof(contentElementCodename));
}
var endpointUrl = UrlBuilder.GetContentElementUrl(contentTypeCodename, contentElementCodename);
var response = await GetDeliverResponseAsync(endpointUrl);
var elementCodename = response["codename"].ToString();
return new ContentElement(response, elementCodename);
}
/// <summary>
/// Returns a taxonomy group as JSON data.
/// </summary>
/// <param name="codename">The codename of a taxonomy group.</param>
/// <returns>The <see cref="JObject"/> instance that represents the taxonomy group with the specified codename.</returns>
public async Task<JObject> GetTaxonomyJsonAsync(string codename)
{
if (codename == null)
{
throw new ArgumentNullException(nameof(codename), "The codename of a taxonomy group is not specified.");
}
if (codename == string.Empty)
{
throw new ArgumentException("The codename of a taxonomy group is not specified.", nameof(codename));
}
var endpointUrl = UrlBuilder.GetTaxonomyUrl(codename);
return await GetDeliverResponseAsync(endpointUrl);
}
/// <summary>
/// Returns taxonomy groups as JSON data.
/// </summary>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for paging.</param>
/// <returns>The <see cref="JObject"/> instance that represents the taxonomy groups. If no query parameters are specified, all taxonomy groups are returned.</returns>
public async Task<JObject> GetTaxonomiesJsonAsync(params string[] parameters)
{
var endpointUrl = UrlBuilder.GetTaxonomiesUrl(parameters);
return await GetDeliverResponseAsync(endpointUrl);
}
/// <summary>
/// Returns a taxonomy group.
/// </summary>
/// <param name="codename">The codename of a taxonomy group.</param>
/// <returns>The taxonomy group with the specified codename.</returns>
public async Task<TaxonomyGroup> GetTaxonomyAsync(string codename)
{
if (codename == null)
{
throw new ArgumentNullException(nameof(codename), "The codename of a taxonomy group is not specified.");
}
if (codename == string.Empty)
{
throw new ArgumentException("The codename of a taxonomy group is not specified.", nameof(codename));
}
var endpointUrl = UrlBuilder.GetTaxonomyUrl(codename);
var response = await GetDeliverResponseAsync(endpointUrl);
return new TaxonomyGroup(response);
}
/// <summary>
/// Returns taxonomy groups.
/// </summary>
/// <param name="parameters">An array that contains zero or more query parameters, for example, for paging.</param>
/// <returns>The <see cref="DeliveryTaxonomyListingResponse"/> instance that represents the taxonomy groups. If no query parameters are specified, all taxonomy groups are returned.</returns>
public async Task<DeliveryTaxonomyListingResponse> GetTaxonomiesAsync(params IQueryParameter[] parameters)
{
return await GetTaxonomiesAsync((IEnumerable<IQueryParameter>)parameters);
}
/// <summary>
/// Returns taxonomy groups.
/// </summary>
/// <param name="parameters">A collection of query parameters, for example, for paging.</param>
/// <returns>The <see cref="DeliveryTaxonomyListingResponse"/> instance that represents the taxonomy groups. If no query parameters are specified, all taxonomy groups are returned.</returns>
public async Task<DeliveryTaxonomyListingResponse> GetTaxonomiesAsync(IEnumerable<IQueryParameter> parameters)
{
var endpointUrl = UrlBuilder.GetTaxonomiesUrl(parameters);
var response = await GetDeliverResponseAsync(endpointUrl);
return new DeliveryTaxonomyListingResponse(response, endpointUrl);
}
private async Task<JObject> GetDeliverResponseAsync(string endpointUrl)
{
var message = new HttpRequestMessage(HttpMethod.Get, endpointUrl);
if (_deliveryOptions.WaitForLoadingNewContent)
{
message.Headers.Add("X-KC-Wait-For-Loading-New-Content", "true");
}
if (_deliveryOptions.UsePreviewApi && !string.IsNullOrEmpty(_deliveryOptions.PreviewApiKey))
{
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _deliveryOptions.PreviewApiKey);
}
var response = await HttpClient.SendAsync(message);
if (response.StatusCode == HttpStatusCode.OK)
{
var content = await response.Content.ReadAsStringAsync();
return JObject.Parse(content);
}
throw new DeliveryException(response.StatusCode, await response.Content.ReadAsStringAsync());
}
}
}