Skip to content

Commit ca478e4

Browse files
committed
feat: updates for RestSharp 108.0.3
1 parent 4556ea0 commit ca478e4

15 files changed

+577
-188
lines changed

Fossology.Rest.Dotnet/ErrorCode.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@ public enum ErrorCode
4343
/// File to upload not found.
4444
/// </summary>
4545
FileNotFound = 4,
46+
47+
/// <summary>
48+
/// The no valid answer from server.
49+
/// </summary>
50+
NoValidAnswer = 5,
4651
} // ErrorCode
4752
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// ---------------------------------------------------------------------------
2+
// <copyright file="FileStreamWithProgress.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;
18+
using System.IO;
19+
using Tethys.Logging;
20+
21+
/// <summary>
22+
/// Implements a file stream that can report the read progress.
23+
/// </summary>
24+
/// <seealso cref="System.IO.Stream" />
25+
internal class FileStreamWithProgress : Stream
26+
{
27+
#region PRIVATE PROPERTIES
28+
/// <summary>
29+
/// The logger for this class.
30+
/// </summary>
31+
private static readonly ILog Log = LogManager.GetLogger(typeof(FileStreamWithProgress));
32+
33+
/// <summary>
34+
/// The file stream.
35+
/// </summary>
36+
private readonly FileStream fileStream;
37+
38+
/// <summary>
39+
/// The upload finished callback.
40+
/// </summary>
41+
private readonly Action uploadFinished;
42+
43+
/// <summary>
44+
/// The upload progress callback.
45+
/// </summary>
46+
private readonly Action<float> uploadProgress;
47+
48+
/// <summary>
49+
/// The current position.
50+
/// </summary>
51+
private long sofar;
52+
#endregion // PRIVATE PROPERTIES
53+
54+
//// ---------------------------------------------------------------------
55+
56+
#region PUBLIC PROPERTIES
57+
/// <inheritdoc />
58+
public override bool CanRead
59+
{
60+
get { return this.fileStream != null && this.fileStream.CanRead; }
61+
}
62+
63+
/// <inheritdoc />
64+
public override bool CanSeek
65+
{
66+
get { return this.fileStream != null && this.fileStream.CanSeek; }
67+
}
68+
69+
/// <inheritdoc />
70+
public override bool CanWrite
71+
{
72+
get { return this.fileStream != null && this.fileStream.CanWrite; }
73+
}
74+
75+
/// <inheritdoc />
76+
public override long Length
77+
{
78+
get
79+
{
80+
return this.fileStream?.Length ?? 0;
81+
}
82+
}
83+
84+
/// <inheritdoc />
85+
public override long Position
86+
{
87+
get { return this.fileStream?.Position ?? 0; }
88+
set { }
89+
}
90+
#endregion // PUBLIC PROPERTIES
91+
92+
//// ---------------------------------------------------------------------
93+
94+
#region CONSTRUCTION
95+
/// <summary>
96+
/// Initializes a new instance of the <see cref="FileStreamWithProgress"/> class.
97+
/// </summary>
98+
/// <param name="fileName">Name of the file.</param>
99+
/// <param name="uploadFinished">The optional upload finished callback.</param>
100+
/// <param name="uploadProgress">The optional upload progress callback.</param>
101+
public FileStreamWithProgress(
102+
string fileName,
103+
Action uploadFinished = null,
104+
Action<float> uploadProgress = null)
105+
{
106+
this.fileStream = new FileStream(fileName, FileMode.Open);
107+
this.uploadFinished = uploadFinished;
108+
this.uploadProgress = uploadProgress;
109+
this.sofar = 0;
110+
} // FileStreamWithProgress()
111+
#endregion // CONSTRUCTION
112+
113+
//// ---------------------------------------------------------------------
114+
115+
#region PUBLIC METHODS
116+
/// <inheritdoc />
117+
public override void Flush()
118+
{
119+
this.fileStream?.Flush();
120+
}
121+
122+
/// <inheritdoc />
123+
public override int Read(byte[] buffer, int offset, int count)
124+
{
125+
if (this.fileStream == null)
126+
{
127+
return 0;
128+
} // if
129+
130+
var result = this.fileStream.Read(buffer, offset, count);
131+
132+
this.sofar = this.fileStream.Position;
133+
134+
var percentage = this.sofar * 100 / this.fileStream.Length;
135+
var progress = percentage / 100f;
136+
137+
Log.Debug($"Upload progress = {progress}");
138+
this.uploadProgress?.Invoke(progress);
139+
140+
if (this.sofar >= this.fileStream.Length)
141+
{
142+
this.uploadFinished?.Invoke();
143+
} // if
144+
145+
return result;
146+
}
147+
148+
/// <inheritdoc />
149+
public override long Seek(long offset, SeekOrigin origin)
150+
{
151+
if (this.fileStream == null)
152+
{
153+
return 0;
154+
} // if
155+
156+
var result = this.fileStream.Seek(offset, origin);
157+
this.sofar = result;
158+
return result;
159+
}
160+
161+
/// <inheritdoc />
162+
public override void SetLength(long value)
163+
{
164+
this.fileStream?.SetLength(value);
165+
}
166+
167+
/// <inheritdoc />
168+
public override void Write(byte[] buffer, int offset, int count)
169+
{
170+
this.fileStream?.Write(buffer, offset, count);
171+
}
172+
#endregion // PUBLIC METHODS
173+
} // FileStreamWithProgress
174+
}

