Compare commits

..

1 commit

Author SHA1 Message Date
Mira
0b60f2e32d
chore: Update to net9 2025-01-27 00:02:49 +01:00
2 changed files with 175 additions and 164 deletions

View file

@ -3,10 +3,9 @@ using System.Net.Http;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.IO; using System.IO;
using System.Text.Json.Serialization;
namespace QuickChart;
namespace QuickChart
{
internal class QuickChartShortUrlResponse internal class QuickChartShortUrlResponse
{ {
#pragma warning disable IDE1006 // Naming Styles #pragma warning disable IDE1006 // Naming Styles
@ -30,92 +29,104 @@ public class Chart
public string Host { get; set; } public string Host { get; set; }
public int Port { get; set; } public int Port { get; set; }
private static readonly HttpClient Client = new(); private static readonly HttpClient Client = new HttpClient();
public Chart(string scheme = null, string host = null, int? port = null) public Chart(string scheme = null, string host = null, int? port = null)
{ {
this.Width = 500; Width = 500;
this.Height = 300; Height = 300;
this.DevicePixelRatio = 1.0; DevicePixelRatio = 1.0;
this.Format = "png"; Format = "png";
this.BackgroundColor = "transparent"; BackgroundColor = "transparent";
if (host != null) if (host != null)
{ {
this.Host = host; Host = host;
if (scheme != null) if (scheme != null)
{ {
this.Scheme = scheme; Scheme = scheme;
this.Port = port ?? (scheme == "http" ? 80 : 443); if (port.HasValue)
{
Port = port.Value;
} }
else else
{ {
this.Scheme = "https"; if(scheme == "http")
this.Port = 443; {
Port = 80;
}
else
{
Port = 443;
}
} }
} }
else else
{ {
this.Scheme = "https"; Scheme = "https";
this.Host = "quickchart.io"; Port = 443;
this.Port = 443; }
}
else
{
Scheme = "https";
Host = "quickchart.io";
Port = 443;
} }
} }
public string GetUrl() public string GetUrl()
{ {
if (this.Config == null) if (Config == null)
{ {
throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL");
} }
var builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
_ = builder.Append("w=").Append(this.Width); builder.Append("w=").Append(Width.ToString());
_ = builder.Append("&h=").Append(this.Height); builder.Append("&h=").Append(Height.ToString());
_ = builder.Append("&devicePixelRatio=").Append(this.DevicePixelRatio); builder.Append("&devicePixelRatio=").Append(DevicePixelRatio.ToString());
_ = builder.Append("&f=").Append(this.Format); builder.Append("&f=").Append(Format);
_ = builder.Append("&bkg=").Append(Uri.EscapeDataString(this.BackgroundColor)); builder.Append("&bkg=").Append(Uri.EscapeDataString(BackgroundColor));
_ = builder.Append("&c=").Append(Uri.EscapeDataString(this.Config)); builder.Append("&c=").Append(Uri.EscapeDataString(Config));
if (!string.IsNullOrEmpty(this.Key)) if (!string.IsNullOrEmpty(Key))
{ {
_ = builder.Append("&key=").Append(Uri.EscapeDataString(this.Key)); builder.Append("&key=").Append(Uri.EscapeDataString(Key));
} }
if (!string.IsNullOrEmpty(this.Version)) if (!string.IsNullOrEmpty(Version))
{ {
_ = builder.Append("&v=").Append(Uri.EscapeDataString(this.Version)); builder.Append("&v=").Append(Uri.EscapeDataString(Version));
} }
return $"{this.Scheme}://{this.Host}:{this.Port}/chart?{builder}"; return $"{Scheme}://{Host}:{Port}/chart?{builder}";
} }
public string GetShortUrl() public string GetShortUrl()
{ {
if (this.Config == null) if (Config == null)
{ {
throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL");
} }
var options = new JsonSerializerOptions JsonSerializerOptions options = new JsonSerializerOptions();
{ options.IgnoreNullValues = true;
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var json = JsonSerializer.Serialize(new String json = JsonSerializer.Serialize(new
{ {
width = this.Width, width = Width,
height = this.Height, height = Height,
backgroundColor = this.BackgroundColor, backgroundColor = BackgroundColor,
devicePixelRatio = this.DevicePixelRatio, devicePixelRatio = DevicePixelRatio,
format = this.Format, format = Format,
chart = this.Config, chart = Config,
key = this.Key, key = Key,
version = this.Version, version = Version,
}, options); }, options);
var url = $"{this.Scheme}://{this.Host}:{this.Port}/chart/create"; string url = $"{Scheme}://{Host}:{Port}/chart/create";
var response = Client.PostAsync( HttpResponseMessage response = Client.PostAsync(
url, url,
new StringContent(json, Encoding.UTF8, "application/json") new StringContent(json, Encoding.UTF8, "application/json")
).Result; ).Result;
@ -125,49 +136,52 @@ public class Chart
throw new ApiException("Unsuccessful response from API", response); throw new ApiException("Unsuccessful response from API", response);
} }
var responseText = response.Content.ReadAsStringAsync().Result; string responseText = response.Content.ReadAsStringAsync().Result;
var result = JsonSerializer.Deserialize<QuickChartShortUrlResponse>(responseText); QuickChartShortUrlResponse result = JsonSerializer.Deserialize<QuickChartShortUrlResponse>(responseText);
return result.url; return result.url;
} }
public byte[] ToByteArray() public byte[] ToByteArray()
{ {
if (this.Config == null) if (Config == null)
{ {
throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL");
} }
var options = new JsonSerializerOptions JsonSerializerOptions options = new JsonSerializerOptions();
{ options.IgnoreNullValues = true;
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var json = JsonSerializer.Serialize(new String json = JsonSerializer.Serialize(new
{ {
width = this.Width, width = Width,
height = this.Height, height = Height,
backgroundColor = this.BackgroundColor, backgroundColor = BackgroundColor,
devicePixelRatio = this.DevicePixelRatio, devicePixelRatio = DevicePixelRatio,
format = this.Format, format = Format,
chart = this.Config, chart = Config,
key = this.Key, key = Key,
version = this.Version, version = Version,
}, options); }, options);
var url = $"{this.Scheme}://{this.Host}:{this.Port}/chart"; string url = $"{Scheme}://{Host}:{Port}/chart";
var response = Client.PostAsync( HttpResponseMessage response = Client.PostAsync(
url, url,
new StringContent(json, Encoding.UTF8, "application/json") new StringContent(json, Encoding.UTF8, "application/json")
).Result; ).Result;
return !response.IsSuccessStatusCode if (!response.IsSuccessStatusCode)
? throw new ApiException("Unsuccessful response from API", response) {
: response.Content.ReadAsByteArrayAsync().Result; throw new ApiException("Unsuccessful response from API", response);
}
return response.Content.ReadAsByteArrayAsync().Result;
} }
public void ToFile(string filePath) public void ToFile(string filePath)
=> File.WriteAllBytes(filePath, this.ToByteArray()); {
File.WriteAllBytes(filePath, this.ToByteArray());
}
} }
public class ApiException : Exception public class ApiException : Exception
@ -177,3 +191,4 @@ public class ApiException : Exception
public HttpResponseMessage Response { get; private set; } public HttpResponseMessage Response { get; private set; }
} }
}

