-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRequestDataExtensions.cs
45 lines (41 loc) · 1.4 KB
/
HttpRequestDataExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Microsoft.Azure.Functions.Worker.Http;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using System.Threading;
using Whds.ArticleMaster.DataIngest.FileSplitter.Extensions;
namespace Whds.ArticleMaster.DataIngest.FileSplitter.Extensions
{
public static class HttpRequestDataExtensions
{
public static (T body, List<ValidationResult> validationResults) ExtractAndValidate<T>(
this HttpRequestData request)
{
var body = JsonSerializer.Deserialize<T>(request.Body);
var validationResults = new List<ValidationResult>();
Validator.TryValidateObject(
body,
new ValidationContext(
body,
null,
null),
validationResults,
true);
return (body, validationResults);
}
public static async Task<HttpResponseData> CreateResponse<T>(
this HttpRequestData httpRequest,
HttpStatusCode statusCode,
T body,
CancellationToken cancellationToken)
{
var response = httpRequest.CreateResponse(statusCode);
await response.WriteAsJsonAsync(
body,
cancellationToken);
return response;
}
}
}