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 <dduca@globebmg.com>
This commit is contained in:
dianaduca 2022-03-12 02:11:32 +00:00 committed by GitHub
parent 32d5ada399
commit 03ac2f63d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 4 deletions

View file

@ -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()

View file

@ -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);
}
}
}