Over a million developers have joined DZone.

Integrating Google QPX With Mule

DZone's Guide to

Integrating Google QPX With Mule

You can use this connector to integrate Mule with the Google QPX Express API, giving your app a flight availability and price search functionality.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

The Google QPX Express API allows developers to search for real flight availability and prices and use this data into their applications. Google Flights use this same data to provide a flight search and travel insights into Google search.

Recently, I developed a community connector using Mule DevKit allowing a better and easier integration for Mule developers. This connector interacts with Google QPX API, providing a flight search operation and allows DataSense metadata, consequently improving the integration with Mule using Dataweave.

In this article, I'll present this connector building an application to define an API using RAML to search for flights and transforming data using Dataweave.

The project is published on GitHub and you could access the connector documentation here.

First of all, let's install the connector:

Clone or download the Github repository and import the project into Anypoint Studio with File-->Import-->Anypoint Connector Project from External Location.

Right-click the imported project and choose Anypoint Connector-->Install or Update.

After the installation, the connector will be available on the Anypoint Studio palette.

The RAML API is defined with a unique resource: GET /air/search and query parameters as follow:

      displayName: airSearch
      description: Air search      
          required: true
          displayName: origin
          type: string
          pattern: "^[a-zA-Z]{3}$"         
          required: true
          displayName: destination
          type: string
          pattern: "^[a-zA-Z]{3}$"
          required: true
          displayName: departureDate
          type: date-only 
          required: false
          displayName: returnDate
          type: date-only
          required: true
          displayName: adtCount
          type: integer 
          minimum: 1
          maximum: 9   
          required: false
          displayName: chdCount
          type: integer
          minimum: 1
          maximum: 9        
              displayName: airSearchResponse
              type: Recommendation[]  

The response (Recommendation array) is defined using RAML Types. See the complete RAML definition below for details.

Create a new Mule project and use this RAML definition to obtain auto-generated flows for the API.

Create a new Mule configuration file to implement the API. Create a new flow and reference it on auto-generated air/search flow.

Image title

In the new flow, drag the Google-QPX-Connector from your palette and create a new configuration to the connector. You need to specify a valid API Key, see this link to obtain a new one.

Click "Test connection..." to validate the API Key and check if everything is ok with your configuration.
Image title

Open the connector tab select the operation "Trips" and define the expression: #[payload] at "Trips Search Request Reference."

Drag two Dataweave components to the flow, one in each side of the Google-QPX-Connector.

Image title

In the first one, code the request transformation using the air/search resource parameters previously defined in the RAML. This will create a minimum request data to query Google QPX API.

%dw 1.0
%output application/java
request: {
passengers: {
adultCount: inboundProperties."http.query.params".adtCount as :number,
(childCount: inboundProperties."http.query.params".chdCount as :number) when inboundProperties."http.query.params".chdCount?
slice: [{
date: inboundProperties."http.query.params".departureDate,
destination: inboundProperties."http.query.params".destination,
origin: inboundProperties."http.query.params".origin
}] when inboundProperties."http.query.params".returnDate is :null
date: inboundProperties."http.query.params".departureDate,
destination: inboundProperties."http.query.params".destination,
origin: inboundProperties."http.query.params".origin
date: inboundProperties."http.query.params".returnDate,
destination: inboundProperties."http.query.params".origin,
origin: inboundProperties."http.query.params".destination
} as :object {
class : "org.mule.modules.googleqpx.model.TripsSearchRequest"

In the last one, code the response transformation previously defined in the RAML air/search resource. This will convert the response data from Google QPX API to the RAML response type.

%dw 1.0
%output application/json
%var farePattern = /([A-Z]{3})(\d+\.?\d*)/
%function getFareBasis(pricing, segmentPricing)
(pricing.fare filter $.id == segmentPricing.fareId)[0].basisCode
%function getClassOfService(tripOption, segmentPricing)
(flatten tripOption.slice.segment filter $.id == segmentPricing.segmentId)[0].bookingCode
%function getCabin(tripOption, segmentPricing)
using (c = (flatten tripOption.slice.segment filter $.id == segmentPricing.segmentId)[0].cabin)
('ECONOMY' when c == 'COACH' otherwise 
'ECONOMY_PREMIUM' when c == 'PREMIUM_COACH' otherwise 
%function getAmmount(value)
(value match farePattern)[2] as :number
%function getCurrency(value)
(value match farePattern)[1]
payload.trips.tripOption map ((tripOption , indexOfTripOption) -> {
itinerary: tripOption.slice map ((slice , indexOfSlouse) -> {
duration: slice.duration,
mileage: sum (flatten slice.segment.leg).mileage,
segment: slice.segment map ((segment , indexOfSegment) -> {
flightDesignator: {
flightNumber: segment.flight.number,
marketingCarrierCode: segment.flight.carrier
flightDetails: segment.leg map ((leg , indexOfLeg) -> {
aircraft: leg.aircraft,
arrivalDate: leg.arrivalTime,
arrivalStation: leg.destination,
departureDate: leg.departureTime,
departureStation: leg.origin,
duration: leg.duration,
mileage: leg.mileage
id: segment.id
pricing: [{
pricingInfo: tripOption.pricing map ((pricing , indexOfPricing) -> {
baseFare: {
amount: getAmmount(pricing.baseFareTotal),
currencyCode: getCurrency(pricing.baseFareTotal)
fare: {
amount: getAmmount(pricing.saleFareTotal),
currencyCode: getCurrency(pricing.saleFareTotal)
fareCalculation: pricing.fareCalculation,
segmentFareInfo: pricing.segmentPricing map ((segmentPricing , indexOfSegmentPricing) -> {
airSegmentId: segmentPricing.segmentId,
cabin: getCabin(tripOption, segmentPricing),
classOfService: getClassOfService(tripOption, segmentPricing),
fareBasis: getFareBasis(pricing, segmentPricing)
ptc: pricing.ptc
validatingCarrier: tripOption.pricing[0].fare[0].carrier
provider: "Google"

Start the project and access the API console: http://localhost:8081/console

Play with the API using de RAML Console and manipulate the request parameters to search for flights!!!
API Console

API request example:

Image title

API response example:

API Response

The complete project can be found on the GitHub repository in the demo folder.

This is a simple example of how to play with Google QPX Express and could help you to start a new project relating to travel content.

Please feel free to get in touch if you have any doubts or ideas!

Build and deploy API integrations 7x faster. Try the Cloud Elements 100% RESTful platform for 30 days free. Access your trial here.

integration ,mulesoft ,connectors ,google ,travel ,raml

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}