Compare commits
8 commits
main
...
applied-ed
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92a231e7c7 | ||
|
|
443c742a22 | ||
|
|
e4534c87a8 | ||
|
|
86d5577a91 | ||
|
|
33ba00a462 | ||
|
|
7ec2c8b60c | ||
|
|
bae09d2727 | ||
|
|
3cab23c592 |
2 changed files with 161 additions and 172 deletions
|
|
@ -3,9 +3,10 @@ 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
|
||||||
|
|
@ -29,104 +30,92 @@ namespace QuickChart
|
||||||
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 HttpClient();
|
private static readonly HttpClient Client = new();
|
||||||
|
|
||||||
public Chart(string scheme = null, string host = null, int? port = null)
|
public Chart(string scheme = null, string host = null, int? port = null)
|
||||||
{
|
{
|
||||||
Width = 500;
|
this.Width = 500;
|
||||||
Height = 300;
|
this.Height = 300;
|
||||||
DevicePixelRatio = 1.0;
|
this.DevicePixelRatio = 1.0;
|
||||||
Format = "png";
|
this.Format = "png";
|
||||||
BackgroundColor = "transparent";
|
this.BackgroundColor = "transparent";
|
||||||
|
|
||||||
if (host != null)
|
if (host != null)
|
||||||
{
|
{
|
||||||
Host = host;
|
this.Host = host;
|
||||||
if (scheme != null)
|
if (scheme != null)
|
||||||
{
|
{
|
||||||
Scheme = scheme;
|
this.Scheme = scheme;
|
||||||
if (port.HasValue)
|
this.Port = port ?? (scheme == "http" ? 80 : 443);
|
||||||
{
|
|
||||||
Port = port.Value;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(scheme == "http")
|
this.Scheme = "https";
|
||||||
{
|
this.Port = 443;
|
||||||
Port = 80;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Port = 443;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Scheme = "https";
|
this.Scheme = "https";
|
||||||
Port = 443;
|
this.Host = "quickchart.io";
|
||||||
}
|
this.Port = 443;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Scheme = "https";
|
|
||||||
Host = "quickchart.io";
|
|
||||||
Port = 443;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetUrl()
|
public string GetUrl()
|
||||||
{
|
{
|
||||||
if (Config == null)
|
if (this.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");
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder builder = new StringBuilder();
|
var builder = new StringBuilder();
|
||||||
builder.Append("w=").Append(Width.ToString());
|
_ = builder.Append("w=").Append(this.Width);
|
||||||
builder.Append("&h=").Append(Height.ToString());
|
_ = builder.Append("&h=").Append(this.Height);
|
||||||
|
|
||||||
builder.Append("&devicePixelRatio=").Append(DevicePixelRatio.ToString());
|
_ = builder.Append("&devicePixelRatio=").Append(this.DevicePixelRatio);
|
||||||
builder.Append("&f=").Append(Format);
|
_ = builder.Append("&f=").Append(this.Format);
|
||||||
builder.Append("&bkg=").Append(Uri.EscapeDataString(BackgroundColor));
|
_ = builder.Append("&bkg=").Append(Uri.EscapeDataString(this.BackgroundColor));
|
||||||
builder.Append("&c=").Append(Uri.EscapeDataString(Config));
|
_ = builder.Append("&c=").Append(Uri.EscapeDataString(this.Config));
|
||||||
if (!string.IsNullOrEmpty(Key))
|
if (!string.IsNullOrEmpty(this.Key))
|
||||||
{
|
{
|
||||||
builder.Append("&key=").Append(Uri.EscapeDataString(Key));
|
_ = builder.Append("&key=").Append(Uri.EscapeDataString(this.Key));
|
||||||
}
|
}
|
||||||
if (!string.IsNullOrEmpty(Version))
|
if (!string.IsNullOrEmpty(this.Version))
|
||||||
{
|
{
|
||||||
builder.Append("&v=").Append(Uri.EscapeDataString(Version));
|
_ = builder.Append("&v=").Append(Uri.EscapeDataString(this.Version));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $"{Scheme}://{Host}:{Port}/chart?{builder}";
|
return $"{this.Scheme}://{this.Host}:{this.Port}/chart?{builder}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetShortUrl()
|
public string GetShortUrl()
|
||||||
{
|
{
|
||||||
if (Config == null)
|
if (this.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");
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonSerializerOptions options = new JsonSerializerOptions();
|
var options = new JsonSerializerOptions
|
||||||
options.IgnoreNullValues = true;
|
|
||||||
|
|
||||||
String json = JsonSerializer.Serialize(new
|
|
||||||
{
|
{
|
||||||
width = Width,
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||||
height = Height,
|
};
|
||||||
backgroundColor = BackgroundColor,
|
|
||||||
devicePixelRatio = DevicePixelRatio,
|
var json = JsonSerializer.Serialize(new
|
||||||
format = Format,
|
{
|
||||||
chart = Config,
|
width = this.Width,
|
||||||
key = Key,
|
height = this.Height,
|
||||||
version = Version,
|
backgroundColor = this.BackgroundColor,
|
||||||
|
devicePixelRatio = this.DevicePixelRatio,
|
||||||
|
format = this.Format,
|
||||||
|
chart = this.Config,
|
||||||
|
key = this.Key,
|
||||||
|
version = this.Version,
|
||||||
}, options);
|
}, options);
|
||||||
|
|
||||||
string url = $"{Scheme}://{Host}:{Port}/chart/create";
|
var url = $"{this.Scheme}://{this.Host}:{this.Port}/chart/create";
|
||||||
|
|
||||||
HttpResponseMessage response = Client.PostAsync(
|
var response = Client.PostAsync(
|
||||||
url,
|
url,
|
||||||
new StringContent(json, Encoding.UTF8, "application/json")
|
new StringContent(json, Encoding.UTF8, "application/json")
|
||||||
).Result;
|
).Result;
|
||||||
|
|
@ -136,52 +125,49 @@ namespace QuickChart
|
||||||
throw new ApiException("Unsuccessful response from API", response);
|
throw new ApiException("Unsuccessful response from API", response);
|
||||||
}
|
}
|
||||||
|
|
||||||
string responseText = response.Content.ReadAsStringAsync().Result;
|
var responseText = response.Content.ReadAsStringAsync().Result;
|
||||||
QuickChartShortUrlResponse result = JsonSerializer.Deserialize<QuickChartShortUrlResponse>(responseText);
|
var result = JsonSerializer.Deserialize<QuickChartShortUrlResponse>(responseText);
|
||||||
return result.url;
|
return result.url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] ToByteArray()
|
public byte[] ToByteArray()
|
||||||
{
|
{
|
||||||
if (Config == null)
|
if (this.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");
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonSerializerOptions options = new JsonSerializerOptions();
|
var options = new JsonSerializerOptions
|
||||||
options.IgnoreNullValues = true;
|
|
||||||
|
|
||||||
String json = JsonSerializer.Serialize(new
|
|
||||||
{
|
{
|
||||||
width = Width,
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||||
height = Height,
|
};
|
||||||
backgroundColor = BackgroundColor,
|
|
||||||
devicePixelRatio = DevicePixelRatio,
|
var json = JsonSerializer.Serialize(new
|
||||||
format = Format,
|
{
|
||||||
chart = Config,
|
width = this.Width,
|
||||||
key = Key,
|
height = this.Height,
|
||||||
version = Version,
|
backgroundColor = this.BackgroundColor,
|
||||||
|
devicePixelRatio = this.DevicePixelRatio,
|
||||||
|
format = this.Format,
|
||||||
|
chart = this.Config,
|
||||||
|
key = this.Key,
|
||||||
|
version = this.Version,
|
||||||
}, options);
|
}, options);
|
||||||
|
|
||||||
string url = $"{Scheme}://{Host}:{Port}/chart";
|
var url = $"{this.Scheme}://{this.Host}:{this.Port}/chart";
|
||||||
|
|
||||||
HttpResponseMessage response = Client.PostAsync(
|
var response = Client.PostAsync(
|
||||||
url,
|
url,
|
||||||
new StringContent(json, Encoding.UTF8, "application/json")
|
new StringContent(json, Encoding.UTF8, "application/json")
|
||||||
).Result;
|
).Result;
|
||||||
|
|
||||||
if (!response.IsSuccessStatusCode)
|
return !response.IsSuccessStatusCode
|
||||||
{
|
? throw new ApiException("Unsuccessful response from API", response)
|
||||||
throw new ApiException("Unsuccessful response from API", response);
|
: response.Content.ReadAsByteArrayAsync().Result;
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -191,4 +177,3 @@ namespace QuickChart
|
||||||
|
|
||||||
public HttpResponseMessage Response { get; private set; }
|
public HttpResponseMessage Response { get; private set; }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,23 +2,27 @@
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
<TargetFrameworks>net9.0</TargetFrameworks>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<PlatformTarget>x64</PlatformTarget>
|
||||||
|
<Configurations>Debug;Release;x64</Configurations>
|
||||||
|
<Platforms>AnyCPU;x64</Platforms>
|
||||||
<PackOnBuild>true</PackOnBuild>
|
<PackOnBuild>true</PackOnBuild>
|
||||||
<Authors>Ian Webster</Authors>
|
<Authors>Ian Webster, TheXorog</Authors>
|
||||||
<Copyright>Ian Webster</Copyright>
|
<Copyright>Ian Webster, TheXorog</Copyright>
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<Owners>QuickChart</Owners>
|
<Owners>QuickChart</Owners>
|
||||||
<PackageProjectUrl>https://github.com/typpo/quickchart-csharp</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/TheXorog/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="5.0.0" />
|
<PackageReference Include="System.Text.Json" Version="7.0.3" />
|
||||||
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue