From 18cbe5c555dc8f3e39a9d8d2b24df494aeb2feb4 Mon Sep 17 00:00:00 2001 From: DGO Date: Tue, 16 Apr 2019 13:56:48 +0200 Subject: [PATCH] Tying/not typing in backshlash shouldn't break api. Exceptions can be thrown by different part of JenkinsApi than expected. --- src/Soloplan.WhatsON.Jenkins/JenkinsAPI.cs | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Soloplan.WhatsON.Jenkins/JenkinsAPI.cs b/src/Soloplan.WhatsON.Jenkins/JenkinsAPI.cs index 20168cd..7e357e4 100644 --- a/src/Soloplan.WhatsON.Jenkins/JenkinsAPI.cs +++ b/src/Soloplan.WhatsON.Jenkins/JenkinsAPI.cs @@ -18,7 +18,7 @@ public async Task GetJenkinsJob(JenkinsProject subject, Cancellation var port = subject.GetPort(); var projectName = subject.GetProject(); - var jobRequest = $"{address}:{port}/job/{projectName}/api/json?tree={JenkinsJob.RequestProperties}"; + var jobRequest = $"{address.Trim('/')}:{port}/job/{projectName.Trim('/')}/api/json?tree={JenkinsJob.RequestProperties}"; return await GetJenkinsModel(subject, jobRequest, token); } @@ -28,17 +28,18 @@ public async Task GetJenkinsBuild(JenkinsProject subject, int buil var port = subject.GetPort(); var projectName = subject.GetProject(); - var buildRequest = $"{address}:{port}/job/{projectName}/{buildNumber}/api/json?tree={JenkinsBuild.RequestProperties}"; + var buildRequest = $"{address.Trim('/')}:{port}/job/{projectName.Trim('/')}/{buildNumber}/api/json?tree={JenkinsBuild.RequestProperties}"; return await GetJenkinsModel(subject, buildRequest, token); } private static async Task GetJenkinsModel(JenkinsProject subject, string requestUrl, CancellationToken token) + where TModel : class { var request = WebRequest.Create(requestUrl); - using (token.Register(() => request.Abort(), false)) - using (var response = await request.GetResponseAsync()) + try { - try + using (token.Register(() => request.Abort(), false)) + using (var response = await request.GetResponseAsync()) { // Get the stream containing content returned by the server // Open the stream using a StreamReader for easy access @@ -49,15 +50,15 @@ private static async Task GetJenkinsModel(JenkinsProject subject return JsonConvert.DeserializeObject(responseFromServer); } } - catch (WebException ex) + } + catch (WebException ex) + { + if (token.IsCancellationRequested) { - if (token.IsCancellationRequested) - { - throw new OperationCanceledException(ex.Message, ex, token); - } - - throw; + throw new OperationCanceledException(ex.Message, ex, token); } + + throw; } } }