|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// <copyright file="FossologyClientLicense.cs" company="Tethys"> |
| 3 | +// Copyright (C) 2019-2022 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 |
| 16 | +{ |
| 17 | + using System.Collections.Generic; |
| 18 | + using Fossology.Rest.Dotnet.Model; |
| 19 | + |
| 20 | + using Newtonsoft.Json; |
| 21 | + |
| 22 | + using RestSharp; |
| 23 | + |
| 24 | + using JsonSerializer = RestSharp.Serialization.Json.JsonSerializer; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Client for the SW360 REST API. |
| 28 | + /// </summary> |
| 29 | + public partial class FossologyClient |
| 30 | + { |
| 31 | + /// <summary> |
| 32 | + /// Gets the list of all licenses. |
| 33 | + /// </summary> |
| 34 | + /// <param name="page">The page.</param> |
| 35 | + /// <param name="limit">The limit.</param> |
| 36 | + /// <param name="kind">The license kind, one of candidate, main, all.</param> |
| 37 | + /// <param name="active">if set to <c>true</c>, return only active licenses.</param> |
| 38 | + /// <param name="groupName">The group name to choose.</param> |
| 39 | + /// <returns>A list of <see cref="License" /> objects.</returns> |
| 40 | + public IReadOnlyList<License> GetLicenseList( |
| 41 | + int page = 1, |
| 42 | + int limit = 100, |
| 43 | + string kind = "all", |
| 44 | + bool active = false, |
| 45 | + string groupName = "") |
| 46 | + { |
| 47 | + Log.Debug("Getting list of licenses..."); |
| 48 | + |
| 49 | + var request = new RestRequest(this.Url + $"/license?kind={kind}", Method.GET); |
| 50 | + request.RequestFormat = DataFormat.Json; |
| 51 | + request.JsonSerializer = new JsonSerializer(); |
| 52 | + request.Parameters.Clear(); |
| 53 | + request.AddHeader("page", page.ToString()); |
| 54 | + request.AddHeader("limit", limit.ToString()); |
| 55 | + request.AddHeader("active", active.ToString()); |
| 56 | + |
| 57 | + if (!string.IsNullOrEmpty(groupName)) |
| 58 | + { |
| 59 | + request.AddHeader("groupName", groupName); |
| 60 | + } // if |
| 61 | + |
| 62 | + var response = this.api.Execute(request); |
| 63 | + var list = JsonConvert.DeserializeObject<List<License>>( |
| 64 | + response.Content, |
| 65 | + new JsonSerializerSettings |
| 66 | + { |
| 67 | + NullValueHandling = NullValueHandling.Ignore, |
| 68 | + }); |
| 69 | + return list; |
| 70 | + } // GetLicenseList() |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets the license with the specified short name. |
| 74 | + /// </summary> |
| 75 | + /// <param name="shortName">The license short name.</param> |
| 76 | + /// <param name="groupName">The group name to choose.</param> |
| 77 | + /// <returns>A <see cref="License"/> object.</returns> |
| 78 | + public License GetLicense(string shortName, string groupName = "") |
| 79 | + { |
| 80 | + Log.Debug($"Getting license {shortName}..."); |
| 81 | + |
| 82 | + var request = new RestRequest(this.Url + $"/license/{shortName}", Method.GET); |
| 83 | + request.RequestFormat = DataFormat.Json; |
| 84 | + request.JsonSerializer = new JsonSerializer(); |
| 85 | + request.Parameters.Clear(); |
| 86 | + if (!string.IsNullOrEmpty(groupName)) |
| 87 | + { |
| 88 | + request.AddHeader("groupName", groupName); |
| 89 | + } // if |
| 90 | + |
| 91 | + var response = this.api.Execute(request); |
| 92 | + var license = JsonConvert.DeserializeObject<License>( |
| 93 | + response.Content, |
| 94 | + new JsonSerializerSettings |
| 95 | + { |
| 96 | + NullValueHandling = NullValueHandling.Ignore, |
| 97 | + }); |
| 98 | + return license; |
| 99 | + } // GetLicense() |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Creates a new license. |
| 103 | + /// </summary> |
| 104 | + /// <param name="newLicense">The new license to be created.</param> |
| 105 | + /// <param name="groupName">The group name to choose (optional).</param> |
| 106 | + /// <returns>An <see cref="Result" /> object.</returns> |
| 107 | + /// > |
| 108 | + /// <remarks>The message property of the result contains the folder id |
| 109 | + /// which is needed for further operations.</remarks> |
| 110 | + public Result CreateLicense(License newLicense, string groupName = "") |
| 111 | + { |
| 112 | + var json = JsonConvert.SerializeObject(newLicense); |
| 113 | + var request = new RestRequest(this.Url + "/license", Method.POST); |
| 114 | + request.RequestFormat = DataFormat.Json; |
| 115 | + if (!string.IsNullOrEmpty(groupName)) |
| 116 | + { |
| 117 | + request.AddHeader("groupName", groupName); |
| 118 | + } // if |
| 119 | + |
| 120 | + request.AddJsonBody(json); |
| 121 | + |
| 122 | + var resultRaw = this.api.Execute(request); |
| 123 | + var result = JsonConvert.DeserializeObject<Result>(resultRaw.Content); |
| 124 | + Log.Debug($"Folder {result.Message} created."); |
| 125 | + |
| 126 | + return result; |
| 127 | + } // CreateLicense() |
| 128 | + } // FossologyClient |
| 129 | +} |
0 commit comments