This commit is contained in:
Sipke Schoorstra 2021-12-20 17:24:26 +01:00
commit 5bc368efe7
8 changed files with 11 additions and 9 deletions

View file

@ -144,7 +144,7 @@ namespace Elsa.Activities.Http
if (hasContent && ReadContent)
{
var formatter = SelectContentParser(ResponseContentParserName, contentType);
ResponseContent = await formatter.ReadAsync(this, response, cancellationToken);
ResponseContent = await formatter.ReadAsync(response, this, cancellationToken);
}
var statusCode = (int)response.StatusCode;

View file

@ -14,7 +14,7 @@ namespace Elsa.Activities.Http.Parsers.Response
public int Priority => 0;
public bool GetSupportsContentType(string contentType) => contentType.Contains("/json", StringComparison.OrdinalIgnoreCase);
public async Task<object> ReadAsync(SendHttpRequest activity, HttpResponseMessage response, CancellationToken cancellationToken)
public async Task<object> ReadAsync(HttpResponseMessage response, object context, CancellationToken cancellationToken)
{
var json = (await response.Content.ReadAsStringAsync()).Trim();

View file

@ -11,12 +11,13 @@ namespace Elsa.Activities.Http.Parsers.Response
{
public string Name => "File";
public int Priority => 0;
public bool GetSupportsContentType(string contentType)
{
var types = new[] { "audio", "video", "application", "pdf" };
return types.Any(x => contentType.Contains(x, StringComparison.OrdinalIgnoreCase));
}
public async Task<object> ReadAsync(SendHttpRequest activity, HttpResponseMessage response, CancellationToken cancellationToken) => await response.Content.ReadAsByteArrayAsync();
public async Task<object> ReadAsync(HttpResponseMessage response, object context, CancellationToken cancellationToken) => await response.Content.ReadAsByteArrayAsync();
}
}

View file

@ -13,7 +13,7 @@ namespace Elsa.Activities.Http.Parsers.Response
public int Priority => 0;
public bool GetSupportsContentType(string contentType) => contentType.Contains("/json", StringComparison.OrdinalIgnoreCase);
public async Task<object> ReadAsync(SendHttpRequest activity, HttpResponseMessage response, CancellationToken cancellationToken)
public async Task<object> ReadAsync(HttpResponseMessage response, object context, CancellationToken cancellationToken)
{
var json = (await response.Content.ReadAsStringAsync()).Trim();
return GetJTokenValue(json);

View file

@ -11,6 +11,6 @@ namespace Elsa.Activities.Http.Parsers.Response
public string Name => "JSON";
public int Priority => 100;
public bool GetSupportsContentType(string contentType) => contentType.Contains("/json", StringComparison.OrdinalIgnoreCase);
public async Task<object> ReadAsync(SendHttpRequest activity, HttpResponseMessage response, CancellationToken cancellationToken) => await response.Content.ReadAsStringAsync();
public async Task<object> ReadAsync(HttpResponseMessage response, object context, CancellationToken cancellationToken) => await response.Content.ReadAsStringAsync();
}
}

View file

@ -10,6 +10,6 @@ namespace Elsa.Activities.Http.Parsers.Response
public string Name => "Plain Text";
public int Priority => -1;
public bool GetSupportsContentType(string contentType) => true;
public async Task<object> ReadAsync(SendHttpRequest activity, HttpResponseMessage response, CancellationToken cancellationToken) => await response.Content.ReadAsStringAsync();
public async Task<object> ReadAsync(HttpResponseMessage response, object context, CancellationToken cancellationToken) => await response.Content.ReadAsStringAsync();
}
}

View file

@ -15,14 +15,15 @@ namespace Elsa.Activities.Http.Parsers.Response
public int Priority => 0;
public bool GetSupportsContentType(string contentType) => GetIsJsonContentType(contentType);
public async Task<object> ReadAsync(SendHttpRequest activity, HttpResponseMessage response, CancellationToken cancellationToken)
public async Task<object> ReadAsync(HttpResponseMessage response, object context, CancellationToken cancellationToken)
{
var activity = context as SendHttpRequest;
var contentType = response.Content.Headers.ContentType.MediaType;
if (GetIsJsonContentType(contentType))
{
var json = (await response.Content.ReadAsStringAsync()).Trim();
var targetType = activity.ResponseContentTargetType ?? typeof(ExpandoObject);
var targetType = activity?.ResponseContentTargetType ?? typeof(ExpandoObject);
return JsonConvert.DeserializeObject(json, targetType)!;
}

View file

@ -9,6 +9,6 @@ namespace Elsa.Activities.Http.Services
string Name { get; }
int Priority { get; }
bool GetSupportsContentType(string contentType);
Task<object> ReadAsync(SendHttpRequest activity, HttpResponseMessage response, CancellationToken cancellationToken);
Task<object> ReadAsync(HttpResponseMessage response, object context, CancellationToken cancellationToken);
}
}