feat(EConnectClient): update PostAsync to handle Stream instead of StreamContent
This commit is contained in:
parent
48a69f884e
commit
b5082daa1a
@ -11,5 +11,5 @@ public interface IEConnectClient<TError> where TError : class
|
||||
|
||||
public Task<Response<TError>> PostAsync(string? route = null, object? queryParams = null, CancellationToken cancel = default);
|
||||
|
||||
public Task<Response<TError>> PostAsync(StreamContent content, string? route = null, object? queryParams = null, CancellationToken cancel = default);
|
||||
Task<Response<TError>> PostAsync(Stream stream, string fileName, string? route = null, object? queryParams = null, CancellationToken cancel = default);
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
using Leanetec.EConnect.Domain.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace Leanetec.EConnect.Infrastructure;
|
||||
@ -125,24 +126,36 @@ public class EConnectClient<TError> : IEConnectClient<TError> where TError : cl
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Response<TError>> PostAsync(StreamContent content, string? route = null, object? queryParams = null, CancellationToken cancel = default)
|
||||
public async Task<Response<TError>> PostAsync(Stream stream, string fileName, string? route = null, object? queryParams = null, CancellationToken cancel = default)
|
||||
{
|
||||
route = AddQueryString(route, queryParams);
|
||||
|
||||
var res = await Http.PostAsync(route, content, cancel);
|
||||
// Create multipart form data content
|
||||
using var content = new MultipartFormDataContent();
|
||||
|
||||
_logger?.LogCurlAsync(Http, HttpMethod.Post, route, content);
|
||||
// add file with Stream and fileName
|
||||
content.Add(new StreamContent(stream), "file", fileName);
|
||||
|
||||
// add file type
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
|
||||
|
||||
// create message and add accept header
|
||||
using var message = new HttpRequestMessage(HttpMethod.Post, route);
|
||||
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
|
||||
|
||||
message.Content = content;
|
||||
|
||||
var res = await Http.SendAsync(message, cancel);
|
||||
|
||||
_logger?.LogCurlAsync(Http, requestMessage: message);
|
||||
|
||||
if (res.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Ok = true,
|
||||
StatusCode = res.StatusCode
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Ok = false,
|
||||
@ -151,6 +164,5 @@ public class EConnectClient<TError> : IEConnectClient<TError> where TError : cl
|
||||
? await res.Content.ReadFromJsonAsync<TError>(_options.JsonSerializerOptions, cancel)
|
||||
: null
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,13 +5,22 @@ namespace Leanetec.EConnect.Infrastructure;
|
||||
|
||||
public static class LogExtensions
|
||||
{
|
||||
public static async Task LogCurlAsync<TCategoryName>(this ILogger<TCategoryName> logger, HttpClient client, HttpMethod method, string? route = null, HttpContent? content = null, LogLevel logLevel = LogLevel.Information)
|
||||
public static async Task LogCurlAsync<TCategoryName>(
|
||||
this ILogger<TCategoryName> logger,
|
||||
HttpClient client,
|
||||
HttpMethod? method = null,
|
||||
string? route = null,
|
||||
HttpContent? content = null,
|
||||
HttpRequestMessage? requestMessage = null,
|
||||
LogLevel logLevel = LogLevel.Information)
|
||||
{
|
||||
route ??= requestMessage?.RequestUri?.ToString();
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("curl");
|
||||
|
||||
// Method
|
||||
sb.Append($" -X {method.Method}");
|
||||
sb.Append($" -X {method?.Method ?? requestMessage?.Method.ToString()}");
|
||||
|
||||
// URL
|
||||
var fullUrl = string.Empty;
|
||||
@ -31,6 +40,13 @@ public static class LogExtensions
|
||||
sb.AppendLine($"\t-H \"{header.Key}: {string.Join(", ", header.Value)}\"");
|
||||
}
|
||||
|
||||
// Headers
|
||||
if (requestMessage is not null)
|
||||
foreach (var header in requestMessage.Headers)
|
||||
{
|
||||
sb.AppendLine($"\t-H \"{header.Key}: {string.Join(", ", header.Value)}\"");
|
||||
}
|
||||
|
||||
if (content != null)
|
||||
{
|
||||
foreach (var header in content.Headers)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user