1
00:00:00,240 --> 00:00:04,950
In this video, we are going to start to developing basket microservices.

2
00:00:05,190 --> 00:00:09,840
So as you can see, that basket microservices has mainly two task.

3
00:00:09,870 --> 00:00:16,170
The first task is accommodate incoming HTTP post requests from the API gateway and after that it is

4
00:00:16,170 --> 00:00:20,760
basically publish check out event into the Amazon event page.

5
00:00:20,970 --> 00:00:23,150
So let's get start implementation.

6
00:00:23,160 --> 00:00:24,240
For that purpose.

7
00:00:24,240 --> 00:00:27,800
I have created separate section and lecture folder in here.

8
00:00:27,810 --> 00:00:30,970
So this is the common text that we are going to do one by one.

9
00:00:30,990 --> 00:00:38,160
Basically, we will create a Node.js project for our lecture and this will be publishing to basket lambda

10
00:00:38,160 --> 00:00:38,850
function.

11
00:00:38,850 --> 00:00:46,170
So in order to make it Node.js module, I will start the API and why please go to correct folder sector

12
00:00:46,170 --> 00:00:48,320
and start with the NPM command.

13
00:00:48,330 --> 00:00:50,400
So we have create package JSON file.

14
00:00:50,400 --> 00:00:56,370
The first thing we should do that set the type as a module to gain a security six standards.

15
00:00:56,400 --> 00:00:57,110
Okay.

16
00:00:57,120 --> 00:01:02,970
If you come back to common text after that, we, we would like to install the required package.

17
00:01:02,970 --> 00:01:05,310
So which packages we are need to do.

18
00:01:05,340 --> 00:01:07,550
We basically need to client event bridge.

19
00:01:07,590 --> 00:01:13,620
If you go to documentation, you can see that we will use one page client, a WS SDK library.

20
00:01:13,620 --> 00:01:18,540
If you scroll down, you can see the details and how we can install it using API command.

21
00:01:18,540 --> 00:01:23,970
And here you can see the import statement and if you scroll down, you can see the simple implementation

22
00:01:23,970 --> 00:01:25,770
or even client object.

23
00:01:25,830 --> 00:01:31,020
Okay, after installing this package, we will use put event commands in order to publish checkout,

24
00:01:31,020 --> 00:01:32,580
even to Amazon event page.

25
00:01:32,580 --> 00:01:38,460
So let's copy this install command, go back to our project and here I'm going to copy paste from in

26
00:01:38,460 --> 00:01:39,750
here and hit enter.

27
00:01:40,960 --> 00:01:47,500
So these will install required even package that we can use for our basket microservices.

28
00:01:50,460 --> 00:01:53,460
Basically we need to incoming payload from API Gateway.

29
00:01:53,490 --> 00:01:57,620
After that, we need to publish that message to Amazon until each custom event pass.

30
00:01:57,630 --> 00:02:00,140
So that's why we use this event page.

31
00:02:00,180 --> 00:02:03,380
Please verify from the node modules and also package JSON file.

32
00:02:03,390 --> 00:02:08,100
Go to dependencies and make sure that you have installed client event successfully.

33
00:02:08,550 --> 00:02:08,970
Okay.

34
00:02:08,970 --> 00:02:09,330
Very good.

35
00:02:09,330 --> 00:02:11,970
Now it is time to develop our lambda function code.

36
00:02:12,000 --> 00:02:15,060
We should plan our development for basket microservices.

37
00:02:15,090 --> 00:02:17,750
Now let's write a PC code for that purpose.

38
00:02:17,760 --> 00:02:19,770
I have created index file.

39
00:02:19,770 --> 00:02:26,640
If you open this file, you can see that I have implement minimum minimum method of the lambda function.

40
00:02:26,640 --> 00:02:31,650
This is the our handler method and the first line of code I have log incoming event.

41
00:02:31,680 --> 00:02:33,850
After that I put here try cash flow.

42
00:02:33,900 --> 00:02:36,930
Now I'm going to write down the PC code.

43
00:02:36,930 --> 00:02:40,680
That means plan our operations in this lambda function.

44
00:02:40,680 --> 00:02:46,190
So basically the first operation is let me come here and write down one by one.

45
00:02:46,200 --> 00:02:51,270
So the first step is accommodate the incoming HTTP post request to the correct set.

46
00:02:51,270 --> 00:02:56,250
So that means if you go back to our architecture, we will accommodate three post operation from API

47
00:02:56,250 --> 00:02:58,380
Gateway into basket microservices.

48
00:02:58,800 --> 00:03:05,190
If you come back to our used to do operation, the second step is get the request.

49
00:03:05,190 --> 00:03:05,640
But.

50
00:03:06,500 --> 00:03:11,880
Eight requests by the payload, which include event data when it comes to trigger from the API gateway.