Fossology.Rest.Dotnet/FossologyClient.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,9 @@ namespace Fossology.Rest.Dotnet
1919
using Newtonsoft.Json;
2020

2121
using RestSharp;
22-
22+
using RestSharp.Serializers;
2323
using Tethys.Logging;
2424

25-
using JsonSerializer = RestSharp.Serialization.Json.JsonSerializer;
26-
27-
/*************************************************************************
28-
* Required NuGet Packages
29-
* ------------------------
30-
* - Tethys.Logging 1.3.0, Apache-2.0
31-
* - Newtonsoft.Json 12.0.3, MIT
32-
* - RestSharp 106.6.10
33-
*
34-
************************************************************************/
35-
3625
/// <summary>
3726
/// Client for the SW360 REST API.
3827
/// </summary>
@@ -95,6 +84,11 @@ public VersionInfo GetVersion()
9584
Log.Debug("Getting version info...");
9685

9786
var result = this.api.Get(this.Url + "/version");
87+
if (result?.Content == null)
88+
{
89+
throw new FossologyApiException(ErrorCode.NoValidAnswer);
90+
} // if
91+
9892
var version = JsonConvert.DeserializeObject<VersionInfo>(result.Content);
9993
return version;
10094
} // GetVersion()
@@ -109,14 +103,16 @@ public string GetToken(TokenRequest requestDetails)
109103
Log.Debug($"Requesting token {requestDetails.TokenName} for user {requestDetails.Username}...");
110104

111105
var json = JsonConvert.SerializeObject(requestDetails);
112-
var request = new RestRequest(this.Url + "/tokens", Method.POST);
113-
request.RequestFormat = DataFormat.Json;
114-
request.JsonSerializer = new JsonSerializer();
115-
request.Parameters.Clear();
116-
117-
request.AddJsonBody(json);
106+
var request = new RestRequest(this.Url + "/tokens", Method.Post);
107+
request.AddStringBody(json, ContentType.Json);
108+
request.AddHeader("Content-Type", "application/json");
118109

119110
var response = this.api.Execute(request);
111+
if (response?.Content == null)
112+
{
113+
throw new FossologyApiException(ErrorCode.NoValidAnswer);
114+
} // if
115+
120116
var result = JsonConvert.DeserializeObject<TokenResponse>(response.Content);
121117

122118
if (result == null)

Fossology.Rest.Dotnet/FossologyClientFolder.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ namespace Fossology.Rest.Dotnet
2121

2222
using RestSharp;
2323

