@@ -18,11 +18,14 @@ namespace Fossology.Rest.Dotnet
18
18
using System . Collections . Generic ;
19
19
using System . Linq ;
20
20
using System . Net ;
21
+ using System . Runtime . InteropServices . ComTypes ;
22
+ using System . Threading . Tasks ;
21
23
using Fossology . Rest . Dotnet . Model ;
22
24
23
25
using Newtonsoft . Json ;
24
26
25
27
using RestSharp ;
28
+ using RestSharp . Extensions ;
26
29
27
30
/// <summary>
28
31
/// Client for the SW360 REST API.
@@ -41,9 +44,14 @@ public partial class FossologyClient
41
44
/// <param name="accessLevel">The access level.</param>
42
45
/// <param name="ignoreScm">if set to <c>true</c> ignore SCM files.</param>
43
46
/// <param name="applyGlobal">if set to <c>true</c> apply global decisions.</param>
44
- /// <returns>An <see cref="Result" /> object.</returns>
45
- /// <remarks>The message property of the result contains the upload id
46
- /// which is needed for further operations.</remarks>
47
+ /// <param name="details">The upload and scan details.</param>
48
+ /// <returns>
49
+ /// An <see cref="Result" /> object.
50
+ /// </returns>
51
+ /// <remarks>
52
+ /// The message property of the result contains the upload id
53
+ /// which is needed for further operations.
54
+ /// </remarks>
47
55
public Result UploadPackage (
48
56
string fileName ,
49
57
int folderId ,
@@ -53,7 +61,8 @@ public Result UploadPackage(
53
61
string description = "" ,
54
62
string accessLevel = "public" ,
55
63
bool ignoreScm = true ,
56
- bool applyGlobal = false )
64
+ bool applyGlobal = false ,
65
+ UploadInformationFile details = null )
57
66
{
58
67
Log . Debug ( $ "Uploading package { fileName } to folder { folderId } ...") ;
59
68
@@ -73,6 +82,18 @@ public Result UploadPackage(
73
82
request . AddHeader ( "Content-Type" , "multipart/form-data" ) ;
74
83
request . AddHeader ( "applyGlobal" , applyGlobal . ToString ( ) ) ;
75
84
85
+ if ( details != null )
86
+ {
87
+ var json = JsonConvert . SerializeObject (
88
+ details ,
89
+ new JsonSerializerSettings
90
+ {
91
+ NullValueHandling = NullValueHandling . Ignore ,
92
+ } ) ;
93
+ request . AddJsonBody ( json ) ;
94
+ request . AddHeader ( "Content-Type" , "application/json" ) ;
95
+ } // if
96
+
76
97
var options = new FileParameterOptions ( ) ;
77
98
var fp = FileParameter . Create (
78
99
"fileInput" ,
@@ -119,13 +140,13 @@ public Result UploadPackage(
119
140
/// </remarks>
120
141
public Result UploadPackageFromUrl (
121
142
int folderId ,
122
- UrlUpload details ,
143
+ UploadInformationUrl details ,
123
144
string groupName = "" ,
124
145
string description = "" ,
125
146
string accessLevel = "public" ,
126
147
bool ignoreScm = true )
127
148
{
128
- Log . Debug ( $ "Uploading package { details . Name } from URL { details . Url } to folder { folderId } ...") ;
149
+ Log . Debug ( $ "Uploading package { details . Location . Name } from URL { details . Location . Url } to folder { folderId } ...") ;
129
150
130
151
var request = new RestRequest ( this . Url + "/uploads" , Method . Post ) ;
131
152
request . RequestFormat = DataFormat . Json ;
@@ -136,7 +157,12 @@ public Result UploadPackageFromUrl(
136
157
request . AddHeader ( "ignoreScm" , ignoreScm . ToString ( ) ) ;
137
158
request . AddHeader ( "uploadType" , "url" ) ;
138
159
139
- var json = JsonConvert . SerializeObject ( details ) ;
160
+ var json = JsonConvert . SerializeObject (
161
+ details ,
162
+ new JsonSerializerSettings
163
+ {
164
+ NullValueHandling = NullValueHandling . Ignore ,
165
+ } ) ;
140
166
request . AddJsonBody ( json ) ;
141
167
request . AddHeader ( "Content-Type" , "application/json" ) ;
142
168
@@ -177,13 +203,13 @@ public Result UploadPackageFromUrl(
177
203
/// </remarks>
178
204
public Result UploadPackageFromVcs (
179
205
int folderId ,
180
- VcsUpload details ,
206
+ UploadInformationVcs details ,
181
207
string groupName = "" ,
182
208
string description = "" ,
183
209
string accessLevel = "public" ,
184
210
bool ignoreScm = true )
185
211
{
186
- Log . Debug ( $ "Uploading package { details . VcsName } from { details . VcsUrl } to folder { folderId } ...") ;
212
+ Log . Debug ( $ "Uploading package { details . Location . VcsName } from { details . Location . VcsUrl } to folder { folderId } ...") ;
187
213
var request = new RestRequest ( this . Url + "/uploads" , Method . Post ) ;
188
214
request . RequestFormat = DataFormat . Json ;
189
215
request . AddHeader ( "folderId" , folderId . ToString ( ) ) ;
@@ -193,7 +219,12 @@ public Result UploadPackageFromVcs(
193
219
request . AddHeader ( "ignoreScm" , ignoreScm . ToString ( ) ) ;
194
220
request . AddHeader ( "uploadType" , "vcs" ) ;
195
221
196
- var json = JsonConvert . SerializeObject ( details ) ;
222
+ var json = JsonConvert . SerializeObject (
223
+ details ,
224
+ new JsonSerializerSettings
225
+ {
226
+ NullValueHandling = NullValueHandling . Ignore ,
227
+ } ) ;
197
228
request . AddJsonBody ( json ) ;
198
229
request . AddHeader ( "Content-Type" , "application/json" ) ;
199
230
@@ -418,7 +449,7 @@ public IReadOnlyList<Upload> GetUploadList(string groupName = "")
418
449
/// Gets the summary for the upload with the specified id.
419
450
/// </summary>
420
451
/// <param name="id">The identifier.</param>
421
- /// <param name="agent">Agent name, one of (nomos, monk, ninka, ojo).</param>
452
+ /// <param name="agent">Agent name, one of (nomos, monk, ninka, ojo, scancode ).</param>
422
453
/// <param name="containers">if set to <c>true</c> show directories and containers.</param>
423
454
/// <returns>A list of <see cref="UploadLicenses" /> objects.</returns>
424
455
public List < UploadLicenses > GetUploadLicenses ( int id , string agent , bool containers )
@@ -470,5 +501,49 @@ public Result DeleteUpload(int id, string groupName = "")
470
501
var result = JsonConvert . DeserializeObject < Result > ( response . Content ) ;
471
502
return result ;
472
503
} // DeleteUpload()
504
+
505
+ /// <summary>
506
+ /// Gets the upload file by identifier.
507
+ /// </summary>
508
+ /// <param name="id">The identifier.</param>
509
+ /// <param name="filename">The filename.</param>
510
+ /// <returns>
511
+ /// An <see cref="Result" /> object.
512
+ /// </returns>
513
+ public Result GetUploadFileById ( int id , string filename )
514
+ {
515
+ Log . Debug ( $ "Downloading upload { id } ...") ;
516
+
517
+ var request = new RestRequest ( this . Url + $ "/uploads/{ id } /download", Method . Get ) ;
518
+ var response = this . api . Execute ( request ) ;
519
+ if ( response ? . Content == null )
520
+ {
521
+ throw new FossologyApiException ( ErrorCode . NoValidAnswer ) ;
522
+ } // if
523
+
524
+ if ( response . StatusCode == HttpStatusCode . OK )
525
+ {
526
+ System . IO . File . WriteAllBytes ( filename , response . RawBytes ) ;
527
+ var res = new Result ( ) ;
528
+ res . Code = ( int ) HttpStatusCode . OK ;
529
+ res . Message = string . Empty ;
530
+ return res ;
531
+ } // if
532
+
533
+ var result = JsonConvert . DeserializeObject < Result > ( response . Content ) ;
534
+ if ( result == null )
535
+ {
536
+ Log . Error ( "Got empty response!" ) ;
537
+ }
538
+ else
539
+ {
540
+ if ( result . Code != ( int ) HttpStatusCode . OK )
541
+ {
542
+ Log . Error ( $ "Error downloading upload: { result . Message } ") ;
543
+ } // if
544
+ } // if
545
+
546
+ return result ;
547
+ } // GetUploadFileById()
473
548
} // FossologyClient
474
549
}
0 commit comments