51
00:03:11,910 --> 00:03:16,960
The body information includes the actual payload, so that's why we should get the request by the payload.

52
00:03:16,980 --> 00:03:19,710
The second second step and the third step.

53
00:03:19,710 --> 00:03:26,430
After getting the request data, we basically publish message to Amazon event page custom event twice

54
00:03:26,430 --> 00:03:28,740
using different HDC libraries.

55
00:03:28,740 --> 00:03:29,880
And the last step.

56
00:03:29,880 --> 00:03:35,280
Since it is a synchronous invocation, we should return back the synchronous basket payload to the API

57
00:03:35,280 --> 00:03:35,890
gateway.

58
00:03:35,910 --> 00:03:37,570
So now let's get started.

59
00:03:37,590 --> 00:03:41,670
Implement these four step in our lambda function in the try case below.

60
00:03:41,700 --> 00:03:45,600
For that purpose, I'm going to start with the first step in the trip.

61
00:03:45,610 --> 00:03:48,660
Look, let me come here and copy paste in here.

62
00:03:48,690 --> 00:03:52,880
As you can see that in the first step we are validating the incoming acid method.

63
00:03:52,890 --> 00:03:58,590
The acid method should be post, otherwise we are throwing an exception because we are expecting a HTTP

64
00:03:58,620 --> 00:04:04,860
post operation for the basket payload for the checkout operation and the payload include should include

65
00:04:04,860 --> 00:04:08,010
the required event details about the checkout basket.

66
00:04:08,190 --> 00:04:09,780
Okay, this was the first step.

67
00:04:09,780 --> 00:04:14,640
And if you go back the second step, which is the gate request, but the payload in order to do that,

68
00:04:14,640 --> 00:04:17,150
we should check the incoming event.

69
00:04:17,160 --> 00:04:25,140
You can see in here basically we got the event that body and JSON parse to get basket request and validate

70
00:04:25,140 --> 00:04:29,070
the basket request should not be null and also detailed type should not be null.

71
00:04:29,160 --> 00:04:34,680
So you can see the expected payload that we are expecting from the HTTP post operation in here.

72
00:04:34,680 --> 00:04:38,130
And also I'll copy paste from the command text if you come here.

73
00:04:38,130 --> 00:04:44,310
We basically expect this kind of payload include item price, detail, type and source.

74
00:04:44,370 --> 00:04:47,760
You can see the second expected payload example in here.

75
00:04:48,510 --> 00:04:53,370
We are expecting this payload because if you scroll down, you can remember that in the last video we

76
00:04:53,370 --> 00:04:55,890
had defined event pattern in Amazon event page.

77
00:04:55,890 --> 00:04:59,640
So our event pattern including to detail type and source information.

78
00:04:59,640 --> 00:05:04,320
So that's why we will paste these to information from payload to the event page.

79
00:05:04,450 --> 00:05:05,030
Okay.

80
00:05:05,040 --> 00:05:10,680
So after understanding the payload, we basically validate the payload that including the detail type

81
00:05:10,680 --> 00:05:11,280
also.

82
00:05:11,280 --> 00:05:16,410
And we got the request basket request from the event, but the information which comes from the Amazon

83
00:05:16,410 --> 00:05:19,560
API gateway and now I'm going to come to third step.

84
00:05:19,560 --> 00:05:25,290
In the third step, I don't want to implement now I will implement in the next video, but in the third

85
00:05:25,290 --> 00:05:30,990
step we will use the event event page client object to publish message to the Amazon event page.

86
00:05:31,020 --> 00:05:35,250
In the fourth step, basically we will return back to synchronous response.

87
00:05:35,250 --> 00:05:42,840
So if you come here and we should return 200 success response when it comes to work, everything well.

88
00:05:42,840 --> 00:05:48,030
And as you can see that when I am facing the body information as a data which will come from the published

89
00:05:48,030 --> 00:05:51,270
message and we we return the successful message.

90
00:05:51,270 --> 00:05:54,510
So if you scroll down in the cache block, we will lock the error.

91
00:05:54,510 --> 00:06:00,330
And after that we should return the 500 error statement to the sync response.

92
00:06:00,330 --> 00:06:08,160
You can see in here, we basically return 500 such code and error details in this return statement.

93
00:06:08,160 --> 00:06:13,650
So that means since it is a synchronous invocation, we are response back to synchronous response to

94
00:06:13,650 --> 00:06:14,730
API gateway.

95
00:06:14,730 --> 00:06:16,920
If everything goes well, we return success.

96
00:06:16,920 --> 00:06:22,770
Response is if there is an exception, we will return back the exception details and 500 cells code.

97
00:06:23,070 --> 00:06:28,370
As you can see that we have write down our code and implement into the cache block of our handle method.

98
00:06:28,380 --> 00:06:32,960
Now in the next video we will focus on the third step, which is the published message to Amazon event

99
00:06:32,960 --> 00:06:33,360
page.
