1
+ namespace Skyline . DataMiner . Sdk . CatalogService
2
+ {
3
+ using System ;
4
+ using System . IO ;
5
+ using System . Net ;
6
+ using System . Net . Http ;
7
+ using System . Net . Http . Headers ;
8
+ using System . Security . Authentication ;
9
+ using System . Threading ;
10
+ using System . Threading . Tasks ;
11
+
12
+ using Newtonsoft . Json ;
13
+
14
+ internal sealed class HttpCatalogService : ICatalogService , IDisposable
15
+ {
16
+ /// <summary>
17
+ /// Artifact information returned from uploading an artifact to the catalog using the non-volatile upload.
18
+ /// </summary>
19
+ private sealed class CatalogUploadResult
20
+ {
21
+ [ JsonProperty ( "catalogId" ) ]
22
+ public string CatalogId { get ; set ; }
23
+
24
+ [ JsonProperty ( "catalogVersionNumber" ) ]
25
+ public string CatalogVersionNumber { get ; set ; }
26
+
27
+ [ JsonProperty ( "azureStorageId" ) ]
28
+ public string AzureStorageId { get ; set ; }
29
+ }
30
+
31
+
32
+ private const string RegistrationPath = "api/key-catalog/v2-0/catalogs/register" ;
33
+ private const string VersionUploadPathEnd = "/register/version" ;
34
+ private const string VersionUploadPathStart = "api/key-catalog/v2-0/catalogs/" ;
35
+ private readonly HttpClient _httpClient ;
36
+
37
+ public HttpCatalogService ( HttpClient httpClient )
38
+ {
39
+ _httpClient = httpClient ;
40
+ }
41
+
42
+ public void Dispose ( )
43
+ {
44
+ _httpClient . Dispose ( ) ;
45
+ }
46
+
47
+ public async Task < ArtifactUploadResult > RegisterCatalogAsync ( byte [ ] catalogDetailsZip , string key , CancellationToken cancellationToken )
48
+ {
49
+ using ( var formData = new MultipartFormDataContent ( ) )
50
+ {
51
+ formData . Headers . Add ( "Ocp-Apim-Subscription-Key" , key ) ;
52
+
53
+ // Add file
54
+ using ( MemoryStream ms = new MemoryStream ( catalogDetailsZip ) )
55
+ {
56
+ ms . Write ( catalogDetailsZip , 0 , catalogDetailsZip . Length ) ;
57
+ ms . Position = 0 ;
58
+ formData . Add ( new StreamContent ( ms ) , "file" , "catalogDetails.zip" ) ;
59
+
60
+
61
+ // Make PUT request
62
+ var response = await _httpClient . PutAsync ( RegistrationPath , formData , cancellationToken ) . ConfigureAwait ( false ) ;
63
+
64
+ // Get the response body
65
+ var body = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
66
+
67
+ if ( response . IsSuccessStatusCode )
68
+ {
69
+ var returnedResult = JsonConvert . DeserializeObject < CatalogUploadResult > ( await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ) ;
70
+ return new ArtifactUploadResult ( ) { ArtifactId = returnedResult . AzureStorageId } ;
71
+ }
72
+
73
+ if ( response . StatusCode is HttpStatusCode . Forbidden || response . StatusCode is HttpStatusCode . Unauthorized )
74
+ {
75
+ throw new AuthenticationException ( $ "The registration api returned a { response . StatusCode } response. Body: { body } ") ;
76
+ }
77
+
78
+ throw new InvalidOperationException ( $ "The registration api returned a { response . StatusCode } response. Body: { body } ") ;
79
+ }
80
+ }
81
+ }
82
+
83
+ public async Task < ArtifactUploadResult > UploadVersionAsync ( byte [ ] package , string fileName , string key , string catalogId , string version , string description , CancellationToken cancellationToken )
84
+ {
85
+ if ( String . IsNullOrWhiteSpace ( version ) ) throw new ArgumentNullException ( nameof ( version ) ) ;
86
+
87
+ string versionUploadPath = $ "{ VersionUploadPathStart } { catalogId } { VersionUploadPathEnd } ";
88
+ using ( var formData = new MultipartFormDataContent ( ) )
89
+ {
90
+ formData . Headers . Add ( "Ocp-Apim-Subscription-Key" , key ) ;
91
+
92
+ // Add the package (zip file) to the form data
93
+ using ( MemoryStream ms = new MemoryStream ( package ) )
94
+ {
95
+ ms . Position = 0 ; // Reset the stream position after writing
96
+
97
+ // Set up StreamContent with correct headers for the file
98
+ var fileContent = new StreamContent ( ms ) ;
99
+
100
+ fileContent . Headers . ContentDisposition = new ContentDispositionHeaderValue ( "form-data" )
101
+ {
102
+ Name = "\" file\" " ,
103
+ FileName = "\" " + fileName + "\" "
104
+ } ;
105
+ formData . Add ( fileContent ) ;
106
+
107
+
108
+ // Add version information to the form data
109
+ formData . Add ( new StringContent ( version ) , "versionNumber" ) ;
110
+ formData . Add ( new StringContent ( description ) , "versionDescription" ) ;
111
+
112
+ // Log the info for debugging
113
+ string logInfo = $ "name { fileName } --versionNumber { version } --versionDescription { description } ";
114
+
115
+ // Make the HTTP POST request
116
+ var response = await _httpClient . PostAsync ( versionUploadPath , formData , cancellationToken ) . ConfigureAwait ( false ) ;
117
+
118
+ // Read and log the response body
119
+ var body = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
120
+
121
+ if ( response . IsSuccessStatusCode )
122
+ {
123
+ var returnedResult = JsonConvert . DeserializeObject < CatalogUploadResult > ( await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ) ;
124
+ return new ArtifactUploadResult ( ) { ArtifactId = returnedResult . AzureStorageId } ;
125
+ }
126
+
127
+ if ( response . StatusCode is HttpStatusCode . Forbidden || response . StatusCode is HttpStatusCode . Unauthorized )
128
+ {
129
+ throw new AuthenticationException ( $ "The version upload api returned a { response . StatusCode } response. Body: { body } ") ;
130
+ }
131
+
132
+ throw new InvalidOperationException ( $ "The version upload api returned a { response . StatusCode } response. Body: { body } ") ;
133
+ }
134
+ }
135
+ }
136
+ }
137
+ }
0 commit comments