-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathHelixApi.cs
572 lines (465 loc) · 16.3 KB
/
HelixApi.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
namespace Microsoft.DotNet.Helix.Client
{
public partial interface IHelixApi
{
HelixApiOptions Options { get; set; }
IAggregate Aggregate { get; }
IAnalysis Analysis { get; }
IInformation Information { get; }
IJob Job { get; }
ILogSearch LogSearch { get; }
IMachine Machine { get; }
IScaleSets ScaleSets { get; }
IStorage Storage { get; }
ITelemetry Telemetry { get; }
IWorkItem WorkItem { get; }
}
public partial interface IServiceOperations<T>
{
T Client { get; }
}
public partial class HelixApiOptions : ClientOptions
{
public HelixApiOptions()
: this(new Uri("https://helix.dot.net/"))
{
}
public HelixApiOptions(Uri baseUri)
: this(baseUri, null)
{
}
public HelixApiOptions(TokenCredential credentials)
: this(new Uri("https://helix.dot.net/"), credentials)
{
}
public HelixApiOptions(Uri baseUri, TokenCredential credentials)
{
BaseUri = baseUri;
Credentials = credentials;
InitializeOptions();
}
partial void InitializeOptions();
/// <summary>
/// The base URI of the service.
/// </summary>
public Uri BaseUri { get; }
/// <summary>
/// Credentials to authenticate requests.
/// </summary>
public TokenCredential Credentials { get; }
}
internal partial class HelixApiResponseClassifier : ResponseClassifier
{
}
public partial class HelixApi : IHelixApi
{
private HelixApiOptions _options = null;
public HelixApiOptions Options
{
get => _options;
set
{
_options = value;
Pipeline = CreatePipeline(value);
}
}
private static HttpPipeline CreatePipeline(HelixApiOptions options)
{
return HttpPipelineBuilder.Build(options, Array.Empty<HttpPipelinePolicy>(), Array.Empty<HttpPipelinePolicy>(), new HttpPipelineTransportOptions() { IsClientRedirectEnabled = true }, new HelixApiResponseClassifier());
}
public HttpPipeline Pipeline
{
get;
private set;
}
public JsonSerializerSettings SerializerSettings { get; }
public IAggregate Aggregate { get; }
public IAnalysis Analysis { get; }
public IInformation Information { get; }
public IJob Job { get; }
public ILogSearch LogSearch { get; }
public IMachine Machine { get; }
public IScaleSets ScaleSets { get; }
public IStorage Storage { get; }
public ITelemetry Telemetry { get; }
public IWorkItem WorkItem { get; }
public HelixApi()
:this(new HelixApiOptions())
{
}
public HelixApi(HelixApiOptions options)
{
Options = options;
Aggregate = new Aggregate(this);
Analysis = new Analysis(this);
Information = new Information(this);
Job = new Job(this);
LogSearch = new LogSearch(this);
Machine = new Machine(this);
ScaleSets = new ScaleSets(this);
Storage = new Storage(this);
Telemetry = new Telemetry(this);
WorkItem = new WorkItem(this);
SerializerSettings = new JsonSerializerSettings
{
Converters =
{
new StringEnumConverter()
},
NullValueHandling = NullValueHandling.Ignore,
};
Init();
}
/// <summary>
/// Optional initialization defined outside of auto-gen code
/// </summary>
partial void Init();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void OnFailedRequest(RestApiException ex)
{
HandleFailedRequest(ex);
}
partial void HandleFailedRequest(RestApiException ex);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Serialize(string value)
{
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Serialize(bool value)
{
return value ? "true" : "false";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Serialize(int value)
{
return value.ToString(CultureInfo.InvariantCulture);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Serialize(long value)
{
return value.ToString(CultureInfo.InvariantCulture);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Serialize(float value)
{
return value.ToString(CultureInfo.InvariantCulture);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Serialize(double value)
{
return value.ToString(CultureInfo.InvariantCulture);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Serialize(Guid value)
{
return value.ToString("D", CultureInfo.InvariantCulture);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Serialize<T>(T value)
{
string result = JsonConvert.SerializeObject(value, SerializerSettings);
if (value is Enum)
{
return result.Substring(1, result.Length - 2);
}
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Deserialize<T>(string value)
{
if (typeof(T) == typeof(string))
{
return (T)(object)value;
}
return JsonConvert.DeserializeObject<T>(value, SerializerSettings);
}
public virtual ValueTask<Response> SendAsync(Request request, CancellationToken cancellationToken)
{
return Pipeline.SendRequestAsync(request, cancellationToken);
}
}
public class AllPropertiesContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(
MemberInfo member,
MemberSerialization memberSerialization)
{
var prop = base.CreateProperty(member, memberSerialization);
if (!prop.Writable)
{
var property = member as PropertyInfo;
if (property != null)
{
var hasPrivateSetter = property.GetSetMethod(true) != null;
prop.Writable = hasPrivateSetter;
}
}
return prop;
}
}
public partial class RequestWrapper
{
public RequestWrapper(Request request)
{
Uri = request.Uri.ToUri();
Method = request.Method;
Headers = request.Headers.ToDictionary(h => h.Name, h => h.Value);
}
public Uri Uri { get; }
public RequestMethod Method { get; }
public IReadOnlyDictionary<string, string> Headers { get; }
}
public partial class ResponseWrapper
{
public ResponseWrapper(Response response, string responseContent)
{
Status = response.Status;
ReasonPhrase = response.ReasonPhrase;
Headers = response.Headers;
Content = responseContent;
}
public string Content { get; }
public ResponseHeaders Headers { get; }
public string ReasonPhrase { get; }
public int Status { get; }
}
[Serializable]
public partial class RestApiException : Exception
{
private static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new AllPropertiesContractResolver(),
};
private static string FormatMessage(Response response, string responseContent)
{
var result = $"The response contained an invalid status code {response.Status} {response.ReasonPhrase}";
if (responseContent != null)
{
result += "\n\nBody: ";
result += responseContent.Length < 300 ? responseContent : responseContent.Substring(0, 300);
}
return result;
}
public RequestWrapper Request { get; }
public ResponseWrapper Response { get; }
public RestApiException(Request request, Response response, string responseContent)
: base(FormatMessage(response, responseContent))
{
Request = new RequestWrapper(request);
Response = new ResponseWrapper(response, responseContent);
}
#if NET
[Obsolete]
#endif
protected RestApiException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
var requestString = info.GetString("Request");
var responseString = info.GetString("Response");
Request = JsonConvert.DeserializeObject<RequestWrapper>(requestString, SerializerSettings);
Response = JsonConvert.DeserializeObject<ResponseWrapper>(responseString, SerializerSettings);
}
#if NET
[Obsolete]
#endif
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}
var requestString = JsonConvert.SerializeObject(Request, SerializerSettings);
var responseString = JsonConvert.SerializeObject(Response, SerializerSettings);
info.AddValue("Request", requestString);
info.AddValue("Response", responseString);
base.GetObjectData(info, context);
}
}
[Serializable]
public partial class RestApiException<T> : RestApiException
{
public T Body { get; }
public RestApiException(Request request, Response response, string responseContent, T body)
: base(request, response, responseContent)
{
Body = body;
}
#if NET
[Obsolete]
#endif
protected RestApiException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Body = JsonConvert.DeserializeObject<T>(info.GetString("Body"));
}
#if NET
[Obsolete]
#endif
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}
info.AddValue("Body", JsonConvert.SerializeObject(Body));
base.GetObjectData(info, context);
}
}
public partial class QueryBuilder : List<KeyValuePair<string, string>>
{
public QueryBuilder()
{
}
public QueryBuilder(IEnumerable<KeyValuePair<string, string>> parameters)
:base(parameters)
{
}
public void Add(string key, IEnumerable<string> values)
{
foreach (string str in values)
Add(new KeyValuePair<string, string>(key, str));
}
public void Add(string key, string value)
{
Add(new KeyValuePair<string, string>(key, value));
}
public override string ToString()
{
var builder = new StringBuilder();
for (int index = 0; index < Count; ++index)
{
KeyValuePair<string, string> keyValuePair = this[index];
if (index != 0)
{
builder.Append("&");
}
builder.Append(UrlEncoder.Default.Encode(keyValuePair.Key));
builder.Append("=");
builder.Append(UrlEncoder.Default.Encode(keyValuePair.Value));
}
return builder.ToString();
}
}
public class ResponseStream : Stream
{
private readonly Stream _inner;
private readonly Response _response;
public ResponseStream(Stream inner, Response response)
{
_inner = inner;
_response = response;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_inner.Dispose();
_response.Dispose();
}
}
#region Forwarding Members
public override void Flush()
{
_inner.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return _inner.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _inner.Seek(offset, origin);
}
public override void SetLength(long value)
{
_inner.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
_inner.Write(buffer, offset, count);
}
public override string ToString()
{
return _inner.ToString();
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return _inner.BeginRead(buffer, offset, count, callback, state);
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return _inner.BeginWrite(buffer, offset, count, callback, state);
}
public override void Close()
{
_inner.Close();
}
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
return _inner.CopyToAsync(destination, bufferSize, cancellationToken);
}
public override int EndRead(IAsyncResult asyncResult)
{
return _inner.EndRead(asyncResult);
}
public override void EndWrite(IAsyncResult asyncResult)
{
_inner.EndWrite(asyncResult);
}
public override Task FlushAsync(CancellationToken cancellationToken)
{
return _inner.FlushAsync(cancellationToken);
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return _inner.ReadAsync(buffer, offset, count, cancellationToken);
}
public override int ReadByte()
{
return _inner.ReadByte();
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return _inner.WriteAsync(buffer, offset, count, cancellationToken);
}
public override void WriteByte(byte value)
{
_inner.WriteByte(value);
}
public override bool CanRead => _inner.CanRead;
public override bool CanSeek => _inner.CanSeek;
public override bool CanWrite => _inner.CanWrite;
public override long Length => _inner.Length;
public override long Position
{
get => _inner.Position;
set => _inner.Position = value;
}
public override bool CanTimeout => _inner.CanTimeout;
public override int ReadTimeout { get => _inner.ReadTimeout; set => _inner.ReadTimeout = value; }
public override int WriteTimeout { get => _inner.WriteTimeout; set => _inner.WriteTimeout = value; }
#endregion
}
}