View file

@ -2,27 +2,23 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFrameworks>net9.0</TargetFrameworks>
<PlatformTarget>x64</PlatformTarget>
<Configurations>Debug;Release;x64</Configurations>
<Platforms>AnyCPU;x64</Platforms>
<PackOnBuild>true</PackOnBuild> <PackOnBuild>true</PackOnBuild>
<Authors>Ian Webster, TheXorog</Authors> <Authors>Ian Webster</Authors>
<Copyright>Ian Webster, TheXorog</Copyright> <Copyright>Ian Webster</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<Owners>QuickChart</Owners> <Owners>QuickChart</Owners>
<PackageProjectUrl>https://github.com/TheXorog/quickchart-csharp</PackageProjectUrl> <PackageProjectUrl>https://github.com/typpo/quickchart-csharp</PackageProjectUrl>
<Summary>Client library for the QuickChart Chart API, used to generate chart images.</Summary> <Summary>Client library for the QuickChart Chart API, used to generate chart images.</Summary>
<PackageTags>chart image, chart api, chart, charts, image charts</PackageTags> <PackageTags>chart image, chart api, chart, charts, image charts</PackageTags>
<Title>QuickChart</Title> <Title>QuickChart</Title>
<Description>Client library for the QuickChart Chart API, used to generate chart images.</Description> <Description>Client library for the QuickChart Chart API, used to generate chart images.</Description>
<PackageVersion>2.3.0</PackageVersion> <PackageVersion>2.3.0</PackageVersion>
<PackageId>QuickChart</PackageId> <PackageId>QuickChart</PackageId>
<RunAnalyzersDuringBuild>False</RunAnalyzersDuringBuild>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Text.Json" Version="7.0.3" /> <PackageReference Include="System.Text.Json" Version="5.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" /> <PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup> </ItemGroup>
</Project> </Project>