Compare commits
1 commit
applied-ed
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b60f2e32d |
2 changed files with 175 additions and 164 deletions
|
|
@ -3,20 +3,19 @@ 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
|
||||
{
|
||||
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 class Chart
|
||||
{
|
||||
public int Width { get; set; }
|
||||
public int Height { get; set; }
|
||||
public double DevicePixelRatio { get; set; }
|
||||
|
|
@ -30,92 +29,104 @@ public class Chart
|
|||
public string Host { 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)
|
||||
{
|
||||
this.Width = 500;
|
||||
this.Height = 300;
|
||||
this.DevicePixelRatio = 1.0;
|
||||
this.Format = "png";
|
||||
this.BackgroundColor = "transparent";
|
||||
Width = 500;
|
||||
Height = 300;
|
||||
DevicePixelRatio = 1.0;
|
||||
Format = "png";
|
||||
BackgroundColor = "transparent";
|
||||
|
||||
if (host != null)
|
||||
{
|
||||
this.Host = host;
|
||||
Host = host;
|
||||
if (scheme != null)
|
||||
{
|
||||
this.Scheme = scheme;
|
||||
this.Port = port ?? (scheme == "http" ? 80 : 443);
|
||||
Scheme = scheme;
|
||||
if (port.HasValue)
|
||||
{
|
||||
Port = port.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Scheme = "https";
|
||||
this.Port = 443;
|
||||
if(scheme == "http")
|
||||
{
|
||||
Port = 80;
|
||||
}
|
||||
else
|
||||
{
|
||||
Port = 443;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Scheme = "https";
|
||||
this.Host = "quickchart.io";
|
||||
this.Port = 443;
|
||||
Scheme = "https";
|
||||
Port = 443;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Scheme = "https";
|
||||
Host = "quickchart.io";
|
||||
Port = 443;
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
var builder = new StringBuilder();
|
||||
_ = builder.Append("w=").Append(this.Width);
|
||||
_ = builder.Append("&h=").Append(this.Height);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.Append("w=").Append(Width.ToString());
|
||||
builder.Append("&h=").Append(Height.ToString());
|
||||
|
||||
_ = 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("&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(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()
|
||||
{
|
||||
if (this.Config == null)
|
||||
if (Config == null)
|
||||
{
|
||||
throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL");
|
||||
}
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||
};
|
||||
JsonSerializerOptions options = new JsonSerializerOptions();
|
||||
options.IgnoreNullValues = true;
|
||||
|
||||
var json = JsonSerializer.Serialize(new
|
||||
String 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,
|
||||
width = Width,
|
||||
height = Height,
|
||||
backgroundColor = BackgroundColor,
|
||||
devicePixelRatio = DevicePixelRatio,
|
||||
format = Format,
|
||||
chart = Config,
|
||||
key = Key,
|
||||
version = Version,
|
||||
}, 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,
|
||||
new StringContent(json, Encoding.UTF8, "application/json")
|
||||
).Result;
|
||||
|
|
@ -125,55 +136,59 @@ public class Chart
|
|||
throw new ApiException("Unsuccessful response from API", response);
|
||||
}
|
||||
|
||||
var responseText = response.Content.ReadAsStringAsync().Result;
|
||||
var result = JsonSerializer.Deserialize<QuickChartShortUrlResponse>(responseText);
|
||||
string responseText = response.Content.ReadAsStringAsync().Result;
|
||||
QuickChartShortUrlResponse result = JsonSerializer.Deserialize<QuickChartShortUrlResponse>(responseText);
|
||||
return result.url;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||
};
|
||||
JsonSerializerOptions options = new JsonSerializerOptions();
|
||||
options.IgnoreNullValues = true;
|
||||
|
||||
var json = JsonSerializer.Serialize(new
|
||||
String 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,
|
||||
width = Width,
|
||||
height = Height,
|
||||
backgroundColor = BackgroundColor,
|
||||
devicePixelRatio = DevicePixelRatio,
|
||||
format = Format,
|
||||
chart = Config,
|
||||
key = Key,
|
||||
version = Version,
|
||||
}, 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,
|
||||
new StringContent(json, Encoding.UTF8, "application/json")
|
||||
).Result;
|
||||
|
||||
return !response.IsSuccessStatusCode
|
||||
? throw new ApiException("Unsuccessful response from API", response)
|
||||
: response.Content.ReadAsByteArrayAsync().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());
|
||||
}
|
||||
{
|
||||
File.WriteAllBytes(filePath, this.ToByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
public class ApiException : Exception
|
||||
{
|
||||
public class ApiException : Exception
|
||||
{
|
||||
public ApiException(string message, HttpResponseMessage response) : base(message)
|
||||
=> this.Response = response;
|
||||
|
||||
public HttpResponseMessage Response { get; private set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,27 +2,23 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<Configurations>Debug;Release;x64</Configurations>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
<TargetFrameworks>net9.0</TargetFrameworks>
|
||||
<PackOnBuild>true</PackOnBuild>
|
||||
<Authors>Ian Webster, TheXorog</Authors>
|
||||
<Copyright>Ian Webster, TheXorog</Copyright>
|
||||
<Authors>Ian Webster</Authors>
|
||||
<Copyright>Ian Webster</Copyright>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<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>
|
||||
<PackageTags>chart image, chart api, chart, charts, image charts</PackageTags>
|
||||
<Title>QuickChart</Title>
|
||||
<Description>Client library for the QuickChart Chart API, used to generate chart images.</Description>
|
||||
<PackageVersion>2.3.0</PackageVersion>
|
||||
<PackageId>QuickChart</PackageId>
|
||||
<RunAnalyzersDuringBuild>False</RunAnalyzersDuringBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue