Skip to content

Commit 493a841

Browse files
committed
feat: new GetUploadFileById to download an upload file
1 parent f3a4671 commit 493a841

File tree

2 files changed

+134
-11
lines changed

2 files changed

+134
-11
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// ---------------------------------------------------------------------------
2+
// <copyright file="ScanCodeInfo.cs" company="Tethys">
3+
// Copyright (C) 2023 T. Graf
4+
// </copyright>
5+
//
6+
// Licensed under the MIT License.
7+
// SPDX-License-Identifier: MIT
8+
//
9+
// Unless required by applicable law or agreed to in writing,
10+
// software distributed under the License is distributed on an
11+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12+
// either express or implied.
13+
// ---------------------------------------------------------------------------
14+
15+
namespace Fossology.Rest.Dotnet.Model
16+
{
17+
using Newtonsoft.Json;
18+
19+
/// <summary>
20+
/// Trigger ScanCode information.
21+
/// </summary>
22+
public class ScanCodeInfo
23+
{
24+
/// <summary>
25+
/// Gets or sets a value indicating whether to scan for licenses.
26+
/// </summary>
27+
[JsonProperty("license")]
28+
public bool License { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets a value indicating whether to scan for copyrights.
32+
/// </summary>
33+
[JsonProperty("copyright")]
34+
public bool Copyright { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets a value indicating whether to scan for emails.
38+
/// </summary>
39+
[JsonProperty("email")]
40+
public bool Email { get; set; }
41+
42+
/// <summary>
43+
/// Gets or sets a value indicating whether to scan for urls.
44+
/// </summary>
45+
[JsonProperty("url")]
46+
public bool Url { get; set; }
47+
} // ScanCodeInfo
48+
}

Fossology.Rest.Dotnet/FossologyClientUpload.cs

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ namespace Fossology.Rest.Dotnet
1818
using System.Collections.Generic;
1919
using System.Linq;
2020
using System.Net;
21+
using System.Runtime.InteropServices.ComTypes;
22+
using System.Threading.Tasks;
2123
using Fossology.Rest.Dotnet.Model;
2224

2325
using Newtonsoft.Json;
2426

2527
using RestSharp;
28+
using RestSharp.Extensions;
2629

2730
/// <summary>
2831
/// Client for the SW360 REST API.
@@ -41,9 +44,14 @@ public partial class FossologyClient
4144
/// <param name="accessLevel">The access level.</param>
4245
/// <param name="ignoreScm">if set to <c>true</c> ignore SCM files.</param>
4346
/// <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>
4755
public Result UploadPackage(
4856
string fileName,
4957
int folderId,
@@ -53,7 +61,8 @@ public Result UploadPackage(
5361
string description = "",
5462
string accessLevel = "public",
5563
bool ignoreScm = true,
56-
bool applyGlobal = false)
64+
bool applyGlobal = false,
65+
UploadInformationFile details = null)
5766
{
5867
Log.Debug($"Uploading package {fileName} to folder {folderId}...");
5968

@@ -73,6 +82,18 @@ public Result UploadPackage(
7382
request.AddHeader("Content-Type", "multipart/form-data");
7483
request.AddHeader("applyGlobal", applyGlobal.ToString());
7584

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+
7697
var options = new FileParameterOptions();
7798
var fp = FileParameter.Create(
7899
"fileInput",
@@ -119,13 +140,13 @@ public Result UploadPackage(
119140
/// </remarks>
120141
public Result UploadPackageFromUrl(
121142
int folderId,
122-
UrlUpload details,
143+
UploadInformationUrl details,
123144
string groupName = "",
124145
string description = "",
125146
string accessLevel = "public",
126147
bool ignoreScm = true)
127148
{
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}...");
129150

130151
var request = new RestRequest(this.Url + "/uploads", Method.Post);
131152
request.RequestFormat = DataFormat.Json;
@@ -136,7 +157,12 @@ public Result UploadPackageFromUrl(
136157
request.AddHeader("ignoreScm", ignoreScm.ToString());
137158
request.AddHeader("uploadType", "url");
138159

139-
var json = JsonConvert.SerializeObject(details);
160+
var json = JsonConvert.SerializeObject(
161+
details,
162+
new JsonSerializerSettings
163+
{
164+
NullValueHandling = NullValueHandling.Ignore,
165+
});
140166
request.AddJsonBody(json);
141167
request.AddHeader("Content-Type", "application/json");
142168

@@ -177,13 +203,13 @@ public Result UploadPackageFromUrl(
177203
/// </remarks>
178204
public Result UploadPackageFromVcs(
179205
int folderId,
180-
VcsUpload details,
206+
UploadInformationVcs details,
181207
string groupName = "",
182208
string description = "",
183209
string accessLevel = "public",
184210
bool ignoreScm = true)
185211
{
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}...");
187213
var request = new RestRequest(this.Url + "/uploads", Method.Post);
188214
request.RequestFormat = DataFormat.Json;
189215
request.AddHeader("folderId", folderId.ToString());
@@ -193,7 +219,12 @@ public Result UploadPackageFromVcs(
193219
request.AddHeader("ignoreScm", ignoreScm.ToString());
194220
request.AddHeader("uploadType", "vcs");
195221

196-
var json = JsonConvert.SerializeObject(details);
222+
var json = JsonConvert.SerializeObject(
223+
details,
224+
new JsonSerializerSettings
225+
{
226+
NullValueHandling = NullValueHandling.Ignore,
227+
});
197228
request.AddJsonBody(json);
198229
request.AddHeader("Content-Type", "application/json");
199230

@@ -418,7 +449,7 @@ public IReadOnlyList<Upload> GetUploadList(string groupName = "")
418449
/// Gets the summary for the upload with the specified id.
419450
/// </summary>
420451
/// <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>
422453
/// <param name="containers">if set to <c>true</c> show directories and containers.</param>
423454
/// <returns>A list of <see cref="UploadLicenses" /> objects.</returns>
424455
public List<UploadLicenses> GetUploadLicenses(int id, string agent, bool containers)
@@ -470,5 +501,49 @@ public Result DeleteUpload(int id, string groupName = "")
470501
var result = JsonConvert.DeserializeObject<Result>(response.Content);
471502
return result;
472503
} // 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()
473548
} // FossologyClient
474549
}

0 commit comments

Comments
 (0)