-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNewCognitiveReq.cs
93 lines (79 loc) · 3.67 KB
/
NewCognitiveReq.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using CognitivePipeline.Functions.Models;
using CognitivePipeline.Functions.Abstractions;
using CognitivePipeline.Functions.Models.DTO;
using CognitivePipeline.Functions.Validation;
using Newtonsoft.Json.Converters;
namespace CognitivePipeline.Functions.Functions
{
/// <summary>
/// Azure Function that primarily parse & save both the Cognitive File details to Cosmos DB, Cognitive File bytes to Azure Storage container
/// </summary>
public class NewCognitiveReq
{
private readonly ICognitiveFileRepository cognitiveFilesRepo;
private readonly IUserAccountRepository userAccountsRepo;
private readonly IStorageRepository filesStorageRepo;
private readonly IQueueRepository newReqsQueue;
/// <summary>
/// Leveraging the new Azure Functions Dependency Injection by sending common services in the constructor
/// </summary>
/// <param name="filesRepo">Cosmos Db repository for Cognitive Files</param>
public NewCognitiveReq(ICognitiveFileRepository filesRepo, IUserAccountRepository usersRepo, IStorageRepository storageRepo, IQueueRepository queueRepo)
{
cognitiveFilesRepo = filesRepo;
userAccountsRepo = usersRepo;
filesStorageRepo = storageRepo;
newReqsQueue = queueRepo;
}
/// <summary>
/// Initiate new cognitive pipeline processing for a document
/// </summary>
/// <param name="req"></param>
/// <param name="log"></param>
/// <returns></returns>
[FunctionName("NewCognitiveReq")]
public async Task<IActionResult> Run(
//Trigger
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req,
//Output (I'm using queue repo from the dependency injection to be more independent from the trigger bindings)
//[Queue("newreq", Connection = "cognitivePipelineStorageConnection")]ICollector<string> newReqsQueue,
//Logger
ILogger log)
{
log.LogInformation("******* NewCognitiveReq starting");
try
{
var provider = new MultipartMemoryStreamProvider();
await req.Content.ReadAsMultipartAsync(provider);
//Get cognitive file attributes
var fileInfo = provider.Contents[0];
var fileInfoJson = await fileInfo.ReadAsStringAsync();
var cognitiveFileDTO = JsonConvert.DeserializeObject<CognitiveFileDTO>(fileInfoJson);
var cognitiveFile = ValidateCognitiveFile.ValidateForSubmission(cognitiveFileDTO, userAccountsRepo);
//Get file bytes
var fileData = provider.Contents[1];
var fileBytes = await fileData.ReadAsByteArrayAsync();
var url = await filesStorageRepo.CreateFileAsync(cognitiveFile.FileName, fileBytes);
cognitiveFile.FileUrl = url;
await cognitiveFilesRepo.AddAsync(cognitiveFile);
await newReqsQueue.QueueMessage(JsonConvert.SerializeObject(cognitiveFile));
return new CreatedResult(cognitiveFile.FileName, null);
}
catch (Exception ex)
{
log.LogError($"####### FAILED: {ex.Message}");
return new BadRequestObjectResult($"Request submission failed");
}
}
}
}