Lesson 4: Basic REST APIs

Full Weather Reporting API

Overview

In this lesson we build on the previous lessons and enhance our Weather Reporting System by adding a full RESTful API to our existing flow that allows the client application to Create (POST), Read (GET), Update (PUT/PATCH), and Delete (DELETE).

This is a common pattern in HTTP flows that provide a custom data access layer for a web or mobile application.

Objective

Learn about how to implement the various REST operations using a combination of HTTP-IN nodes and Object nodes.

Description:

  • Enhance the incoming weather report to support additional fields including humidity, wind, wind direction, and UV index.

  • Update the web form to allow the submission of these additional properties

  • Add new endpoints to the flow to support a /weather-report API with GET, POST, PUT, PATCH, and DELETE.

  • Test the new web app with the new endpoints.

Terminal Objective: Possess a solid understanding of how to create an HTTP-Flow that exposes BASIC REST API endpoints (GET, POST, PUT, PATCH, DELETE) and then uses the various Native Object nodes to implement those operations.

Enabling Objectives:

  • Learn to use an HTTP-IN POST node combined with NO CREATE

  • Learn to use an HTTP-IN PUT node combined with NO PUT

  • Learn to use an HTTP-IN PATCH node combined with function node to build patch payload and NO PATCH

  • Learn to use an HTTP-IN GET node combined with NO GET

  • Learn to use an HTTP-IN DELETE node combined with NO DELETE

New Skills: NO-3, NO-4, NO-5, NO-6

Detailed Steps

Add New Properties to Weather Report Object-Type

  1. Edit the weather-report object type and add the following properties:

    • Humidity

    • Wind Speed

    • Wind direction

    • UV index

  2. Open the submit-weather-report flow and modify the HTML form using the following HTML.

Add a Full REST API

The steps above satify the need for a POST node to create a new record. Now let's add the additional nodes to provide for update and delete capabilities to give our flow a full REST API.

  1. First, add a HTTP-IN POST node with a path of /weather-report and wire it in parallel with your existing /submit node so that it does the same thing.

  2. Now add an HTTP-IN node with a verb=GET and a path=/weather-report/:reportId

  • Now add a function node with the following JS

  • Now add a GET Object node and configure it's type for "weather-report" and it's object id for msg.reportId

  1. Now add an HTTP-IN node with a verb=PUT and a path=/weather-report

  • Now add a function node with the following JS

  • Now add a PUT Object node and configure it's type for "weather-report" and it's object id for msg.reportId, and input as msg.payload

  1. Now add an HTTP-IN node with a verb=PATCH and a path=/weather-report

  • Now add a function node with the following JS

  • Now add a SWITCH node with two conditions:

    • msg.payload: is not empty

    • otherwise

  • Dont forget to select "stopping after first match" at the bottom of the node configuration.

  • Now add a PATCH Object node and configure it's type for "weather-report" and it's object id for msg.reportId, and input as msg.payload

  1. Your complete flow should now look like this:

  1. Push SAVE to save the changes and then restart the agent.

Testing in the Revised Web App

Create Weather Summary Object-Type

  1. Now create a new object type and call it "weather-summary". This will be the object type that is used to store hourly weather summaries.

  2. Give it the following properties:

    • date

    • hour

    • avgTemperature

    • minTemperature

    • maxTemperature

    • avgHumidity

    • minHumidity

    • maxHumidity

    • avgWindSpeed

    • minWindSpeed

    • maxWindSpeed

    • reports

  3. Make sure it has an auto-generated 'id' field as the primary key

  4. Save the new object type

Create a New Weather Summarization Flow

This new flow will receive the incoming weather report using a trigger. It will then GET the existing weather summary for the current hour (if there is one). Using the report and the existing summary, it will calculate the updated values for the report and then PATCH the report with the modified values. It will also add the report to the reports array on the summary record.

  1. Create a new flow called "weather-summarization"

  2. Add the Inject, Prepare Event, and Start Event nodes

  3. Add a

Last updated

Was this helpful?