/
Function.cs
65 lines (56 loc) · 1.94 KB
/
Function.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.APIGatewayEvents;
using Newtonsoft.Json;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace GetRandomTIL
{
public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task<response> FunctionHandler(APIGatewayCustomAuthorizerRequest request, ILambdaContext context)
{
try
{
Get get_til = new Get("reddit_til", context);
string child_str = JsonConvert.SerializeObject(await get_til.Child(), Formatting.Indented);
//Response
var response = new response()
{
statusCode = "200",
headers = new Dictionary<string, string>() { { "Access-Control-Allow-Origin", "*" } },
body = child_str
};
//Return
return response;
}
catch (Exception e)
{
//Response
var response = new response()
{
statusCode = "400",
headers = new Dictionary<string, string>() { { "Access-Control-Allow-Origin", "*" } },
body = e.Message
};
//Return
return response;
}
}
//Response
public class response
{
public string statusCode { get; set; }
public Dictionary<string, string> headers { get; set; }
public string body { get; set; }
}
}
}