-
Notifications
You must be signed in to change notification settings - Fork 0
/
FetchData.cs
60 lines (51 loc) · 2.11 KB
/
FetchData.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
using System;
using System.Net;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using static System.Net.WebRequestMethods;
namespace Fetcher
{
public class FetchData
{
private readonly ILogger _logger;
public FetchData(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<FetchData>();
}
[Function("fetchBlob")]
public async Task<HttpResponseData> FetchBlob
(
[HttpTrigger(AuthorizationLevel.Function, Http.Get)] HttpRequestData req,
[BlobInput("static-files/index.html", Connection = "FetcherConnection")] string indexHtml,
string name
)
{
_logger.LogInformation("C# HTTP trigger function {FncName} processed a request.", "fetchBlob");
return await PrepareResponseAsync(req.CreateResponse(HttpStatusCode.OK), indexHtml, name);
}
[Function("fetchLocal")]
public async Task<HttpResponseData> FetchLocal
(
[HttpTrigger(AuthorizationLevel.Function, Http.Get)] HttpRequestData req,
string name
)
{
_logger.LogInformation("C# HTTP trigger function {FncName} processed a request.", "fetchLocal");
var path = System.IO.Path.Join(Environment.CurrentDirectory, "static/index.html");
var indexHtml = await System.IO.File.ReadAllTextAsync(path);
return await PrepareResponseAsync(req.CreateResponse(HttpStatusCode.OK), indexHtml, name);
}
private static async Task<HttpResponseData> PrepareResponseAsync(HttpResponseData httpResponseData, string indexHtml, string name)
{
if (!string.IsNullOrWhiteSpace(name))
indexHtml = indexHtml.Replace("Hi!", $"Hi, {name}!");
httpResponseData.Headers.Add("Content-Type", MediaTypeNames.Text.Html);
await httpResponseData.WriteStringAsync(indexHtml, Encoding.UTF8);
return httpResponseData;
}
}
}