From ee07affbbd1edd4fc0b4ab172641e529820b527e Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 11 Apr 2022 01:48:43 +0100 Subject: [PATCH] ... --- .gitignore | 2 ++ README.md | 3 +- isnd.session.sql | 0 src/isn.abst/isn.abst.csproj | 2 +- src/isn/Program.cs | 5 ++- src/isn/SourceHelpers.cs | 49 ++++++++++++++++++++++++--- src/isn/{ => commands}/PushCommand.cs | 7 ++-- src/isn/commands/push.cs | 2 +- src/isn/commands/set-api-key.cs | 8 +++-- src/isn/isn.csproj | 2 +- src/isn/{ => model}/PushReport.cs | 0 test/isn.tests/UnitTest1.cs | 36 ++++++++++++++++++++ test/isn.tests/isn.tests.csproj | 3 +- 13 files changed, 102 insertions(+), 17 deletions(-) delete mode 100644 isnd.session.sql rename src/isn/{ => commands}/PushCommand.cs (91%) rename src/isn/{ => model}/PushReport.cs (100%) diff --git a/.gitignore b/.gitignore index fde2aa0..d722330 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ test/isn.tests/obj/ test/isnd.tests/appsettings.Testing.json wwwroot/.sass-cache/ src/isnd/wwwroot/.sass-cache/ +src/isn.abst/bin +src/isn.abst/obj diff --git a/README.md b/README.md index 415dbec..3468df2 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,8 @@ sudo ln -s /usr/local/lib/isn/isn /usr/local/bin/isn ````bash # compiler tout -dotnet publish -c Release +dotnet build -c Release +dotnet publish -c Release -f netcoreapp2.1 src/isnd # MAJ du serveur sudo systemctl stop isnd sudo cp -a src/isnd/bin/Release/netcoreapp2.1/publish/* /srv/www/isnd diff --git a/isnd.session.sql b/isnd.session.sql deleted file mode 100644 index e69de29..0000000 diff --git a/src/isn.abst/isn.abst.csproj b/src/isn.abst/isn.abst.csproj index 8f24508..ae0f314 100644 --- a/src/isn.abst/isn.abst.csproj +++ b/src/isn.abst/isn.abst.csproj @@ -1,7 +1,7 @@ - net4.7.2 + net45 diff --git a/src/isn/Program.cs b/src/isn/Program.cs index 5058f22..37b623d 100644 --- a/src/isn/Program.cs +++ b/src/isn/Program.cs @@ -7,7 +7,7 @@ using Newtonsoft.Json; namespace isn { - partial class Program + public partial class Program { public static IEnumerable Sources { get; protected set; } @@ -155,7 +155,6 @@ namespace isn var pushCmd = new Command(push) { - Run = async sargs => { var pargs = pushoptions.Parse(sargs); @@ -172,11 +171,11 @@ namespace isn } }; - commandSet.Add(pushCmd); var setapikey = new Command("set-api-key") { Run = sargs => StoreApiKey(sargs) }; + commandSet.Add(pushCmd); commandSet.Add(setapikey); commandSet.Add(srcCmd); commandSet.Add(showCommand); diff --git a/src/isn/SourceHelpers.cs b/src/isn/SourceHelpers.cs index 0202937..5b568fa 100644 --- a/src/isn/SourceHelpers.cs +++ b/src/isn/SourceHelpers.cs @@ -1,3 +1,6 @@ +using System; +using System.IO; +using System.Net; using System.Net.Http; using System.Threading.Tasks; using isn.Abstract; @@ -10,12 +13,50 @@ namespace isn public static async Task GetServerResourcesAsync(string url) { HttpClient client = new HttpClient(); - client.DefaultRequestHeaders.Add("content-type","application/json; utf-8"); - using (var indexResponse = await client.GetAsync(url)) + // var json = await client.GetStringAsync(new System.Uri(url)); + try { - var json = await indexResponse.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(json); + var response = await client.GetStringAsync(url); + // var json = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject(response); + + } + catch(Exception ex) + { + throw; + } + } + + public static async Task GetServerResourcesUsingWebRequestAsync(string url) + { + ApiIndexViewModel viewModel=null; + var uri = new Uri(url); + + HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); + httpWebRequest.Method = "GET"; + httpWebRequest.AllowAutoRedirect = false; + try{ + + using (var resp = await httpWebRequest.GetResponseAsync()) + { + using (var stream = resp.GetResponseStream()) + { + using (var reader = new StreamReader(stream)) + { + var json = await reader.ReadToEndAsync(); + Console.Write("got json : "+json); + viewModel = JsonConvert.DeserializeObject(json); + + } + } + + } + } + catch(Exception ex) + { + Console.Error.WriteLine(ex.Message); } + return viewModel; } } } \ No newline at end of file diff --git a/src/isn/PushCommand.cs b/src/isn/commands/PushCommand.cs similarity index 91% rename from src/isn/PushCommand.cs rename to src/isn/commands/PushCommand.cs index 7c61b39..794db2a 100644 --- a/src/isn/PushCommand.cs +++ b/src/isn/commands/PushCommand.cs @@ -7,9 +7,9 @@ using Newtonsoft.Json; namespace isn { - internal class PushCommand + public class PushCommand { - static internal async Task RunAsync(string pkg, string source, string apikey) + static public async Task RunAsync(string pkg, string source, string apikey) { FileInfo fi = new FileInfo(pkg); var report = new PushReport @@ -39,7 +39,8 @@ namespace isn var resources = await SourceHelpers.GetServerResourcesAsync(source); if (resources.Resources==null || resources.Resources.Any(res => res.Id == "" )) throw new InvalidOperationException("Source won't serve the expected push command"); - wrqueryHandler.UploadFilesToServer(report, new Uri(source), fi, apikey); + Console.WriteLine(JsonConvert.SerializeObject(resources)); + wrqueryHandler.UploadFilesToServer(report, new Uri(resources.Resources[0].Id), fi, apikey); } catch (WebException ex) { diff --git a/src/isn/commands/push.cs b/src/isn/commands/push.cs index 906ca5c..abe021f 100644 --- a/src/isn/commands/push.cs +++ b/src/isn/commands/push.cs @@ -9,7 +9,7 @@ namespace isn partial class Program { - private static async Task> PushPkgAsync(IEnumerable pkgs) + public static async Task> PushPkgAsync(IEnumerable pkgs) { List pushReports = new List(); diff --git a/src/isn/commands/set-api-key.cs b/src/isn/commands/set-api-key.cs index 364eb82..02d651b 100644 --- a/src/isn/commands/set-api-key.cs +++ b/src/isn/commands/set-api-key.cs @@ -28,7 +28,12 @@ namespace isn public static void EnsureKeyStored() { - if (source == null) return; + if (source == null) + { + if (Settings.DefaultSource == null) + return; + source = Settings.DefaultSource; + } if (Settings.Sources.ContainsKey(source)) { @@ -52,7 +57,6 @@ namespace isn string ptd = Protector.Protect(apiKey); Settings.Sources.Add(source, new SourceSettings { ApiKey = ptd }); } - else return; SaveConfig(); } public static void SaveConfig() diff --git a/src/isn/isn.csproj b/src/isn/isn.csproj index 62b17c7..48cbcb6 100644 --- a/src/isn/isn.csproj +++ b/src/isn/isn.csproj @@ -2,7 +2,7 @@ Exe - net6 + net45;netcoreapp2.1;net6 nuget_cli 45b74c62-05bc-4603-95b4-3e80ae2fdf50 1.0.1 diff --git a/src/isn/PushReport.cs b/src/isn/model/PushReport.cs similarity index 100% rename from src/isn/PushReport.cs rename to src/isn/model/PushReport.cs diff --git a/test/isn.tests/UnitTest1.cs b/test/isn.tests/UnitTest1.cs index 92bb565..03720c8 100644 --- a/test/isn.tests/UnitTest1.cs +++ b/test/isn.tests/UnitTest1.cs @@ -2,7 +2,13 @@ using System; using System.Data; using System.IO; using System.Xml; +using System.Net.Http; + using NUnit.Framework; +using System.Threading.Tasks; +using Newtonsoft.Json; +using isn.Abstract; +using System.Linq; namespace isn.tests { @@ -35,5 +41,35 @@ dataRow[1]= 2; dataTable.Rows.Add(dataRow); } + + [Test] + public async Task TestHttpClient() + { + string url = "http://localhost:88/index.json"; + HttpClient client = new HttpClient(); + // var json = await client.GetStringAsync(new System.Uri(url)); + var response = await client.GetAsync(url); + var json = await response.Content.ReadAsStringAsync(); + var vm = JsonConvert.DeserializeObject(json); + Console.WriteLine( JsonConvert.SerializeObject(vm)); + Assert.NotNull(vm); + Assert.NotNull(vm.Resources); + } + [Test] + public async Task TestPush() + { + Program.LoadConfig(); + var report = await Program.PushPkgAsync(new string[] { "bin/Debug/isn.1.0.1.nupkg" }); + } + + [Test] + public async Task GetServerResourcesUsingWebRequestAsyncTest() + { + var model = await SourceHelpers.GetServerResourcesUsingWebRequestAsync("Http://localhost:88/index.json"); + Console.WriteLine(JsonConvert.SerializeObject(model)); + Assert.NotNull(model.Resources); + Assert.True(model.Resources.Any((r) => r.Type == "PackagePublish/2.0.0")); + } + } } \ No newline at end of file diff --git a/test/isn.tests/isn.tests.csproj b/test/isn.tests/isn.tests.csproj index e5bb5fd..87f70d9 100644 --- a/test/isn.tests/isn.tests.csproj +++ b/test/isn.tests/isn.tests.csproj @@ -1,7 +1,7 @@ - net6 + net45 false @@ -15,6 +15,7 @@ +