Skip to content

Commit 2530c0d

Browse files
committed
feat: update demo application to create tokens
1 parent 339ae55 commit 2530c0d

File tree

4 files changed

+147
-25
lines changed

4 files changed

+147
-25
lines changed

FossyApiDemo/FossyApiDemo.ruleset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<Rule Id="SA1204" Action="None" />
7272
<Rule Id="SA1305" Action="Warning" />
7373
<Rule Id="SA1412" Action="Warning" />
74+
<Rule Id="SA1514" Action="None" />
7475
<Rule Id="SA1515" Action="None" />
7576
<Rule Id="SA1636" Action="None" />
7677
</Rules>

FossyApiDemo/MainForm.Designer.cs

Lines changed: 36 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FossyApiDemo/MainForm.cs

Lines changed: 108 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ---------------------------------------------------------------------------
22
// <copyright file="MainForm.cs" company="Tethys">
3-
// Copyright (C) 2019 T. Graf
3+
// Copyright (C) 2019-2022 T. Graf
44
// </copyright>
55
//
66
// Licensed under the MIT License.
@@ -118,7 +118,7 @@ private void MainFormLoad(object sender, EventArgs e)
118118
{
119119
#if DEBUG
120120
this.txtFossyUrl.Text = "http://localhost:8081/repo/api/v1";
121-
this.txtToken.Text = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsb2NhbGhvc3QiLCJhdWQiOiJsb2NhbGhvc3QiLCJleHAiOjE1NzUxNTgzOTksIm5iZiI6MTU3NDU1MzYwMCwianRpIjoiTWk0eiIsInNjb3BlIjoid3JpdGUifQ.vubUlZv2u_9naEU1VAkAPWhS0Ccn8tVnbNNURlQyDko";
121+
this.txtToken.Text = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NDc0NzUxOTksIm5iZiI6MTY0NzEyOTYwMCwianRpIjoiTXk0eiIsInNjb3BlIjoid3JpdGUifQ.DSDQDyodi5LgIUyqUbOKQiMtpoqOkI0RnF-uphwI_0A";
122122
this.txtFolder.Text = "TestFolder";
123123
this.txtFile.Text = @"..\..\..\TestData\fetch-retry-master.zip";
124124
#endif
@@ -147,27 +147,74 @@ private void BtnBrowseUploadClick(object sender, EventArgs e)
147147
} // BtnBrowseUploadClick()
148148

149149
/// <summary>
150-
/// Handles the Click event of the <c>btnUpload</c> control.
150+
/// Handles the Click event of the btnGetToken control.
151151
/// </summary>
152152
/// <param name="sender">The source of the event.</param>
153153
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
154-
private async void BtnUploadClick(object sender, EventArgs e)
154+
private void BtnGetTokenClick(object sender, EventArgs e)
155155
{
156-
if (!this.CheckFolder())
156+
try
157157
{
158-
return;
158+
log.Info("Creating token for default user 'fossy'...");
159+
var token = this.GetToken();
160+
161+
log.Info($"Token = {token}");
162+
163+
this.txtToken.Text = token;
164+
log.Info("Token has been inserted into token text box.");
165+
}
166+
catch (Exception ex)
167+
{
168+
log.Error("Error getting token: " + ex.Message);
159169
} // if
170+
} // BtnGetTokenClick()
160171

161-
if (string.IsNullOrEmpty(this.txtFile.Text))
172+
/// <summary>
173+
/// Handles the Click event of the btnShowInfo control.
174+
/// </summary>
175+
/// <param name="sender">The source of the event.</param>
176+
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
177+
private void BtnShowInfoClick(object sender, EventArgs e)
178+
{
179+
try
162180
{
163-
log.Error("No file to upload specified!");
181+
this.ShowInfo();
182+
}
183+
catch (Exception ex)
184+
{
185+
log.Error("Error showing info: " + ex.Message);
164186
} // if
187+
} // BtnShowInfoClick()
165188

166-
this.cancelUpload = false;
167-
var result = await this.ProcessFileAsync(this.txtFile.Text);
168-
if (result)
189+
/// <summary>
190+
/// Handles the Click event of the <c>btnUpload</c> control.
191+
/// </summary>
192+
/// <param name="sender">The source of the event.</param>
193+
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
194+
private async void BtnUploadClick(object sender, EventArgs e)
195+
{
196+
try
169197
{
170-
log.Info("Done.");
198+
if (!this.CheckFolder())
199+
{
200+
return;
201+
} // if
202+
203+
if (string.IsNullOrEmpty(this.txtFile.Text))
204+
{
205+
log.Error("No file to upload specified!");
206+
} // if
207+
208+
this.cancelUpload = false;
209+
var result = await this.ProcessFileAsync(this.txtFile.Text);
210+
if (result)
211+
{
212+
log.Info("Done.");
213+
} // if
214+
}
215+
catch (Exception ex)
216+
{
217+
log.Error("Error: " + ex.Message);
171218
} // if
172219
} // BtnUploadClick()
173220

@@ -214,6 +261,55 @@ private void MainFormDragDrop(object sender, DragEventArgs e)
214261
//// ---------------------------------------------------------------------
215262

216263
#region PRIVATE METHODS
264+
/// <summary>
265+
/// Gets an access token for the default user 'fossy'.
266+
/// </summary>
267+
/// <returns>System.String.</returns>
268+
private string GetToken()
269+
{
270+
if (this.client == null)
271+
{
272+
this.CreateClient();
273+
} // if
274+
275+
var request = new TokenRequest();
276+
request.Username = "fossy";
277+
request.Password = "fossy";
278+
request.TokenName = "TestToken";
279+
request.TokenScope = "write";
280+
request.TokenExpire = DateTime.Today.AddDays(3).ToString("yyyy-MM-dd");
281+
var result = this.client.GetToken(request);
282+
283+
return result;
284+
} // GetToken()
285+
286+
/// <summary>
287+
/// Shows the information.
288+
/// </summary>
289+
private void ShowInfo()
290+
{
291+
if (this.client == null)
292+
{
293+
this.CreateClient();
294+
} // if
295+
296+
// get version does not require an access token
297+
var version = this.client.GetVersion();
298+
log.Info($"FOSSology version = {version.Version}");
299+
300+
// get user does require an access token
301+
var users = this.client.GetUserList();
302+
foreach (var user in users)
303+
{
304+
if (user.Name != "fossy")
305+
{
306+
continue;
307+
} // if
308+
309+
var userinfo = this.client.GetUser(user.Id);
310+
log.Info($"Current user = {userinfo.Name}, email = {userinfo.Email}, description = {userinfo.Description}");
311+
} // foreach
312+
} // ShowInfo()
217313

218314
/// <summary>
219315
/// Processes the file.

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ More information about FOSSology can be found [here](https://www.fossology.org/.
4242
http://localhost:8081/repo/
4343
* Login via the admin account
4444
user = ```fossy```, pwd = ```fossy```
45-
* Go to ```admin/users/edit user account``` and create an API token or
46-
use the appropriate REST call to generate a token.
45+
* Start the demo application and create a token
4746
* Optionally create a folder and upload a package
4847

4948
## License
5049

51-
Copyright (C) 2019-2020 T. Graf
50+
Copyright (C) 2019-2022 T. Graf
5251

5352
Licensed under the **MIT License** (the "License");
5453
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)