-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersClient.cs
More file actions
1257 lines (1158 loc) · 56.1 KB
/
VersClient.cs
File metadata and controls
1257 lines (1158 loc) · 56.1 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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Generated by Sterling SDK Generator
// Orchestrator Control Plane API v0.1.0
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace VersSdk
{
/// <summary>Per-request options that override client defaults.</summary>
public class RequestOptions
{
/// <summary>Additional headers to send with the request.</summary>
public Dictionary<string, string?>? Headers { get; set; }
/// <summary>Request timeout. Overrides the client-level timeout.</summary>
public TimeSpan? Timeout { get; set; }
}
/// <summary>Client for Orchestrator Control Plane API</summary>
public class VersSdkClient : IDisposable
{
private static readonly HashSet<int> RetryableStatusCodes = new() { 408, 409, 429 };
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
};
private readonly HttpClient _httpClient;
private readonly string _baseUrl;
private readonly string? _apiKey;
private readonly int _maxRetries;
private readonly TimeSpan _timeout;
private readonly ILogger _logger;
private readonly bool _ownsHttpClient;
/// <summary>Create a new VersSdkClient.</summary>
/// <param name="baseUrl">Base URL of the API. Defaults to VERS_BASE_URL env var or https://api.vers.sh.</param>
/// <param name="apiKey">Bearer token for authentication. Defaults to VERS_API_KEY env var.</param>
/// <param name="maxRetries">Maximum number of retries on transient failures.</param>
/// <param name="timeout">Request timeout.</param>
/// <param name="httpClient">Optional pre-configured HttpClient instance.</param>
/// <param name="logger">Optional logger instance.</param>
public VersSdkClient(
string? baseUrl = null,
string? apiKey = null,
int maxRetries = 2,
TimeSpan? timeout = null,
HttpClient? httpClient = null,
ILogger? logger = null)
{
_baseUrl = (baseUrl ?? Environment.GetEnvironmentVariable("VERS_BASE_URL") ?? "https://api.vers.sh").TrimEnd('/');
_apiKey = apiKey ?? Environment.GetEnvironmentVariable("VERS_API_KEY");
_maxRetries = maxRetries;
_timeout = timeout ?? TimeSpan.FromSeconds(30);
_logger = logger ?? NullLogger.Instance;
if (httpClient != null)
{
_httpClient = httpClient;
_ownsHttpClient = false;
}
else
{
_httpClient = new HttpClient();
_ownsHttpClient = true;
}
var osDesc = RuntimeInformation.OSDescription;
var arch = RuntimeInformation.OSArchitecture.ToString().ToLowerInvariant();
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(
$"vers-sdk/0.1.8 csharp/{Environment.Version} {osDesc}/{arch}");
if (!string.IsNullOrEmpty(_apiKey))
{
_httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _apiKey);
}
}
private static bool IsRetryableStatus(int statusCode)
{
return statusCode >= 500 || RetryableStatusCodes.Contains(statusCode);
}
/// <summary>Convert a query parameter value to its string representation, with correct boolean casing.</summary>
private static string ToQueryString(object? value)
{
if (value is bool b) return b ? "true" : "false";
return value?.ToString() ?? "";
}
private static double RetryDelay(int attempt)
{
var baseDelay = 0.5 * Math.Pow(2, attempt);
var jitter = Random.Shared.NextDouble() * baseDelay * 0.25;
return baseDelay + jitter;
}
private static double? ParseRetryAfter(HttpResponseMessage response)
{
if (response.Headers.TryGetValues("Retry-After", out var values))
{
var headerValue = values.FirstOrDefault();
if (headerValue == null) return null;
if (double.TryParse(headerValue, NumberStyles.Float, CultureInfo.InvariantCulture, out var seconds))
{
return Math.Min(Math.Max(seconds, 0.0), 60.0);
}
if (DateTimeOffset.TryParse(headerValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
{
var delay = (date - DateTimeOffset.UtcNow).TotalSeconds;
return Math.Min(Math.Max(delay, 0.0), 60.0);
}
}
return null;
}
private void CheckResponse(HttpResponseMessage response, string? body)
{
if (response.IsSuccessStatusCode) return;
var statusCode = (int)response.StatusCode;
object? parsedBody = null;
string? message = null;
if (!string.IsNullOrEmpty(body))
{
var ct = response.Content.Headers.ContentType?.MediaType ?? "";
if (ct.Contains("application/json"))
{
try
{
parsedBody = JsonSerializer.Deserialize<Dictionary<string, object>>(body!);
}
catch
{
message = body;
}
}
else
{
message = body;
}
}
var headers = response.Headers
.Concat(response.Content.Headers)
.ToDictionary(h => h.Key, h => string.Join(", ", h.Value));
throw ApiException.Generate(statusCode, parsedBody, message, headers);
}
private async Task<HttpResponseMessage> RequestAsync(
HttpMethod method,
string path,
object? body = null,
Dictionary<string, string?>? queryParams = null,
RequestOptions? options = null,
CancellationToken cancellationToken = default)
{
_logger.LogDebug("request: {Method} {Path}", method, path);
var effectiveTimeout = options?.Timeout ?? _timeout;
Exception? lastException = null;
var retryAfterUsed = false;
for (int attempt = 0; attempt <= _maxRetries; attempt++)
{
if (attempt > 0 && !retryAfterUsed)
{
_logger.LogInformation("retry attempt {Attempt}/{MaxRetries} for {Method} {Path}",
attempt, _maxRetries, method, path);
await Task.Delay(TimeSpan.FromSeconds(RetryDelay(attempt - 1)), cancellationToken);
}
retryAfterUsed = false;
try
{
var url = _baseUrl + path;
if (queryParams != null && queryParams.Count > 0)
{
var qp = string.Join("&", queryParams
.Where(kv => kv.Value != null)
.Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value!)}"));
if (qp.Length > 0)
url += "?" + qp;
}
using var request = new HttpRequestMessage(method, url);
// Idempotency key for mutating methods
if (method == HttpMethod.Post || method == HttpMethod.Put ||
method == HttpMethod.Patch || method == HttpMethod.Delete)
{
request.Headers.Add("X-Idempotency-Key", Guid.NewGuid().ToString());
}
// Apply per-request headers
if (options?.Headers != null)
{
foreach (var (key, value) in options.Headers)
{
if (value == null)
{
request.Headers.Remove(key);
}
else
{
request.Headers.TryAddWithoutValidation(key, value);
}
}
}
if (body != null)
{
var json = JsonSerializer.Serialize(body, JsonOptions);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(effectiveTimeout);
var response = await _httpClient.SendAsync(request, cts.Token);
var responseBody = await response.Content.ReadAsStringAsync(cts.Token);
_logger.LogDebug("response: {StatusCode} for {Method} {Path}",
(int)response.StatusCode, method, path);
var statusCode = (int)response.StatusCode;
if (IsRetryableStatus(statusCode) && attempt < _maxRetries)
{
var retryAfterDelay = ParseRetryAfter(response);
if (retryAfterDelay.HasValue)
{
retryAfterUsed = true;
_logger.LogInformation(
"retry attempt {Attempt}/{MaxRetries} for {Method} {Path} (retry-after: {Delay:F1}s)",
attempt + 1, _maxRetries, method, path, retryAfterDelay.Value);
await Task.Delay(TimeSpan.FromSeconds(retryAfterDelay.Value), cancellationToken);
}
lastException = new Exception($"HTTP {statusCode}");
continue;
}
CheckResponse(response, responseBody);
// Return a new response with the body re-attached since we consumed it
var result = new HttpResponseMessage(response.StatusCode);
result.Content = new StringContent(responseBody, Encoding.UTF8, "application/json");
foreach (var header in response.Headers)
result.Headers.TryAddWithoutValidation(header.Key, header.Value);
return result;
}
catch (ApiException)
{
throw;
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
_logger.LogError("timeout: {Method} {Path}", method, path);
if (attempt == _maxRetries)
throw new ApiTimeoutException();
lastException = new ApiTimeoutException();
}
catch (HttpRequestException ex)
{
_logger.LogError("connection error: {Method} {Path} - {Error}", method, path, ex.Message);
if (attempt == _maxRetries)
throw new ApiConnectionException(cause: ex);
lastException = ex;
}
}
throw lastException ?? new VersException("Request failed after retries.");
}
// ── API Operations ──────────────────────────────────────────────────
/// <summary></summary>
public async Task<JsonElement> VmStatusAsync(string vm_id, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_id}/status";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ResizeVmDiskAsync(string vm_id, object? body = null, ResizeVmDiskParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_id}/disk";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.SkipWaitBoot != null)
qp["skip_wait_boot"] = ToQueryString(queryParams.SkipWaitBoot);
}
var response = await RequestAsync(
new HttpMethod("PATCH"),
path,
body: body,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ExecVmStreamAttachAsync(string vm_id, object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_id}/exec/stream/attach";
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ListVmsAsync(RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/vms";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> CreateNewRootVmAsync(object? body = null, CreateNewRootVmParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/vm/new_root";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.WaitBoot != null)
qp["wait_boot"] = ToQueryString(queryParams.WaitBoot);
}
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> VmLogsAsync(string vm_id, VmLogsParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_id}/logs";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.Offset != null)
qp["offset"] = ToQueryString(queryParams.Offset);
if (queryParams.MaxEntries != null)
qp["max_entries"] = ToQueryString(queryParams.MaxEntries);
if (queryParams.Stream != null)
qp["stream"] = ToQueryString(queryParams.Stream);
}
var response = await RequestAsync(
new HttpMethod("GET"),
path,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> BranchByRefAsync(string repo_name, string tag_name, object? body = null, BranchByRefParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/branch/by_ref/{repo_name}/{tag_name}";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.Count != null)
qp["count"] = ToQueryString(queryParams.Count);
}
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> LabelVmAsync(string vm_id, object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_id}/label";
var response = await RequestAsync(
new HttpMethod("PATCH"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ListPublicCommitsAsync(RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/commits/public";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> GetImageStatusAsync(string image_name, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/images/{image_name}/status";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ListPublicRepoTagsAsync(string org_name, string repo_name, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/public/repositories/{org_name}/{repo_name}/tags";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> VersionHandlerAsync(RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/system/version";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> BranchByTagAsync(string tag_name, object? body = null, BranchByTagParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/branch/by_tag/{tag_name}";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.Count != null)
qp["count"] = ToQueryString(queryParams.Count);
}
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> GetRepoTagAsync(string repo_name, string tag_name, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/repositories/{repo_name}/tags/{tag_name}";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> DeleteRepoTagAsync(string repo_name, string tag_name, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/repositories/{repo_name}/tags/{tag_name}";
var response = await RequestAsync(
new HttpMethod("DELETE"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> UpdateRepoTagAsync(string repo_name, string tag_name, object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/repositories/{repo_name}/tags/{tag_name}";
var response = await RequestAsync(
new HttpMethod("PATCH"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> BranchVmAsync(string vm_or_commit_id, object? body = null, BranchVmParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_or_commit_id}/branch";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.KeepPaused != null)
qp["keep_paused"] = ToQueryString(queryParams.KeepPaused);
if (queryParams.SkipWaitBoot != null)
qp["skip_wait_boot"] = ToQueryString(queryParams.SkipWaitBoot);
if (queryParams.Count != null)
qp["count"] = ToQueryString(queryParams.Count);
}
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> GetRepositoryAsync(string repo_name, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/repositories/{repo_name}";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> DeleteRepositoryAsync(string repo_name, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/repositories/{repo_name}";
var response = await RequestAsync(
new HttpMethod("DELETE"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ListParentCommitsAsync(string commit_id, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/commits/{commit_id}/parents";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ValidateKeyAsync(object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/keys/validate";
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ForkRepositoryAsync(object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/repositories/fork";
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ListRepositoriesAsync(RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/repositories";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> CreateRepositoryAsync(object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/repositories";
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> UpdateVmStateAsync(string vm_id, object? body = null, UpdateVmStateParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_id}/state";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.SkipWaitBoot != null)
qp["skip_wait_boot"] = ToQueryString(queryParams.SkipWaitBoot);
}
var response = await RequestAsync(
new HttpMethod("PATCH"),
path,
body: body,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> CreateImageAsync(object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/images/create";
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> RestoreFromCommitAsync(object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/vm/from_commit";
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> GetDomainAsync(string domain_id, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/domains/{domain_id}";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> DeleteDomainAsync(string domain_id, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/domains/{domain_id}";
var response = await RequestAsync(
new HttpMethod("DELETE"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ListTagsAsync(RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/commit_tags";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> CreateTagAsync(object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/commit_tags";
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ListPublicRepositoriesAsync(RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/public/repositories";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> GetPublicRepoTagAsync(string org_name, string repo_name, string tag_name, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/public/repositories/{org_name}/{repo_name}/tags/{tag_name}";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> GetVmMetadataAsync(string vm_id, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_id}/metadata";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ListEnvVarsAsync(RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/env_vars";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> SetEnvVarsAsync(object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/env_vars";
var response = await RequestAsync(
new HttpMethod("PUT"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> DeleteVmAsync(string vm_id, DeleteVmParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_id}";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.SkipWaitBoot != null)
qp["skip_wait_boot"] = ToQueryString(queryParams.SkipWaitBoot);
}
var response = await RequestAsync(
new HttpMethod("DELETE"),
path,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> ListCommitsAsync(RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = "/api/v1/commits";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> GetTagAsync(string tag_name, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/commit_tags/{tag_name}";
var response = await RequestAsync(
new HttpMethod("GET"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> DeleteTagAsync(string tag_name, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/commit_tags/{tag_name}";
var response = await RequestAsync(
new HttpMethod("DELETE"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> UpdateTagAsync(string tag_name, object? body = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/commit_tags/{tag_name}";
var response = await RequestAsync(
new HttpMethod("PATCH"),
path,
body: body,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> BranchByVmAsync(string vm_id, object? body = null, BranchByVmParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/branch/by_vm/{vm_id}";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.KeepPaused != null)
qp["keep_paused"] = ToQueryString(queryParams.KeepPaused);
if (queryParams.SkipWaitBoot != null)
qp["skip_wait_boot"] = ToQueryString(queryParams.SkipWaitBoot);
if (queryParams.Count != null)
qp["count"] = ToQueryString(queryParams.Count);
}
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> DeleteEnvVarAsync(string key, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/env_vars/{key}";
var response = await RequestAsync(
new HttpMethod("DELETE"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> DeleteImageAsync(string base_image_id, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/images/images/{base_image_id}";
var response = await RequestAsync(
new HttpMethod("DELETE"),
path,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> CommitVmAsync(string vm_id, object? body = null, CommitVmParams? queryParams = null, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/vm/{vm_id}/commit";
Dictionary<string, string?>? qp = null;
if (queryParams != null)
{
qp = new Dictionary<string, string?>();
if (queryParams.KeepPaused != null)
qp["keep_paused"] = ToQueryString(queryParams.KeepPaused);
if (queryParams.SkipWaitBoot != null)
qp["skip_wait_boot"] = ToQueryString(queryParams.SkipWaitBoot);
}
var response = await RequestAsync(
new HttpMethod("POST"),
path,
body: body,
queryParams: qp,
options: options,
cancellationToken: cancellationToken);
var json = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonSerializer.Deserialize<JsonElement>(json);
}
/// <summary></summary>
public async Task<JsonElement> DeleteCommitAsync(string commit_id, RequestOptions? options = null, CancellationToken cancellationToken = default)
{
var path = $"/api/v1/commits/{commit_id}";
var response = await RequestAsync(
new HttpMethod("DELETE"),
path,
options: options,
cancellationToken: cancellationToken);