24-
using JsonSerializer = RestSharp.Serialization.Json.JsonSerializer;
25-
2624
/// <summary>
2725
/// Client for the SW360 REST API.
2826
/// </summary>
@@ -38,16 +36,19 @@ public Folder GetFolder(int id, string groupName = "")
3836
{
3937
Log.Debug($"Getting folder {id}...");
4038

41-
var request = new RestRequest(this.Url + $"/folders/{id}", Method.GET);
39+
var request = new RestRequest(this.Url + $"/folders/{id}");
4240
request.RequestFormat = DataFormat.Json;
43-
request.JsonSerializer = new JsonSerializer();
44-
request.Parameters.Clear();
4541
if (!string.IsNullOrEmpty(groupName))
4642
{
4743
request.AddHeader("groupName", groupName);
4844
} // if
4945

5046
var response = this.api.Execute(request);
47+
if (response?.Content == null)
48+
{
49+
throw new FossologyApiException(ErrorCode.NoValidAnswer);
50+
} // if
51+
5152
var folder = JsonConvert.DeserializeObject<Folder>(
5253
response.Content,
5354
new JsonSerializerSettings
@@ -66,16 +67,19 @@ public IReadOnlyList<Folder> GetFolderList(string groupName = "")
6667
{
6768
Log.Debug("Getting list of folder...");
6869

69-
var request = new RestRequest(this.Url + $"/folders", Method.GET);
70+
var request = new RestRequest(this.Url + "/folders");
7071
request.RequestFormat = DataFormat.Json;
71-
request.JsonSerializer = new JsonSerializer();
72-
request.Parameters.Clear();
7372
if (!string.IsNullOrEmpty(groupName))
7473
{
7574
request.AddHeader("groupName", groupName);
7675
} // if
7776

7877
var response = this.api.Execute(request);
78+
if (response?.Content == null)
79+
{
80+
throw new FossologyApiException(ErrorCode.NoValidAnswer);
81+
} // if
82+
7983
var list = JsonConvert.DeserializeObject<List<Folder>>(
8084
response.Content,
8185
new JsonSerializerSettings
@@ -99,7 +103,7 @@ public IReadOnlyList<Folder> GetFolderList(string groupName = "")
99103
/// </remarks>>
100104
public Result CreateFolder(string folderName, int parentFolder, string description = "", string groupName = "")
101105
{
102-
var request = new RestRequest(this.Url + "/folders", Method.POST);
106+
var request = new RestRequest(this.Url + "/folders", Method.Post);
103107
request.RequestFormat = DataFormat.Json;
104108
request.AddHeader("parentFolder", parentFolder.ToString());
105109
request.AddHeader("folderName", folderName);
@@ -114,8 +118,16 @@ public Result CreateFolder(string folderName, int parentFolder, string descripti
114118
} // if
115119

116120
var resultRaw = this.api.Execute(request);
121+
if (resultRaw?.Content == null)
122+
{
123+
throw new FossologyApiException(ErrorCode.NoValidAnswer);
124+
} // if
125+
117126
var result = JsonConvert.DeserializeObject<Result>(resultRaw.Content);
118-
Log.Debug($"Folder {result.Message} created.");
127+
if (result != null)
128+
{
129+
Log.Debug($"Folder {result.Message} created.");
130+
} // if
119131

120132
return result;
121133
} // CreateFolder()
@@ -130,16 +142,19 @@ public Result DeleteFolder(int id, string groupName = "")
130142
{
131143
Log.Debug($"Deleting folder {id}...");
132144

133-
var request = new RestRequest(this.Url + $"/folders/{id}", Method.DELETE);
145+
var request = new RestRequest(this.Url + $"/folders/{id}", Method.Delete);
134146
request.RequestFormat = DataFormat.Json;
135-
request.JsonSerializer = new JsonSerializer();
136-
request.Parameters.Clear();
137147
if (!string.IsNullOrEmpty(groupName))
138148
{
139149
request.AddHeader("groupName", groupName);
140150
} // if
141151

142152
var response = this.api.Execute(request);
153+
if (response?.Content == null)
154+
{
155+
throw new FossologyApiException(ErrorCode.NoValidAnswer);
156+
} // if
157+
143158
var result = JsonConvert.DeserializeObject<Result>(response.Content);
144159
return result;
145160
} // DeleteFolder()

Fossology.Rest.Dotnet/FossologyClientGroup.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ namespace Fossology.Rest.Dotnet
2121

2222
using RestSharp;
2323

24-
using JsonSerializer = RestSharp.Serialization.Json.JsonSerializer;
25-
2624
/// <summary>
2725
/// Client for the SW360 REST API.
2826
/// </summary>
@@ -36,12 +34,15 @@ public IReadOnlyList<Group> GetGroupList()
3634
{
3735
Log.Debug("Getting list of folder...");
3836

39-
var request = new RestRequest(this.Url + $"/groups", Method.GET);
37+
var request = new RestRequest(this.Url + "/groups");
4038
request.RequestFormat = DataFormat.Json;
41-
request.JsonSerializer = new JsonSerializer();
42-
request.Parameters.Clear();
4339

4440
var response = this.api.Execute(request);
41+
if (response?.Content == null)
42+
{
43+
throw new FossologyApiException(ErrorCode.NoValidAnswer);
44+
} // if
45+
4546
var list = JsonConvert.DeserializeObject<List<Group>>(
4647
response.Content,
4748
new JsonSerializerSettings
@@ -62,13 +63,21 @@ public IReadOnlyList<Group> GetGroupList()
6263
/// </remarks>>
6364
public Result CreateGroup(string groupName)
6465
{
65-
var request = new RestRequest(this.Url + "/groups", Method.POST);
66+
var request = new RestRequest(this.Url + "/groups", Method.Post);
6667
request.RequestFormat = DataFormat.Json;
6768
request.AddHeader("name", groupName);
6869

6970
var resultRaw = this.api.Execute(request);
71+
if (resultRaw?.Content == null)
72+
{
73+
throw new FossologyApiException(ErrorCode.NoValidAnswer);
74+
} // if
75+
7076
var result = JsonConvert.DeserializeObject<Result>(resultRaw.Content);
71-
Log.Debug($"Group {result.Message} created.");
77+
if (result != null)
78+
{
79+
Log.Debug($"Group {result.Message} created.");
80+
} // if
7281

7382
return result;
7483
} // CreateGroup()

0 commit comments

Comments
 (0)