Contact Us 1-800-596-4880

Implementing the American Flights API Spec

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

Open Beta Release: The cloud IDE is in open beta. Any use of Anypoint Code Builder in its beta state is subject to the applicable beta services terms and conditions, available from the IDE.

Import your American Flights API specification from Anypoint Exchange. Scaffold the specification into the American Flights project that you are developing, and add the logic to implement an endpoint in the API.

Import and Scaffold Your American Flights API Specification

Import your American Flights API specification from Anypoint Exchange, and scaffold the specification into an interface for your American Flights integration project.

  1. In Anypoint Code Builder, open your integration, american-ws-anypoint-code-builder.

    For information about this application, see Integrating American Flights Processes.

  2. Open the Command Palette.

    Show me how
    • Use the keyboard shortcuts:

      • Mac: Cmd+Shift+p

      • Windows: Ctrl+Shift+p

    • In the desktop IDE, select View > Command Palette.

    • In the cloud IDE, click the (menu) icon, and select View > Command Palette.

  3. Provide this command:

    MuleSoft: Import Asset from Exchange
  4. Select Rest API:

    Rest API option highlighted after selecting MuleSoft: Import Asset from Exchange
  5. If prompted, log in to Anypoint Platform, allowing the extension to sign in and open an external web site and to open Visual Studio Code.

  6. Type the name of your American Flights API specification, for example:

    American Flights API

    For information about this specification, see Designing the American Flights API Spec.

  7. Wait for the IDE to load a list of matches to the name:

    American Flights API asset is highlighted
  8. When prompted for a version, select the version of the API to import, such as 1.0.0.

  9. Select Yes when prompted to scaffold the API dependency.

    This step adds the API specification as a dependency in your project’s pom.xml file and creates a new configuration XML file, such as american-flights-api.xml:

    American-flights.xml file highlighted in the package explorer section
  10. Proceed to Tour the Interface File.

Tour the Interface File

Examine the scaffolded flows and error handlers for your interface in the canvas and configuration XML.

The main flow is highlighted
  1. From the EXPLORER menu, open the configuration file for your interface, american-flight-api.xml.

  2. Use the Flow List panel to navigate through flows:

    American-flights-api-main flow highlighted
    1. Expand the Flow List panel.

    2. Select a flow from the list.

    3. Click the flow in the canvas to highlight the flow in the configuration XML.

  3. In the configuration XML, locate the flows created for the endpoints in your API specification:

    • get:\flights

      <flow name="get:\flights:american-flight-api-config">
      </flow>
    • post:\flights

      <flow name="post:\flights:application\json:american-flight-api-config">
      </flow>
    • get:\flights{ID}

      <flow name="get:\flights\(ID):application\json:american-flight-api-config">
      </flow>
  4. Notice the automatically generated Error Handler components, for example:

    <apikit:router config-ref="american-flights-api-config" />
    <error-handler>
        <on-error-propagate type="APIKIT:BAD_REQUEST">
    
        </on-error-propagate>
        <on-error-propagate type="APIKIT:NOT_FOUND">
    
        </on-error-propagate>
        <on-error-propagate type="APIKIT:METHOD_NOT_ALLOWED">
    
        </on-error-propagate>
        <on-error-propagate type="APIKIT:NOT_ACCEPTABLE">
    
        </on-error-propagate>
        <on-error-propagate type="APIKIT:UNSUPPORTED_MEDIA_TYPE">
    
        </on-error-propagate>
        <on-error-propagate type="APIKIT:NOT_IMPLEMENTED">
    
        </on-error-propagate>
    </error-handler>
  5. Proceed to Name Your Interface and Implementation Files.

Name Your Interface and Implementation Files

Provide descriptive names for the interface and implementation files in your American Flights project. The interface receives all incoming requests to your application. The scaffolded flows in the interface validate and route requests. The implementation provides the backend logic for calls to the interface.

Rename option highlighted the context menu of american-flight-api-acb.xml file
  1. Rename american-flight-api-acb.xml to interface.xml by right-clicking the file name, selecting Rename, and providing the new name.

  2. Rename american-ws-anypoint-code-builder.xml to implementation.xml.

  3. Proceed to Configure an Endpoint through the Interface.

Configure an Endpoint through the Interface

To receive HTTP requests through the interface, remove the HTTP listener from implementation.xml, and in your interface file (interface.xml), add a Flow Ref component (<flow-ref/>) to the getFlights flow in your implementation.

  1. Open implementation.xml.

    HTTP listener highlighted in the implementation.xml file
  2. Delete the HTTP /flights listener XML from the configuration XML:

    <http:listener path="flights" config-ref="inbound-request" doc:name="HTTP /flights" />
    Click for the resulting flow.
    <flow name="getFlights">
        <db:select doc:name="Query Flights" doc:id="pvuqsc" config-ref="Database_Config" >
            <db:sql>
                <![CDATA[Select * FROM american]]>
            </db:sql>
        </db:select>
    
        <ee:transform doc:name="Transform Message" doc:id="uniqueId" >
            <ee:message >
                <ee:set-payload >
                    <![CDATA[
                    %dw 2.0
                    output application/json
                    ---
                    payload map ( payload01 , indexOfPayload01 ) -> {
                      ID: payload01.ID,
                      code: (payload01.code1 default "") ++ (payload01.code2 default ""),
                      price: payload01.price default 0,
                      departureDate: payload01.takeOffDate as String default "",
                      origin: payload01.fromAirport default "",
                      destination: payload01.toAirport default "",
                      emptySeats: payload01.seatsAvailable default 0,
                      plane: {
                        "type": payload01.planeType default "",
                        totalSeats: payload01.totalSeats default 0
                      }
                    }
                    ]]>
                </ee:set-payload>
            </ee:message>
        </ee:transform>
    
    </flow>
  3. In Anypoint Code Builder, open your interface.xml file.

  4. Locate the get:\flights:american-flights-api-config flow:

    <flow name="get:\flights:american-flights-api-config">
        <logger level="INFO" message="get:\flights:american-flights-api-config" />
    </flow>
  5. Before the <logger/> element in the flow, add a Flow Reference component (<flow-ref/>) that references the flow getFlights in the implementation:

    <flow name="get:\flights:american-flights-api-config">
        <flow-ref doc:name="getFlightsRef" name="getFlights"/>
        <logger level="INFO" message="get:\flights:american-flights-api-config" />
    </flow>
  6. Optional: Trigger a flow through your interface, and process the request with your implementation.

  7. Proceed to Debugging the American Flights App.