This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
travel_agency_service.bal
187 lines (161 loc) · 7.27 KB
/
travel_agency_service.bal
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import ballerina/http;
import ballerina/log;
//import ballerinax/docker;
//import ballerinax/kubernetes;
//@docker:Config {
// registry:"ballerina.guides.io",
// name:"travel_agency_service",
// tag:"v1.0"
//}
//
//@docker:Expose{}
//@kubernetes:Ingress {
// hostname:"ballerina.guides.io",
// name:"ballerina-guides-travel-agency-service",
// path:"/"
//}
//
//@kubernetes:Service {
// serviceType:"NodePort",
// name:"ballerina-guides-travel-agency-service"
//}
//
//@kubernetes:Deployment {
// image:"ballerina.guides.io/travel_agency_service:v1.0",
// name:"ballerina-guides-travel-agency-service"
//}
// Service endpoint
listener http:Listener travelAgencyEP = new (9090);
// Client endpoint to communicate with Airline reservation service
http:Client airlineReservationEP = new ("http://localhost:9091/airline");
// Client endpoint to communicate with Hotel reservation service
http:Client hotelReservationEP = new ("http://localhost:9092/hotel");
// Client endpoint to communicate with Car rental service
http:Client carRentalEP = new ("http://localhost:9093/car");
// Travel agency service to arrange a complete tour for a user
@http:ServiceConfig {
basePath: "/travel"
}
service travelAgencyService on travelAgencyEP {
// Resource to arrange a tour
@http:ResourceConfig {
methods: ["POST"],
consumes: ["application/json"],
produces: ["application/json"]
}
resource function arrangeTour(http:Caller caller, http:Request inRequest) returns error? {
http:Response outResponse = new;
map<json> inReqPayload = {};
// JSON payload format for an HTTP out request.
map<json> outReqPayload = {"Name": "", "ArrivalDate": "", "DepartureDate": "", "Preference": ""};
// Try parsing the JSON payload from the user request
var payload = inRequest.getJsonPayload();
if (payload is map<json>) {
// Valid JSON payload
inReqPayload = payload;
} else {
// NOT a valid JSON payload
outResponse.statusCode = 400;
outResponse.setJsonPayload({"Message": "Invalid payload - Not a valid JSON payload"});
var result = caller->respond(outResponse);
handleError(result);
return;
}
outReqPayload["Name"] = inReqPayload["Name"];
outReqPayload["ArrivalDate"] = inReqPayload["ArrivalDate"];
outReqPayload["DepartureDate"] = inReqPayload["DepartureDate"];
json | error airlinePreference = inReqPayload.Preference.Airline;
json | error hotelPreference = inReqPayload.Preference.Accommodation;
json | error carPreference = inReqPayload.Preference.Car;
// If payload parsing fails, send a "Bad Request" message as the response
if (outReqPayload.Name is () || outReqPayload.ArrivalDate is () || outReqPayload.DepartureDate is () ||
airlinePreference is error || hotelPreference is error || carPreference is error) {
outResponse.statusCode = 400;
outResponse.setJsonPayload({"Message": "Bad Request - Invalid Payload"});
var result = caller->respond(outResponse);
handleError(result);
return;
}
// Reserve airline ticket for the user by calling Airline reservation service
// construct the payload
map<json> outReqPayloadAirline = outReqPayload.clone();
outReqPayloadAirline["Preference"] = <json>airlinePreference;
// Send a post request to airlineReservationService with appropriate payload and get response
http:Response inResAirline = check airlineReservationEP->post("/reserve", <@untainted>outReqPayloadAirline);
// Get the reservation status
var airlineResPayload = check inResAirline.getJsonPayload();
string airlineStatus = airlineResPayload.Status.toString();
// If reservation status is negative, send a failure response to user
if (airlineStatus != "Success") {
outResponse.setJsonPayload({
"Message": "Failed to reserve airline! " +
"Provide a valid 'Preference' for 'Airline' and try again"
});
var result = caller->respond(outResponse);
handleError(result);
return;
}
// Reserve hotel room for the user by calling Hotel reservation service
// construct the payload
map<json> outReqPayloadHotel = outReqPayload.clone();
outReqPayloadHotel["Preference"] = <json>hotelPreference;
// Send a post request to hotelReservationService with appropriate payload and get response
http:Response inResHotel = check hotelReservationEP->post("/reserve", <@untainted>outReqPayloadHotel);
// Get the reservation status
var hotelResPayload = check inResHotel.getJsonPayload();
string hotelStatus = hotelResPayload.Status.toString();
// If reservation status is negative, send a failure response to user
if (hotelStatus != "Success") {
outResponse.setJsonPayload({
"Message": "Failed to reserve hotel! " +
"Provide a valid 'Preference' for 'Accommodation' and try again"
});
var result = caller->respond(outResponse);
handleError(result);
return;
}
// Renting car for the user by calling Car rental service
// construct the payload
map<json> outReqPayloadCar = outReqPayload.clone();
outReqPayloadCar["Preference"] = <json>carPreference;
// Send a post request to carRentalService with appropriate payload and get response
http:Response inResCar = check carRentalEP->post("/rent", <@untainted>outReqPayloadCar);
// Get the rental status
var carResPayload = check inResCar.getJsonPayload();
string carRentalStatus = carResPayload.Status.toString();
// If rental status is negative, send a failure response to user
if (carRentalStatus != "Success") {
outResponse.setJsonPayload({
"Message": "Failed to rent car! " +
"Provide a valid 'Preference' for 'Car' and try again"
});
var result = caller->respond(outResponse);
handleError(result);
return;
}
// If all three services response positive status, send a successful message to the user
outResponse.setJsonPayload({"Message": "Congratulations! Your journey is ready!!"});
var result = caller->respond(outResponse);
handleError(result);
}
}
function handleError(error? result) {
if (result is error) {
log:printError(result.reason(), err = result);
}
}