From 60e4ca8c54ddafa80d0172dd5d9903e1a783b18d Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sun, 17 Apr 2022 23:30:48 +0100 Subject: [PATCH] ex to output --- .build | 8 +-- src/isn/UploadFilesToServerUsingHttpClient.cs | 69 ++++++++++--------- src/isn/commands/PushCommand.cs | 44 +++--------- 3 files changed, 52 insertions(+), 69 deletions(-) diff --git a/.build b/.build index ca7ddf0..c03868d 100644 --- a/.build +++ b/.build @@ -16,9 +16,9 @@ - - - - + + + + diff --git a/src/isn/UploadFilesToServerUsingHttpClient.cs b/src/isn/UploadFilesToServerUsingHttpClient.cs index c68a302..ce51297 100644 --- a/src/isn/UploadFilesToServerUsingHttpClient.cs +++ b/src/isn/UploadFilesToServerUsingHttpClient.cs @@ -9,43 +9,50 @@ namespace isn { public static class UploadFilesToServerUsingHttpClient { - public static PushReport UploadFilesToServer(Uri uri, FileInfo fi, + public static PushReport UploadFilesToServer(this HttpClient client, Uri uri, FileInfo fi, + string apikey) + { + return UploadFilesToServerAsync(client, uri, fi, apikey).Result; + } + + public static async Task UploadFilesToServerAsync(this HttpClient client, Uri uri, FileInfo fi, string apikey) { - using (var client = new HttpClient()) + + client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", Constants.ClientVersion); + client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey); + + using (var multipartFormDataContent = new MultipartFormDataContent()) { - client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", Constants.ClientVersion); - client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey); - using (var multipartFormDataContent = new MultipartFormDataContent()) - { - /* var values = new[] - { - new KeyValuePair("Id", Guid.NewGuid().ToString()), - new KeyValuePair("Key", "awesome"), - new KeyValuePair("From", "khalid@home.com") - //other values - };foreach (var keyValuePair in values) - { - multipartFormDataContent.Add(new StringContent(keyValuePair.Value), - String.Format("\"{0}\"", keyValuePair.Key)); - } */ - multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(fi.FullName)), - '"' + "File" + '"', - '"' + fi.Name + '"'); + /* var values = new[] + { + new KeyValuePair("Id", Guid.NewGuid().ToString()), + new KeyValuePair("Key", "awesome"), + new KeyValuePair("From", "khalid@home.com") + //other values + };foreach (var keyValuePair in values) + { + multipartFormDataContent.Add(new StringContent(keyValuePair.Value), + String.Format("\"{0}\"", keyValuePair.Key)); + } */ + multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(fi.FullName)), + '"' + "File" + '"', + '"' + fi.Name + '"'); - var result = client.PutAsync(uri, multipartFormDataContent).Result; - result.EnsureSuccessStatusCode(); - if (result.IsSuccessStatusCode) - { - Task.Run(async ()=> - { - string report = await result.Content.ReadAsStringAsync(); - Console.WriteLine(report); - }).Wait(); + var result = await client.PutAsync(uri, multipartFormDataContent); + + if (result.IsSuccessStatusCode) + { + string report = await result.Content.ReadAsStringAsync(); + Console.WriteLine(report); - } - return new PushReport(); } + else + { + string ereport = await result.Content.ReadAsStringAsync(); + Console.WriteLine(ereport); + } + return new PushReport(); } } } diff --git a/src/isn/commands/PushCommand.cs b/src/isn/commands/PushCommand.cs index 8ff7a0f..44ab15c 100644 --- a/src/isn/commands/PushCommand.cs +++ b/src/isn/commands/PushCommand.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Linq; using System.Net; +using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json; @@ -30,47 +31,22 @@ namespace isn }; return report; } - + using (var client = new HttpClient()) try { Console.WriteLine("Connecting to "+ pubRes.Id); - return UploadFilesToServerUsingHttpClient.UploadFilesToServer(new Uri(pubRes.Id), fi, apikey); + return client.UploadFilesToServer(new Uri(pubRes.Id), fi, apikey); } - catch (WebException ex) + catch (HttpRequestException hrex) { - Console.Error.WriteLine(ex.Message); var report = new PushReport { - PkgName = fi.Name + PkgName = fi.Name, + Message = "HttpRequest: " + hrex.Message, + StackTrace = hrex.StackTrace, + StatusCode = hrex.HResult.ToString() }; - report.StatusCode = ex.Status.ToString(); - report.OK = false; - if (ex.Response != null) - { - try - { - using (Stream respStream = ex.Response.GetResponseStream()) - { - StreamReader sr = new StreamReader(respStream); - string json = sr.ReadToEnd(); - var res = JsonConvert.DeserializeObject(json); - report.Message = res.msg; - - // ecode == 1 => package already present server side. - report.AlreadyPresent = res.ecode == 1; - } - } - catch (Exception iex) - { - report.Message = iex.Message; - } - - } - - else - { - report.Message = ex.Message; - } + Console.Error.WriteLine(hrex.Message); return report; } catch (Exception ex) @@ -82,7 +58,7 @@ namespace isn StackTrace = ex.StackTrace }; Console.Error.WriteLine(ex.Message); - throw; + return report; } } }