diff --git a/QuickChart/QuickChart.cs b/QuickChart/QuickChart.cs index a914717..01d16e6 100644 --- a/QuickChart/QuickChart.cs +++ b/QuickChart/QuickChart.cs @@ -3,177 +3,192 @@ using System.Net.Http; using System.Text; using System.Text.Json; using System.IO; -using System.Text.Json.Serialization; -namespace QuickChart; - -internal class QuickChartShortUrlResponse +namespace QuickChart { -#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 Format { get; set; } - public string BackgroundColor { get; set; } - public string Key { get; set; } - public string Version { get; set; } - public string Config { get; set; } - - public string Scheme { get; set; } - public string Host { get; set; } - public int Port { get; set; } - - private static readonly HttpClient Client = new(); - - public Chart(string scheme = null, string host = null, int? port = null) + internal class QuickChartShortUrlResponse { - this.Width = 500; - this.Height = 300; - this.DevicePixelRatio = 1.0; - this.Format = "png"; - this.BackgroundColor = "transparent"; +#pragma warning disable IDE1006 // Naming Styles + public bool status { get; set; } + public string url { get; set; } +#pragma warning restore IDE1006 // Naming Styles + } - if (host != null) + public class Chart + { + public int Width { get; set; } + public int Height { get; set; } + public double DevicePixelRatio { get; set; } + public string Format { get; set; } + public string BackgroundColor { get; set; } + public string Key { get; set; } + public string Version { get; set; } + public string Config { get; set; } + + public string Scheme { get; set; } + public string Host { get; set; } + public int Port { get; set; } + + private static readonly HttpClient Client = new HttpClient(); + + public Chart(string scheme = null, string host = null, int? port = null) { - this.Host = host; - if (scheme != null) + Width = 500; + Height = 300; + DevicePixelRatio = 1.0; + Format = "png"; + BackgroundColor = "transparent"; + + if (host != null) { - this.Scheme = scheme; - this.Port = port ?? (scheme == "http" ? 80 : 443); + 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 { - this.Scheme = "https"; - this.Port = 443; + Scheme = "https"; + Host = "quickchart.io"; + Port = 443; } } - else + + public string GetUrl() { - this.Scheme = "https"; - this.Host = "quickchart.io"; - this.Port = 443; + if (Config == null) + { + throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); + } + + StringBuilder builder = new StringBuilder(); + builder.Append("w=").Append(Width.ToString()); + builder.Append("&h=").Append(Height.ToString()); + + builder.Append("&devicePixelRatio=").Append(DevicePixelRatio.ToString()); + builder.Append("&f=").Append(Format); + builder.Append("&bkg=").Append(Uri.EscapeDataString(BackgroundColor)); + builder.Append("&c=").Append(Uri.EscapeDataString(Config)); + if (!string.IsNullOrEmpty(Key)) + { + builder.Append("&key=").Append(Uri.EscapeDataString(Key)); + } + if (!string.IsNullOrEmpty(Version)) + { + builder.Append("&v=").Append(Uri.EscapeDataString(Version)); + } + + return $"{Scheme}://{Host}:{Port}/chart?{builder}"; + } + + public string GetShortUrl() + { + if (Config == null) + { + throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); + } + + JsonSerializerOptions options = new JsonSerializerOptions(); + options.IgnoreNullValues = true; + + String json = JsonSerializer.Serialize(new + { + width = Width, + height = Height, + backgroundColor = BackgroundColor, + devicePixelRatio = DevicePixelRatio, + format = Format, + chart = Config, + key = Key, + version = Version, + }, options); + + string url = $"{Scheme}://{Host}:{Port}/chart/create"; + + HttpResponseMessage response = Client.PostAsync( + url, + new StringContent(json, Encoding.UTF8, "application/json") + ).Result; + + if (!response.IsSuccessStatusCode) + { + throw new ApiException("Unsuccessful response from API", response); + } + + string responseText = response.Content.ReadAsStringAsync().Result; + QuickChartShortUrlResponse result = JsonSerializer.Deserialize(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"); + } + + JsonSerializerOptions options = new JsonSerializerOptions(); + options.IgnoreNullValues = true; + + String json = JsonSerializer.Serialize(new + { + width = Width, + height = Height, + backgroundColor = BackgroundColor, + devicePixelRatio = DevicePixelRatio, + format = Format, + chart = Config, + key = Key, + version = Version, + }, options); + + string url = $"{Scheme}://{Host}:{Port}/chart"; + + HttpResponseMessage response = Client.PostAsync( + url, + new StringContent(json, Encoding.UTF8, "application/json") + ).Result; + + if (!response.IsSuccessStatusCode) + { + throw new ApiException("Unsuccessful response from API", response); + } + + return response.Content.ReadAsByteArrayAsync().Result; + } + + public void ToFile(string filePath) + { + File.WriteAllBytes(filePath, this.ToByteArray()); } } - public string GetUrl() + public class ApiException : Exception { - if (this.Config == null) - { - throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); - } + public ApiException(string message, HttpResponseMessage response) : base(message) + => this.Response = response; - var builder = new StringBuilder(); - _ = builder.Append("w=").Append(this.Width); - _ = builder.Append("&h=").Append(this.Height); - - _ = builder.Append("&devicePixelRatio=").Append(this.DevicePixelRatio); - _ = builder.Append("&f=").Append(this.Format); - _ = builder.Append("&bkg=").Append(Uri.EscapeDataString(this.BackgroundColor)); - _ = builder.Append("&c=").Append(Uri.EscapeDataString(this.Config)); - if (!string.IsNullOrEmpty(this.Key)) - { - _ = builder.Append("&key=").Append(Uri.EscapeDataString(this.Key)); - } - if (!string.IsNullOrEmpty(this.Version)) - { - _ = builder.Append("&v=").Append(Uri.EscapeDataString(this.Version)); - } - - return $"{this.Scheme}://{this.Host}:{this.Port}/chart?{builder}"; + public HttpResponseMessage Response { get; private set; } } - - public string GetShortUrl() - { - if (this.Config == null) - { - throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); - } - - var options = new JsonSerializerOptions - { - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull - }; - - var json = JsonSerializer.Serialize(new - { - width = this.Width, - height = this.Height, - backgroundColor = this.BackgroundColor, - devicePixelRatio = this.DevicePixelRatio, - format = this.Format, - chart = this.Config, - key = this.Key, - version = this.Version, - }, options); - - var url = $"{this.Scheme}://{this.Host}:{this.Port}/chart/create"; - - var response = Client.PostAsync( - url, - new StringContent(json, Encoding.UTF8, "application/json") - ).Result; - - if (!response.IsSuccessStatusCode) - { - throw new ApiException("Unsuccessful response from API", response); - } - - var responseText = response.Content.ReadAsStringAsync().Result; - var result = JsonSerializer.Deserialize(responseText); - return result.url; - } - - public byte[] ToByteArray() - { - if (this.Config == null) - { - throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); - } - - var options = new JsonSerializerOptions - { - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull - }; - - var json = JsonSerializer.Serialize(new - { - width = this.Width, - height = this.Height, - backgroundColor = this.BackgroundColor, - devicePixelRatio = this.DevicePixelRatio, - format = this.Format, - chart = this.Config, - key = this.Key, - version = this.Version, - }, options); - - var url = $"{this.Scheme}://{this.Host}:{this.Port}/chart"; - - var response = Client.PostAsync( - url, - new StringContent(json, Encoding.UTF8, "application/json") - ).Result; - - return !response.IsSuccessStatusCode - ? throw new ApiException("Unsuccessful response from API", response) - : response.Content.ReadAsByteArrayAsync().Result; - } - - public void ToFile(string filePath) - => File.WriteAllBytes(filePath, this.ToByteArray()); -} - -public class ApiException : Exception -{ - public ApiException(string message, HttpResponseMessage response) : base(message) - => this.Response = response; - - public HttpResponseMessage Response { get; private set; } } diff --git a/QuickChart/QuickChart.csproj b/QuickChart/QuickChart.csproj index a0a9dd5..2e5933a 100644 --- a/QuickChart/QuickChart.csproj +++ b/QuickChart/QuickChart.csproj @@ -2,27 +2,23 @@ Library - net8.0 - x64 - Debug;Release;x64 - AnyCPU;x64 + net9.0 true - Ian Webster, TheXorog - Ian Webster, TheXorog + Ian Webster + Ian Webster MIT QuickChart - https://github.com/TheXorog/quickchart-csharp + https://github.com/typpo/quickchart-csharp Client library for the QuickChart Chart API, used to generate chart images. chart image, chart api, chart, charts, image charts QuickChart Client library for the QuickChart Chart API, used to generate chart images. 2.3.0 QuickChart - False - +