-
Notifications
You must be signed in to change notification settings - Fork 1
/
PluginEndpoint.cs
64 lines (53 loc) · 2.94 KB
/
PluginEndpoint.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
using System.Net;
using System.Text.Json;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
public class PluginEndpoint
{
private readonly ILogger _logger;
public PluginEndpoint(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<PluginEndpoint>();
}
[Function("WellKnownAIPlugin")]
public async Task<HttpResponseData> WellKnownAIPlugin(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = ".well-known/ai-plugin.json")] HttpRequestData req)
{
var toReturn = new AIPlugin();
toReturn.Api.Url = $"{req.Url.Scheme}://{req.Url.Host}:{req.Url.Port}/swagger.json";
var r = req.CreateResponse(HttpStatusCode.OK);
await r.WriteAsJsonAsync(toReturn);
return r;
}
[OpenApiOperation(operationId: "AppendToWordDocument", tags: new[] { "AppendToWordDocumentFunction" }, Description = "Appends the given text to a Word Document stored on an Azure Block Blob.")]
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(AppendToDocRequest), Description = "JSON describing the content to append and the URI to a writeable Azure Block Blob.", Required = true)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.Created, contentType: "text/plain", bodyType: typeof(string), Description = "Confirms that the content was written.")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, contentType: "application/json", bodyType: typeof(string), Description = "Returns the error of the input.")]
[Function("AppendToWordDocument")]
public async Task<HttpResponseData> AppendToWordDocument([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "doc")] HttpRequestData req)
{
_logger.LogInformation("Beginning to append content to blob");
var appendRequest = JsonSerializer.Deserialize<AppendToDocRequest>(req.Body, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (string.IsNullOrWhiteSpace(appendRequest!.WriteableBlobUri))
{
var r1 = req.CreateResponse(HttpStatusCode.BadRequest);
await r1.WriteAsJsonAsync(new { error = "WriteableBlobUri is required." });
return r1;
}
if (string.IsNullOrWhiteSpace(appendRequest.Content))
{
var r2 = req.CreateResponse(HttpStatusCode.BadRequest);
await r2.WriteAsJsonAsync(new { error = "Content is required." });
return r2;
}
await WordDocWriter.AppendContentToBlob(appendRequest.WriteableBlobUri, appendRequest.Content);
_logger.LogInformation("Content was appended to blob");
var r = req.CreateResponse(HttpStatusCode.Created);
r.Headers.Add("Content-Type", "text/plain");
r.WriteString("Content was appended to blob");
return r;
}
}