Skip to content

Commit 1382578

Browse files
Bob WallBob Wall
authored andcommitted
added httpResponse and stock classes
1 parent b1a4fdc commit 1382578

5 files changed

Lines changed: 91 additions & 14 deletions

File tree

sample-apps/robinhood-for-reddit/handlers/hotStock/hotStockHandler.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Context, Handler } from 'aws-lambda';
2-
import { IHttpResponse, IStock } from './types';
2+
import { HttpResponse, Stock } from './types';
33
import { HotStockService } from './hotStockService';
44

55
/**
@@ -8,26 +8,53 @@ import { HotStockService } from './hotStockService';
88
* @param context context object
99
*/
1010
const process: Handler = async (event: any, context: Context) => {
11-
console.info('hotStockHandler.process', { event });
12-
1311
try {
12+
console.info('hotStockHandler.process', { event });
13+
1414
// get the array of hot stocks from the hot stock service
15-
const hotStocks: IStock[] = await HotStockService.getHotStocks();
15+
const hotStocks: Stock[] = await HotStockService.getHotStocks();
1616

17-
// create a valid response object
18-
const response: IHttpResponse = {
17+
/*
18+
create a valid response object
19+
- sets cors and cookie settings
20+
- sets to cache for 4 hours
21+
*/
22+
const response = new HttpResponse({
1923
statusCode: 200,
2024
body: JSON.stringify(hotStocks),
2125
headers: {
22-
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
23-
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
26+
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
27+
"Access-Control-Allow-Credentials" : true, // Required for cookies, authorization headers with HTTPS
28+
"Cache-Control": "public, max-age=14400, s-maxage=14400'" // cache the response for 4 hours
29+
}
30+
});
31+
32+
// return the response back
33+
context.succeed(response);
34+
} catch (error) {
35+
// report the error to the console
36+
console.error('hotStockHandler.error', { error });
37+
38+
let body = '';
39+
40+
try {
41+
body = JSON.stringify(error);
42+
} catch (ex) {
43+
body = 'Unable to stringify result';
44+
}
45+
46+
const response = new HttpResponse({
47+
statusCode: 500,
48+
body,
49+
headers: {
50+
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
51+
"Access-Control-Allow-Credentials" : true, // Required for cookies, authorization headers with HTTPS
52+
"Cache-Control": "no-cache" // No Cache
2453
}
25-
};
54+
});
2655

56+
// return back as success, but this is has a failed statusCode
2757
context.succeed(response);
28-
} catch (exception) {
29-
console.error('hotStockHandler.error', { exception });
30-
context.fail(exception);
3158
}
3259
};
3360

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface IHttpResponse {
2-
statusCode: number;
3-
headers?: any;
42
body?: string;
3+
headers?: any;
4+
statusCode: number;
55
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { IHttpResponse } from './IHttpResponse';
2+
3+
export class HttpResponse implements IHttpResponse {
4+
5+
public body?: string;
6+
public headers?: any;
7+
public statusCode: number;
8+
9+
constructor(response: IHttpResponse) {
10+
this.body = response.body;
11+
this.headers = response.headers;
12+
this.statusCode = response.statusCode;
13+
}
14+
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './httpResponse';
12
export * from './IHttpResponse';
23
export * from './IStock';
34
export * from './StockMarket';
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { IStock } from './IStock';
2+
3+
export class Stock implements IStock {
4+
5+
public bloombergTerminalUrl?: string;
6+
public companyName: string;
7+
public companyUrl: string;
8+
public hasElonMuskTweetedAboutIt: boolean;
9+
public id: string;
10+
public isHot: boolean;
11+
public marketSymbol: string;
12+
public redditMentions: number;
13+
public stockMarket: StockMarket;
14+
public updatedAt: Date;
15+
public value: number;
16+
17+
constructor(response: IHttpResponse) {
18+
this.id = (response.id)
19+
? response.id
20+
:uuid();
21+
22+
this.bloombergTerminalUrl = response.bloombergTerminalUrl;
23+
this.companyName = response.companyName;
24+
this.companyUrl = response.companyUrl;
25+
this.hasElonMuskTweetedAboutIt = response.hasElonMuskTweetedAboutIt;
26+
this.isHot = response.isHot;
27+
this.marketSymbol = response.marketSymbol;
28+
this.redditMentions = response.redditMentions;
29+
this.stockMarket = response.stockMarket;
30+
this.updatedAt = response.updatedAt;
31+
this.value = response.value;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)