From 03ac2f63d673a218cebe40a0db04aa126e3dd137 Mon Sep 17 00:00:00 2001 From: dianaduca <94049431+dianaduca@users.noreply.github.com> Date: Sat, 12 Mar 2022 02:11:32 +0000 Subject: [PATCH] Add ability to specify chart generation url for self hosted QuickChart (#5) * Add ability to specify chart generation url for self hosted QuickChart * Add more configuration setting: if user specify: a host only, default to https:443, a host and http, set port to 80 a host and https, set port to 443 Co-authored-by: Duca Diana --- QuickChart/QuickChart.cs | 39 ++++++++++++++++++++++++++++---- QuickChartTest/QuickChartTest.cs | 27 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/QuickChart/QuickChart.cs b/QuickChart/QuickChart.cs index b485bd1..93ba667 100644 --- a/QuickChart/QuickChart.cs +++ b/QuickChart/QuickChart.cs @@ -31,7 +31,7 @@ namespace QuickChart private static readonly HttpClient Client = new HttpClient(); - public Chart() + public Chart(string scheme = null, string host = null, int? port = null) { Width = 500; Height = 300; @@ -39,9 +39,40 @@ namespace QuickChart Format = "png"; BackgroundColor = "transparent"; - Scheme = "https"; - Host = "quickchart.io"; - Port = 443; + if (host != null) + { + Host = host; + if (scheme != null) + { + Scheme = scheme; + if (port.HasValue) + { + Port = port.Value; + } + else + { + if(scheme == "http") + { + Port = 80; + } + else + { + Port = 443; + } + } + } + else + { + Scheme = "https"; + Port = 443; + } + } + else + { + Scheme = "https"; + Host = "quickchart.io"; + Port = 443; + } } public string GetUrl() diff --git a/QuickChartTest/QuickChartTest.cs b/QuickChartTest/QuickChartTest.cs index 72e67ab..810a598 100644 --- a/QuickChartTest/QuickChartTest.cs +++ b/QuickChartTest/QuickChartTest.cs @@ -113,5 +113,32 @@ namespace QuickChartTest Assert.Contains("h=300", url); Assert.Contains("v=2.9.4", url); } + + [Fact] + public void TestWithSelfHostingQuickChart() + { + var scheme = "http"; + var host = "localhost"; + var port = 47000; + Chart qc = new Chart(scheme, host, port); + qc.Width = 500; + qc.Height = 300; + qc.Config = @"{ + type: 'bar', + data: { + labels: ['Q1', 'Q2', 'Q3', 'Q4'], + datasets: [{ + label: 'Users', + data: [50, 60, 70, 180] + }] + }[] + }"; + + string url = qc.GetUrl(); + Assert.Contains($"{scheme}://{host}:{port}/chart", url); + Assert.Contains("w=500", url); + Assert.Contains("h=300", url); + Assert.DoesNotContain("key=", url); + } } }