1
1
// ---------------------------------------------------------------------------
2
2
// <copyright file="FossologyClientUpload.cs" company="Tethys">
3
- // Copyright (C) 2019-2022 T. Graf
3
+ // Copyright (C) 2019-2025 T. Graf
4
4
// </copyright>
5
5
//
6
6
// Licensed under the MIT License.
@@ -18,14 +18,11 @@ 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 ;
23
21
using Fossology . Rest . Dotnet . Model ;
24
22
25
23
using Newtonsoft . Json ;
26
24
27
25
using RestSharp ;
28
- using RestSharp . Extensions ;
29
26
30
27
/// <summary>
31
28
/// Client for the SW360 REST API.
@@ -514,14 +511,15 @@ public Result GetUploadFileById(int id, string filename)
514
511
{
515
512
Log . Debug ( $ "Downloading upload { id } ...") ;
516
513
514
+ // ReSharper disable once RedundantArgumentDefaultValue
517
515
var request = new RestRequest ( this . Url + $ "/uploads/{ id } /download", Method . Get ) ;
518
516
var response = this . api . Execute ( request ) ;
519
517
if ( response ? . Content == null )
520
518
{
521
519
throw new FossologyApiException ( ErrorCode . NoValidAnswer ) ;
522
520
} // if
523
521
524
- if ( response . StatusCode == HttpStatusCode . OK )
522
+ if ( ( response . StatusCode == HttpStatusCode . OK ) && ( response . RawBytes != null ) )
525
523
{
526
524
System . IO . File . WriteAllBytes ( filename , response . RawBytes ) ;
527
525
var res = new Result ( ) ;
@@ -547,7 +545,7 @@ public Result GetUploadFileById(int id, string filename)
547
545
} // GetUploadFileById()
548
546
549
547
/// <summary>
550
- /// Gets the coüpyright for an upload.
548
+ /// Gets the copyright for an upload.
551
549
/// </summary>
552
550
/// <param name="id">The identifier.</param>
553
551
/// <returns>A list of <see cref="CopyrightEntry" /> objects.</returns>
@@ -572,5 +570,78 @@ public List<CopyrightEntry> GetUploadCopyrights(int id)
572
570
573
571
return summary ;
574
572
} // GetUploadCopyrights()
573
+
574
+ /// <summary>
575
+ /// Get the item id for the top level item in a given upload.
576
+ /// </summary>
577
+ /// <param name="id">The upload identifier.</param>
578
+ /// <returns>System.Int32.</returns>
579
+ public int GetTopItem ( int id )
580
+ {
581
+ Log . Debug ( $ "Getting upload top item upload { id } ...") ;
582
+
583
+ var request = new RestRequest ( this . Url + $ "/uploads/{ id } /topitem") ;
584
+ request . RequestFormat = DataFormat . Json ;
585
+ var response = this . api . Execute ( request ) ;
586
+ if ( response ? . Content == null )
587
+ {
588
+ throw new FossologyApiException ( ErrorCode . NoValidAnswer ) ;
589
+ } // if
590
+
591
+ var result = JsonConvert . DeserializeObject < Result > ( response . Content ) ;
592
+ if ( result == null )
593
+ {
594
+ Log . Error ( "Got empty response!" ) ;
595
+ return - 1 ;
596
+ }
597
+ else
598
+ {
599
+ if ( result . Code != ( int ) HttpStatusCode . OK )
600
+ {
601
+ Log . Error ( $ "Error getting top-item: { result . Message } ") ;
602
+ } // if
603
+ } // if
604
+
605
+ return int . Parse ( result . Message ) ;
606
+ } // GetTopItem()
607
+
608
+ #if false // NOT YET SUPPORTED
609
+ /// <summary>
610
+ /// Gets the copyright found by ScanCode for an upload.
611
+ /// </summary>
612
+ /// <param name="uploadId">The upload id.</param>
613
+ /// <param name="itemId">The upload tree id.</param>
614
+ /// <param name="status">Status of the CX.</param>
615
+ /// <param name="limit">Limits of responses per request.</param>
616
+ /// <param name="page">Page number for responses.</param>
617
+ /// <returns>A list of <see cref="CopyrightEntry" /> objects.</returns>
618
+ public List < CopyrightEntry > GetUploadScanCodeCopyrights (
619
+ int uploadId ,
620
+ int itemId ,
621
+ string status ,
622
+ int limit ,
623
+ int page )
624
+ {
625
+ Log . Debug ( $ "Getting upload copyrights for upload { uploadId } , { itemId } ...") ;
626
+
627
+ ////var request = new RestRequest(this.Url + $"/uploads/{uploadId}/item/{itemId}/scancode-copyrights?status={status}");
628
+ var request = new RestRequest ( "http://localhost:8081/repo/api/v2" + $ "/uploads/{ uploadId } /item/{ itemId } /scancode-copyrights?status={ status } ") ;
629
+ request . RequestFormat = DataFormat . Json ;
630
+ var response = this . api . Execute ( request ) ;
631
+ if ( response ? . Content == null )
632
+ {
633
+ throw new FossologyApiException ( ErrorCode . NoValidAnswer ) ;
634
+ } // if
635
+
636
+ var summary = JsonConvert . DeserializeObject < List < CopyrightEntry > > (
637
+ response . Content ,
638
+ new JsonSerializerSettings
639
+ {
640
+ NullValueHandling = NullValueHandling . Ignore ,
641
+ } ) ;
642
+
643
+ return summary ;
644
+ } // GetUploadScanCodeCopyrights()
645
+ #endif
575
646
} // FossologyClient
576
647
}
0 commit comments