-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsharpStarterHandler.cs
147 lines (125 loc) · 4.84 KB
/
CsharpStarterHandler.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Amazon.Lambda.Core;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using UCLA.EA.Blackbaud;
/// NOTE: - only using this so the handler will return mock data, normally a live Handler wouldn't reference the testing
using UCLA.EA.TestingLib;
[assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
/**
* Every Lambda should live in its own namespace - to prevent accidental dependencies between lambdas
**/
namespace UCLA.EA.Lambda.CsharpStarter
{
/** This boilerplate code demonstrates:\n
* * Initializing a client in the class constructor\n
* * Using async/await\n
* * Lambda handler method signature: LookupCrmUser(Request Request, ILambdaContext context)\n
* * Using built-in lambda JSON deserializers\n
* * Accessing Blackbaud library to make a back end call\n
* * Creating a virtual overridable method to retrieve the Blackbaud DataList class (so the test class can mock the Request to Blackbaud)\n
* * Wrapping Request and response in custom csharp objects\n
* * Logging; WORK IN PROGRESS\n
* \n
* See SimpleHandler.cs for more basic examples\n
*/
public class CsharpStarterHandler
{
static AppFxClient BBClient;
public CsharpStarterHandler()
{
BBClient = new AppFxClient(UCLA.EA.Blackbaud.Environment.Get("TEST4"), "UCLA API");
}
/**
* Handler can be invoked by API Gateway, a step function, or another lambda
* Handler function is specified in serverless.yml
**/
public async Task<Response> LookupCrmUser(Request Request, ILambdaContext context)
{
List<LookupOutput> testResultsList = await findUser(Request);
// logObject(testResultsList);
if (testResultsList.Count() == 1) {
return new Response(testResultsList[0], "SINGLE_USER_FOUND", false);
}
else if (testResultsList.Count() > 1) {
return new Response(null, "MULTIPLE_USERS_FOUND", true);
}
else {
return new Response(null, "NO_USERS_FOUND", true);
}
}
public async Task<List<LookupOutput>> findUser(Request input) {
IDataList<EmailInput, LookupOutput> dataList = getDataList(BBClient, new Guid("2f559fc5-9e76-4cfe-8697-4e57cc2f7c9d"));
IEnumerable<LookupOutput> results = await dataList.LoadAsync(new EmailInput{email = input.email}, null);
List<LookupOutput> testResultsList = results.ToList();
return testResultsList;
}
/**
* Creating a virtual function so the HandlerTest class can override it to return mock results
**/
public virtual IDataList<EmailInput, LookupOutput> getDataList(AppFxClient client, Guid callId)
{
// return new DataList<EmailInput, LookupOutput>(client, callId);
// TODO - using mock for now until the real DataList is ready
var mockDataList = new MockDataList<EmailInput, LookupOutput>(client, callId);
mockDataList.HandlerTest = new CsharpStarterHandlerTest();
return mockDataList;
}
/**
* Simple log function, will be moved to a library, also logging will be standardized/automated in the near future
**/
public static void logObject(object input) {
LambdaLogger.Log("\nObject type: " + input.GetType().Name + "\n"
+ JsonConvert.SerializeObject(input, Formatting.Indented) + "\n");
}
}
public class EmailInput
{
public string email { get; set; }
}
public class LookupOutput
{
public string LOOKUPID { get; set; }
public string UID { get; set; }
}
public class Response
{
public ErrorResponse Error {get; set;}
public SuccessResponse Success {get; set;}
public Response(object payload, string msgkey, bool isError){
if (isError)
{
Error = new ErrorResponse{msgkey=msgkey};
}
else
{
Success = new SuccessResponse{payload=payload, msgkey=msgkey};
}
}
}
public class SuccessResponse
{
public object payload {get; set;}
public string msgkey {get; set;}
}
public class ErrorResponse
{
public string msgkey {get; set;}
}
public class Request
{
public string email {get; set;}
public PhoneNumber phoneNumber {get; set;}
}
public class PhoneNumber
{
public string countryCode { get; set; }
public string number { get; set; }
public string toString()
{
return this.countryCode + " " + this.number;
}
}
}