ApiStreamMessage
This commit is contained in:
@@ -66,8 +66,10 @@ public class ApiClient(string apiUrl) : IApiClient
|
||||
{
|
||||
if (mediaType == "application/json" && context.Serializer != null)
|
||||
result = context.Serializer.Deserialize(await context.HttpResponse.Content.ReadAsStringAsync(), typeof(TResponse)) as TResponse;
|
||||
else if (typeof(TResponse).IsAssignableTo(typeof(ApiRawMessage)) && mediaType == "application/octet-stream")
|
||||
result = (TResponse)(object)new ApiRawMessage(await context.HttpResponse.Content.ReadAsByteArrayAsync());
|
||||
else if (typeof(TResponse).IsAssignableTo(typeof(ApiRawByteMessage)) && mediaType == "application/octet-stream")
|
||||
result = (TResponse)(object)new ApiRawByteMessage(await context.HttpResponse.Content.ReadAsByteArrayAsync());
|
||||
else if (typeof(TResponse).IsAssignableTo(typeof(ApiRawStreamMessage)) && mediaType == "application/octet-stream")
|
||||
result = (TResponse)(object)new ApiRawStreamMessage(await context.HttpResponse.Content.ReadAsStreamAsync());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
6
Pilz.Net/Api/ApiRawByteMessage.cs
Normal file
6
Pilz.Net/Api/ApiRawByteMessage.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Pilz.Net.Api;
|
||||
|
||||
public class ApiRawByteMessage(byte[] data) : ApiRawMessage
|
||||
{
|
||||
public byte[] Data { get; } = data;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Pilz.Net.Api;
|
||||
|
||||
public class ApiRawMessage(byte[] data) : ApiMessage
|
||||
public abstract class ApiRawMessage : ApiMessage
|
||||
{
|
||||
public byte[] Data { get; } = data;
|
||||
public string? FileName { get; set; }
|
||||
}
|
||||
|
||||
6
Pilz.Net/Api/ApiRawStreamMessage.cs
Normal file
6
Pilz.Net/Api/ApiRawStreamMessage.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Pilz.Net.Api;
|
||||
|
||||
public class ApiRawStreamMessage(Stream data) : ApiRawMessage
|
||||
{
|
||||
public Stream Data { get; } = data;
|
||||
}
|
||||
@@ -464,6 +464,8 @@ public class ApiServer : IApiServer
|
||||
// Set response header
|
||||
Log.Debug("Set response headers");
|
||||
context.Response.AppendHeader("API-VERSION", ApiVersion.ToString());
|
||||
if (result.Original.Message is ApiRawMessage apiRawMessage && !string.IsNullOrWhiteSpace(apiRawMessage.FileName))
|
||||
context.Response.AppendHeader("Content-Disposition", $"filename=\"{apiRawMessage.FileName}\"");
|
||||
|
||||
// Set response parameters
|
||||
Log.Debug("Set response parameters");
|
||||
@@ -486,6 +488,14 @@ public class ApiServer : IApiServer
|
||||
context.Response.OutputStream.Write(resultBytes, 0, resultBytes.Length);
|
||||
context.Response.OutputStream.Flush();
|
||||
}
|
||||
else if (result.ResultContent is Stream resultStream)
|
||||
{
|
||||
Log.Info("Sending stream response for " + context.Request.RawUrl);
|
||||
context.Response.ContentType = "application/octet-stream";
|
||||
resultStream.CopyTo(context.Response.OutputStream);
|
||||
context.Response.OutputStream.Flush();
|
||||
resultStream.Close();
|
||||
}
|
||||
|
||||
Log.Debug("Finish response");
|
||||
close(false);
|
||||
@@ -528,8 +538,10 @@ public class ApiServer : IApiServer
|
||||
return new(result, null);
|
||||
|
||||
// Return result with raw data
|
||||
if (result.Message is ApiRawMessage dataMsg)
|
||||
if (result.Message is ApiRawByteMessage dataMsg)
|
||||
return new(result, dataMsg.Data);
|
||||
if (result.Message is ApiRawStreamMessage streamMsg)
|
||||
return new(result, streamMsg.Data);
|
||||
|
||||
// Serializer
|
||||
Log.Debug("Serialize message");
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<Version>2.9.2</Version>
|
||||
<Version>2.9.3</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user