Initial commit
This commit is contained in:
commit
79649a1628
8 changed files with 681 additions and 0 deletions
136
QuickChart/QuickChart.cs
Normal file
136
QuickChart/QuickChart.cs
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Web;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.IO;
|
||||
|
||||
namespace QuickChart
|
||||
{
|
||||
internal class QuickChartShortUrlResponse
|
||||
{
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public bool status { get; set; }
|
||||
public string url { get; set; }
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
}
|
||||
|
||||
public class Chart
|
||||
{
|
||||
public int Width { get; set; }
|
||||
public int Height { get; set; }
|
||||
public double DevicePixelRatio { get; set; }
|
||||
public string BackgroundColor { get; set; }
|
||||
public string Key { get; set; }
|
||||
public string Config { get; set; }
|
||||
|
||||
public string Protocol { get; set; }
|
||||
public string Host { get; set; }
|
||||
public int Port { get; set; }
|
||||
|
||||
private static readonly HttpClient Client = new HttpClient();
|
||||
|
||||
public Chart()
|
||||
{
|
||||
Width = 500;
|
||||
Height = 300;
|
||||
DevicePixelRatio = 1.0;
|
||||
BackgroundColor = "transparent";
|
||||
|
||||
Protocol = "https";
|
||||
Host = "quickchart.io";
|
||||
Port = 443;
|
||||
}
|
||||
|
||||
public string GetUrl()
|
||||
{
|
||||
if (Config == null)
|
||||
{
|
||||
throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL");
|
||||
}
|
||||
|
||||
NameValueCollection queryString = HttpUtility.ParseQueryString(string.Empty);
|
||||
queryString.Add("w", Width.ToString());
|
||||
queryString.Add("h", Height.ToString());
|
||||
queryString.Add("devicePixelRatio", DevicePixelRatio.ToString());
|
||||
queryString.Add("bkg", BackgroundColor);
|
||||
queryString.Add("c", Config);
|
||||
|
||||
return $"{Protocol}://{Host}:{Port}/chart?{queryString}";
|
||||
}
|
||||
|
||||
public string GetShortUrl()
|
||||
{
|
||||
if (Config == null)
|
||||
{
|
||||
throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL");
|
||||
}
|
||||
|
||||
String json = JsonSerializer.Serialize(new
|
||||
{
|
||||
width = Width,
|
||||
height = Height,
|
||||
backgroundColor = BackgroundColor,
|
||||
devicePixelRatio = DevicePixelRatio,
|
||||
chart = Config,
|
||||
});
|
||||
|
||||
// TODO(ian): key
|
||||
|
||||
string url = $"{Protocol}://{Host}:{Port}/chart/create";
|
||||
|
||||
HttpResponseMessage response = Client.PostAsync(
|
||||
url,
|
||||
new StringContent(json, Encoding.UTF8, "application/json")
|
||||
).Result;
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
throw new Exception("Unsuccessful response from API");
|
||||
}
|
||||
|
||||
string responseText = response.Content.ReadAsStringAsync().Result;
|
||||
QuickChartShortUrlResponse result = JsonSerializer.Deserialize<QuickChartShortUrlResponse>(responseText);
|
||||
return result.url;
|
||||
}
|
||||
|
||||
public byte[] ToByteArray()
|
||||
{
|
||||
if (Config == null)
|
||||
{
|
||||
throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL");
|
||||
}
|
||||
|
||||
String json = JsonSerializer.Serialize(new
|
||||
{
|
||||
width = Width,
|
||||
height = Height,
|
||||
backgroundColor = BackgroundColor,
|
||||
devicePixelRatio = DevicePixelRatio,
|
||||
chart = Config,
|
||||
});
|
||||
|
||||
// TODO(ian): key
|
||||
|
||||
string url = $"{Protocol}://{Host}:{Port}/chart";
|
||||
|
||||
HttpResponseMessage response = Client.PostAsync(
|
||||
url,
|
||||
new StringContent(json, Encoding.UTF8, "application/json")
|
||||
).Result;
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
throw new Exception("Unsuccessful response from API");
|
||||
}
|
||||
|
||||
return response.Content.ReadAsByteArrayAsync().Result;
|
||||
}
|
||||
|
||||
public void ToFile(string filePath)
|
||||
{
|
||||
File.WriteAllBytes(filePath, this.ToByteArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
8
QuickChart/QuickChart.csproj
Normal file
8
QuickChart/QuickChart.csproj
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue