better support of raw data transfer
This commit is contained in:
@@ -55,7 +55,16 @@ public class ApiClient(string apiUrl) : IApiClient
|
||||
TResponse? result = null;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
6
Pilz.Net/Api/ApiRawMessage.cs
Normal file
6
Pilz.Net/Api/ApiRawMessage.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Pilz.Net.Api;
|
||||
|
||||
public class ApiRawMessage(byte[] data) : ApiMessage
|
||||
{
|
||||
public byte[] Data { get; } = data;
|
||||
}
|
||||
@@ -411,6 +411,7 @@ public class ApiServer : IApiServer
|
||||
else if (result.ResultContent is byte[] resultBytes)
|
||||
{
|
||||
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.Flush();
|
||||
}
|
||||
@@ -455,6 +456,10 @@ public class ApiServer : IApiServer
|
||||
if (result.Message is null)
|
||||
return new(result, null);
|
||||
|
||||
// Return result with raw data
|
||||
if (result.Message is ApiRawMessage dataMsg)
|
||||
return new(result, dataMsg.Data);
|
||||
|
||||
// Serializer
|
||||
Log.Debug("Serialize message");
|
||||
if (serializer.Serialize(result.Message) is not string resultStr)
|
||||
|
||||
Reference in New Issue
Block a user