-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNewCognitiveFaceAuth.cs
71 lines (62 loc) · 2.63 KB
/
NewCognitiveFaceAuth.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using Contoso.CognitivePipeline.SharedModels.Models;
using Contoso.CognitivePipeline.BackgroundServices.Services;
using System.Threading.Tasks;
using System.Net.Http;
using System;
using Microsoft.Extensions.Logging;
namespace Contoso.CognitivePipeline.BackgroundServices.Functions
{
public static class NewCognitiveFaceAuth
{
private static HttpClient httpClient;
[FunctionName("NewCognitiveFaceAuth")]
public static async Task<IActionResult> Run(
//HTTP Trigger (Functions allow only a single trigger)
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "NewCognitiveFaceAuth/{personId}")]NewRequest<SmartDoc> newRequest,
// Inputs
[Blob("smartdocs/{RequestItem.DocName}", FileAccess.Read, Connection = "SmartDocsStorageConnection")] byte[] smartDocImage,
string personId,
// Logger
ILogger log)
{
string stepName = InstructionFlag.FaceAuthentication.ToString();
log.LogInformation($"***New {stepName} Direct-HTTP Request triggered: {JsonConvert.SerializeObject(newRequest)}");
if (httpClient == null)
{
httpClient = new HttpClient();
}
try
{
var result = await ComputerVisionService.GetFaceAuthAsync(httpClient, smartDocImage, personId);
var resultJson = JsonConvert.SerializeObject(result);
//Update the request information with the newly processed data
newRequest.RequestItem.CognitivePipelineActions.Add(new ProcessingStep
{
StepName = stepName,
LastUpdatedAt = DateTime.UtcNow,
Output = resultJson,
Status = SmartDocStatus.ProccessedSuccessfully.ToString()
});
return (ActionResult)new OkObjectResult(newRequest);
}
catch(Exception ex)
{
newRequest.RequestItem.CognitivePipelineActions.Add(new ProcessingStep
{
StepName = stepName,
LastUpdatedAt = DateTime.UtcNow,
Output = ex.Message,
Status = SmartDocStatus.ProcessedUnsuccessfully.ToString()
});
return (ActionResult)new BadRequestObjectResult(newRequest);
}
}
}
}