better support of raw data transfer

This commit is contained in:
Pilzinsel64
2025-03-18 08:56:08 +01:00
parent 86154e93af
commit 24cde0bd03
3 changed files with 21 additions and 1 deletions

View File

@@ -55,7 +55,16 @@ public class ApiClient(string apiUrl) : IApiClient
TResponse? result = null; TResponse? result = null;
if (res.IsSuccessStatusCode) if (res.IsSuccessStatusCode)
result = serializer.Deserialize(await res.Content.ReadAsStringAsync(), typeof(TResponse)) as TResponse; {
var mediaType = res.Content.Headers.ContentType?.MediaType;
if (!string.IsNullOrWhiteSpace(mediaType))
{
if (mediaType == "application/json")
result = serializer.Deserialize(await res.Content.ReadAsStringAsync(), typeof(TResponse)) as TResponse;
else if (typeof(TResponse).IsAssignableTo(typeof(ApiRawMessage)) && mediaType == "application/octet-stream")
result = (TResponse)(object)new ApiRawMessage(await res.Content.ReadAsByteArrayAsync());
}
}
return new(res, result); return new(res, result);
} }

View File

@@ -0,0 +1,6 @@
namespace Pilz.Net.Api;
public class ApiRawMessage(byte[] data) : ApiMessage
{
public byte[] Data { get; } = data;
}

View File

@@ -411,6 +411,7 @@ public class ApiServer : IApiServer
else if (result.ResultContent is byte[] resultBytes) else if (result.ResultContent is byte[] resultBytes)
{ {
Log.Info("Sending raw bytes response for " + context.Request.RawUrl); Log.Info("Sending raw bytes response for " + context.Request.RawUrl);
context.Response.ContentType = "application/octet-stream";
context.Response.OutputStream.Write(resultBytes, 0, resultBytes.Length); context.Response.OutputStream.Write(resultBytes, 0, resultBytes.Length);
context.Response.OutputStream.Flush(); context.Response.OutputStream.Flush();
} }
@@ -455,6 +456,10 @@ public class ApiServer : IApiServer
if (result.Message is null) if (result.Message is null)
return new(result, null); return new(result, null);
// Return result with raw data
if (result.Message is ApiRawMessage dataMsg)
return new(result, dataMsg.Data);
// Serializer // Serializer
Log.Debug("Serialize message"); Log.Debug("Serialize message");
if (serializer.Serialize(result.Message) is not string resultStr) if (serializer.Serialize(result.Message) is not string resultStr)