/
Headers.cs
461 lines (401 loc) · 15.8 KB
/
Headers.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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Collections;
/// <summary>
/// Header implementation used for Request and Responses
/// </summary>
/// <seealso cref="ResponseMessage"/>
/// <seealso cref="RequestMessage"/>
public class Headers : IEnumerable
{
internal static readonly string SDKSUPPORTEDCAPABILITIES = SDKSupportedCapabilitiesHelpers.GetSDKSupportedCapabilities().ToString(
CultureInfo.InvariantCulture);
internal virtual SubStatusCodes SubStatusCode
{
get => Headers.GetSubStatusCodes(this.SubStatusCodeLiteral);
set => this.SubStatusCodeLiteral = ((uint)value).ToString(CultureInfo.InvariantCulture);
}
/// <summary>
/// Gets the Continuation Token in the current <see cref="ResponseMessage"/>.
/// </summary>
public virtual string ContinuationToken
{
get => this.CosmosMessageHeaders.Continuation;
internal set => this.CosmosMessageHeaders.Continuation = value;
}
/// <summary>
/// Gets or Set the CoorelatedActivityId in the current <see cref="ResponseMessage"/>.
/// </summary>
internal virtual string CorrelatedActivityId
{
get => this.CosmosMessageHeaders.Get(HttpConstants.HttpHeaders.CorrelatedActivityId);
set => this.CosmosMessageHeaders.Set(HttpConstants.HttpHeaders.CorrelatedActivityId, value);
}
/// <summary>
/// Gets the request charge for this request from the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The request charge measured in request units.
/// </value>
public virtual double RequestCharge
{
get
{
string value = this.CosmosMessageHeaders.RequestCharge;
if (value == null)
{
return 0;
}
return double.Parse(value, CultureInfo.InvariantCulture);
}
internal set => this.CosmosMessageHeaders.RequestCharge = value.ToString(CultureInfo.InvariantCulture);
}
/// <summary>
/// Gets the activity ID for the request from the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The activity ID for the request.
/// </value>
public virtual string ActivityId
{
get => this.CosmosMessageHeaders.ActivityId;
internal set => this.CosmosMessageHeaders.ActivityId = value;
}
/// <summary>
/// Gets the entity tag associated with the resource from the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The entity tag associated with the resource.
/// </value>
/// <remarks>
/// ETags are used for concurrency checking when updating resources.
/// </remarks>
public virtual string ETag
{
get => this.CosmosMessageHeaders.ETag;
internal set => this.CosmosMessageHeaders.ETag = value;
}
/// <summary>
/// Gets the Content Type for the current content in the <see cref="ResponseMessage"/>.
/// </summary>
public virtual string ContentType
{
get => this.CosmosMessageHeaders.ContentType;
internal set => this.CosmosMessageHeaders.ContentType = value;
}
/// <summary>
/// Gets the Session Token for the current <see cref="ResponseMessage"/>.
/// </summary>
/// <remarks>
/// Session Token is used along with Session Consistency.
/// </remarks>
public virtual string Session
{
get => this.CosmosMessageHeaders.SessionToken;
internal set => this.CosmosMessageHeaders.SessionToken = value;
}
/// <summary>
/// Gets the Content Length for the current content in the <see cref="ResponseMessage"/>.
/// </summary>
public virtual string ContentLength
{
get => this.CosmosMessageHeaders.ContentLength;
set => this.CosmosMessageHeaders.ContentLength = value;
}
/// <summary>
/// Gets the Location for the current content in the <see cref="ResponseMessage"/>.
/// </summary>
public virtual string Location
{
get => this.CosmosMessageHeaders.Location;
internal set => this.CosmosMessageHeaders.Location = value;
}
internal virtual string SubStatusCodeLiteral
{
get => this.CosmosMessageHeaders.SubStatus;
set => this.CosmosMessageHeaders.SubStatus = value;
}
internal TimeSpan? RetryAfter
{
get => Headers.GetRetryAfter(this.RetryAfterLiteral);
set
{
if (value.HasValue)
{
this.RetryAfterLiteral = value.Value.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
return;
}
this.RetryAfterLiteral = null;
}
}
internal virtual string Authorization
{
get => this.CosmosMessageHeaders.Authorization;
set => this.CosmosMessageHeaders.Authorization = value;
}
internal virtual string RetryAfterLiteral
{
get => this.CosmosMessageHeaders.RetryAfterInMilliseconds;
set => this.CosmosMessageHeaders.RetryAfterInMilliseconds = value;
}
internal virtual string PartitionKey
{
get => this.CosmosMessageHeaders.PartitionKey;
set => this.CosmosMessageHeaders.PartitionKey = value;
}
internal virtual string OptimisticDirectExecute
{
get => this.CosmosMessageHeaders.OptimisticDirectExecute;
set => this.CosmosMessageHeaders.OptimisticDirectExecute = value;
}
internal virtual string RequiresDistribution
{
get => this.CosmosMessageHeaders.RequiresDistribution;
set => this.CosmosMessageHeaders.RequiresDistribution = value;
}
internal virtual string PartitionKeyRangeId
{
get => this.CosmosMessageHeaders.PartitionKeyRangeId;
set => this.CosmosMessageHeaders.PartitionKeyRangeId = value;
}
internal virtual string IsUpsert
{
get => this.CosmosMessageHeaders.IsUpsert;
set => this.CosmosMessageHeaders.IsUpsert = value;
}
internal virtual string OfferThroughput
{
get => this.CosmosMessageHeaders.OfferThroughput;
set => this.CosmosMessageHeaders.OfferThroughput = value;
}
internal virtual string IfNoneMatch
{
get => this.CosmosMessageHeaders.IfNoneMatch;
set => this.CosmosMessageHeaders.IfNoneMatch = value;
}
internal virtual string PageSize
{
get => this.CosmosMessageHeaders.PageSize;
set => this.CosmosMessageHeaders.PageSize = value;
}
internal virtual string QueryMetricsText
{
get => this.CosmosMessageHeaders.QueryMetrics;
set => this.CosmosMessageHeaders.QueryMetrics = value;
}
internal virtual string IndexUtilizationText
{
get => this.CosmosMessageHeaders.IndexUtilization;
set => this.CosmosMessageHeaders.IndexUtilization = value;
}
internal virtual string BackendRequestDurationMilliseconds
{
get => this.CosmosMessageHeaders.BackendRequestDurationMilliseconds;
set => this.CosmosMessageHeaders.BackendRequestDurationMilliseconds = value;
}
internal virtual string ConsistencyLevel
{
get => this.CosmosMessageHeaders.ConsistencyLevel;
set => this.CosmosMessageHeaders.ConsistencyLevel = value;
}
internal virtual string SDKSupportedCapabilities
{
get => this.CosmosMessageHeaders.SDKSupportedCapabilities;
set => this.CosmosMessageHeaders.SDKSupportedCapabilities = value;
}
internal virtual string ContentSerializationFormat
{
get => this.CosmosMessageHeaders.ContentSerializationFormat;
set => this.CosmosMessageHeaders.ContentSerializationFormat = value;
}
internal virtual string ReadFeedKeyType
{
get => this.CosmosMessageHeaders.ReadFeedKeyType;
set => this.CosmosMessageHeaders.ReadFeedKeyType = value;
}
internal virtual string StartEpk
{
get => this.CosmosMessageHeaders.StartEpk;
set => this.CosmosMessageHeaders.StartEpk = value;
}
internal virtual string EndEpk
{
get => this.CosmosMessageHeaders.EndEpk;
set => this.CosmosMessageHeaders.EndEpk = value;
}
internal virtual string ItemCount => this.CosmosMessageHeaders.Get(HttpConstants.HttpHeaders.ItemCount);
/// <summary>
/// Creates a new instance of <see cref="Headers"/>.
/// </summary>
public Headers()
{
this.CosmosMessageHeaders = new StoreRequestHeaders();
}
internal Headers(INameValueCollection nameValueCollection)
{
this.CosmosMessageHeaders = nameValueCollection switch
{
StoreResponseNameValueCollection storeResponseNameValueCollection => new StoreResponseHeaders(storeResponseNameValueCollection),
HttpResponseHeadersWrapper httpResponseHeaders => httpResponseHeaders,
_ => new NameValueResponseHeaders(nameValueCollection),
};
}
/// <summary>
/// Gets the value of a particular header.
/// </summary>
/// <param name="headerName">Header name to look for.</param>
/// <returns>The header value.</returns>
public virtual string this[string headerName]
{
get => this.CosmosMessageHeaders[headerName];
set => this.CosmosMessageHeaders[headerName] = value;
}
/// <summary>
/// Enumerates all the HTTP headers names in the <see cref="Headers"/>.
/// </summary>
/// <returns>An enumerator for all headers.</returns>
public virtual IEnumerator<string> GetEnumerator()
{
foreach (string key in this.CosmosMessageHeaders.AllKeys())
{
yield return key;
}
}
/// <summary>
/// Adds a header to the Header collection.
/// </summary>
/// <param name="headerName">Header name.</param>
/// <param name="value">Header value.</param>
public virtual void Add(string headerName, string value)
{
this.CosmosMessageHeaders.Add(headerName, value);
}
/// <summary>
/// Adds a header to the Header collection.
/// </summary>
/// <param name="headerName">Header name.</param>
/// <param name="values">List of values to be added as a comma-separated list.</param>
public virtual void Add(string headerName, IEnumerable<string> values)
{
this.CosmosMessageHeaders.Add(headerName, values);
}
/// <summary>
/// Adds or updates a header in the Header collection.
/// </summary>
/// <param name="headerName">Header name.</param>
/// <param name="value">Header value.</param>
public virtual void Set(string headerName, string value)
{
this.CosmosMessageHeaders.Set(headerName, value);
}
/// <summary>
/// Gets the value of a particular header.
/// </summary>
/// <param name="headerName">Header name.</param>
/// <returns>The header value.</returns>
public virtual string Get(string headerName)
{
return this.CosmosMessageHeaders.Get(headerName);
}
/// <summary>
/// Tries to get the value for a particular header.
/// </summary>
/// <param name="headerName">Header name.</param>
/// <param name="value">Header value.</param>
/// <returns>True or false if the header name existed in the header collection.</returns>
public virtual bool TryGetValue(string headerName, out string value)
{
return this.CosmosMessageHeaders.TryGetValue(headerName, out value);
}
/// <summary>
/// Returns the header value or the default(string)
/// </summary>
/// <param name="headerName">Header Name</param>
/// <returns>Returns the header value or the default(string).</returns>
public virtual string GetValueOrDefault(string headerName)
{
if (this.TryGetValue(headerName, out string value))
{
return value;
}
return default;
}
/// <summary>
/// Removes a header from the header collection.
/// </summary>
/// <param name="headerName">Header name.</param>
public virtual void Remove(string headerName)
{
this.CosmosMessageHeaders.Remove(headerName);
}
/// <summary>
/// Obtains a list of all header names.
/// </summary>
/// <returns>An array with all the header names.</returns>
public virtual string[] AllKeys()
{
return this.CosmosMessageHeaders.AllKeys();
}
/// <summary>
/// Gets a header value with a particular type.
/// </summary>
/// <typeparam name="T">Type of the header value.</typeparam>
/// <param name="headerName">Header name.</param>
/// <returns>The header value parsed for a particular type.</returns>
public virtual T GetHeaderValue<T>(string headerName)
{
return this.CosmosMessageHeaders.GetHeaderValue<T>(headerName);
}
/// <summary>
/// Enumerates all the HTTP headers names in the <see cref="Headers"/>.
/// </summary>
/// <returns>An enumerator for all headers.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
internal string[] GetValues(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
string value = this[key];
if (value == null)
{
return null;
}
return new string[1] { this[key] };
}
internal CosmosMessageHeadersInternal CosmosMessageHeaders { get; }
internal static int GetIntValueOrDefault(string value)
{
int.TryParse(value, out int number);
return number;
}
internal static SubStatusCodes GetSubStatusCodes(string value)
{
if (uint.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint nSubStatus))
{
return (SubStatusCodes)nSubStatus;
}
return SubStatusCodes.Unknown;
}
internal static TimeSpan? GetRetryAfter(string value)
{
if (long.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out long retryIntervalInMilliseconds))
{
return TimeSpan.FromMilliseconds(retryIntervalInMilliseconds);
}
return null;
}
}
}