|
1 | 1 | // ---------------------------------------------------------------------------
|
2 | 2 | // <copyright file="MainForm.cs" company="Tethys">
|
3 |
| -// Copyright (C) 2019 T. Graf |
| 3 | +// Copyright (C) 2019-2022 T. Graf |
4 | 4 | // </copyright>
|
5 | 5 | //
|
6 | 6 | // Licensed under the MIT License.
|
@@ -118,7 +118,7 @@ private void MainFormLoad(object sender, EventArgs e)
|
118 | 118 | {
|
119 | 119 | #if DEBUG
|
120 | 120 | 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"; |
122 | 122 | this.txtFolder.Text = "TestFolder";
|
123 | 123 | this.txtFile.Text = @"..\..\..\TestData\fetch-retry-master.zip";
|
124 | 124 | #endif
|
@@ -147,27 +147,74 @@ private void BtnBrowseUploadClick(object sender, EventArgs e)
|
147 | 147 | } // BtnBrowseUploadClick()
|
148 | 148 |
|
149 | 149 | /// <summary>
|
150 |
| - /// Handles the Click event of the <c>btnUpload</c> control. |
| 150 | + /// Handles the Click event of the btnGetToken control. |
151 | 151 | /// </summary>
|
152 | 152 | /// <param name="sender">The source of the event.</param>
|
153 | 153 | /// <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) |
155 | 155 | {
|
156 |
| - if (!this.CheckFolder()) |
| 156 | + try |
157 | 157 | {
|
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); |
159 | 169 | } // if
|
| 170 | + } // BtnGetTokenClick() |
160 | 171 |
|
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 |
162 | 180 | {
|
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); |
164 | 186 | } // if
|
| 187 | + } // BtnShowInfoClick() |
165 | 188 |
|
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 |
169 | 197 | {
|
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); |
171 | 218 | } // if
|
172 | 219 | } // BtnUploadClick()
|
173 | 220 |
|
@@ -214,6 +261,55 @@ private void MainFormDragDrop(object sender, DragEventArgs e)
|
214 | 261 | //// ---------------------------------------------------------------------
|
215 | 262 |
|
216 | 263 | #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() |
217 | 313 |
|
218 | 314 | /// <summary>
|
219 | 315 | /// Processes the file.
|
|
0 commit comments