Created
March 23, 2020 15:57
-
-
Save nuwanbando/49aab1704b333e636f1bc0edcad731b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ballerina/http; | |
import ballerina/log; | |
import ballerina/stringutils; | |
import ballerina/observe; | |
// Service endpoint | |
listener http:Listener travelAgencyEP = new(8090); | |
// Client endpoint to communicate with Airline reservation service | |
http:Client airlineReservationEP = new("https://airline-booking-svc-nuwan.preprod.choreo.dev/airline"); | |
// Client endpoint to communicate with Hotel reservation service | |
http:Client hotelReservationEP = new("https://hotel-booking-svc-nuwan.preprod.choreo.dev/hotel"); | |
// Client endpoint to communicate with Car rental service | |
http:Client carRentalEP = new("https://car-rental-svc-nuwan.preprod.choreo.dev/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 | |
@observe:Observable | |
@http:ResourceConfig {methods:["POST"], consumes:["application/json"], produces:["application/json"]} | |
resource function arrangeTour(http:Caller caller, http:Request inRequest) returns @tainted error? { | |
http:Response outResponse = new; | |
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 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"] = check inReqPayload.Name; | |
outReqPayload["ArrivalDate"] = check inReqPayload.ArrivalDate; | |
outReqPayload["DepartureDate"] = check inReqPayload.DepartureDate; | |
json airlinePreference = check inReqPayload.Preference.Airline; | |
json hotelPreference = check inReqPayload.Preference.Accommodation; | |
json carPreference = check inReqPayload.Preference.Car; | |
// If payload parsing fails, send a "Bad Request" message as the response | |
if (outReqPayload.Name == () || outReqPayload.ArrivalDate == () || outReqPayload.DepartureDate == () || | |
airlinePreference == () || hotelPreference == () || carPreference == ()) { | |
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; | |
outReqPayloadAirline["Preference"] = 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 (stringutils:equalsIgnoreCase(airlineStatus, "Failed")) { | |
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; | |
outReqPayloadHotel["Preference"] = 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 (stringutils:equalsIgnoreCase(hotelStatus, "Failed")) { | |
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; | |
outReqPayloadCar["Preference"] = 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 (stringutils:equalsIgnoreCase(carRentalStatus, "Failed")) { | |
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); | |
return (); | |
} | |
} | |
function handleError(error? result) { | |
if (result is error) { | |
log:printError(result.reason(), err = result); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment