HTTP API robot tutorial

Tutorial for creating a software robot for fetching data from an HTTP API using Robot Framework and RPA Framework.

SpaceX logo

This simple software robot fetches and logs the latest launch data from SpaceX API using RPA Framework.

Prerequisites

Set up your development environment.

Initialize the software robot directory

Navigate to your projects directory in the terminal or the command prompt. Initialize the software robot directory:

robo init http-api

Navigate to the directory:

cd http-api

Robot task file

Paste the following Robot Framework code in the tasks/robot.robot file:

*** Settings ***
Documentation     HTTP API robot. Retrieves data from SpaceX API. Demonstrates
...               how to use RPA.HTTP (create session, get response, validate
...               response status, pretty-print, get response as text, get
...               response as JSON, access JSON properties, etc.).
Resource          keywords.robot
Suite Setup       Setup
Suite Teardown    Teardown

*** Tasks ***
Log latest launch info
    Log latest launch

Robot keywords file

Paste the following Robot Framework code in the resources/keywords.robot file:

*** Settings ***
Library           RPA.HTTP
Variables         variables.py

*** Keywords ***
Setup
    Create Session    spacex    ${SPACEX_API_BASE_URL}    verify=True

Teardown
    Delete All Sessions

Log latest launch
    ${launch}=    Get latest launch
    Log info    ${launch}

Get latest launch
    ${response}=    Get Request    spacex    ${SPACEX_API_LATEST_LAUNCHES}
    Request Should Be Successful    ${response}
    Status Should Be    200    ${response}
    [Return]    ${response}

Log info
    [Arguments]    ${response}
    ${pretty_json}=    To Json    ${response.text}    pretty_print=True
    ${launch}=    Set Variable    ${response.json()}
    Log To Console    ${pretty_json}
    Log To Console    ${launch["mission_name"]}
    Log To Console    ${launch["rocket"]["rocket_name"]}

Variables file

Paste the following Python code in the variables/variables.py file:

SPACEX_API_BASE_URL = "https://api.spacexdata.com/v3"
SPACEX_API_LATEST_LAUNCHES = "/launches/latest"

Wrap the robot

robo wrap

Run the robot

Windows:

robo run entrypoint.cmd

macOS / Linux:

robo run entrypoint.sh

Example response:

{
  "crew": null,
  "details": "This mission will launch the eighth batch of operational Starlink satellites, which are expected to be version 1.0, from SLC-40, Cape Canaveral AFS. It is the ninth Starlink launch overall. The satellites will be delivered to low Earth orbit and will spend a few weeks maneuvering to their operational altitude of 550 km. This mission is expected to include rideshare payloads, SkySats 16-18, on top of the Starlink stack. The booster for this mission is expected to land an ASDS.",
  "flight_number": 96,
  "is_tentative": false,
  "last_date_update": "2020-06-11T04:25:39.000Z",
  "last_ll_launch_date": null,
  "last_ll_update": null,
  "last_wiki_launch_date": "2020-06-13T09:21:00.000Z",
  "last_wiki_revision": "9a320975-ab9b-11ea-8445-0e30d54c90e7",
  "last_wiki_update": "2020-06-11T04:25:39.000Z",
  "launch_date_local": "2020-06-13T05:21:00-04:00",
  "launch_date_source": "wiki",
  "launch_date_unix": 1592040060,
  "launch_date_utc": "2020-06-13T09:21:00.000Z",
  "launch_site": {
    "site_id": "ccafs_slc_40",
    "site_name": "CCAFS SLC 40",
    "site_name_long": "Cape Canaveral Air Force Station Space Launch Complex 40"
  },
  "launch_success": true,
  "launch_window": 0,
  "launch_year": "2020",
  "links": {
    "article_link": "https://spaceflightnow.com/2020/06/13/starlink-satellite-deployments-continue-with-successful-falcon-9-launch/",
    "flickr_images": [
      "https://live.staticflickr.com/65535/50009748327_93e52a451f_o.jpg"
    ],
    "mission_patch": "https://images2.imgbox.com/d2/3b/bQaWiil0_o.png",
    "mission_patch_small": "https://images2.imgbox.com/9a/96/nLppz9HW_o.png",
    "presskit": null,
    "reddit_campaign": "https://www.reddit.com/r/spacex/comments/gwbr4t/starlink8_launch_campaign_thread/",
    "reddit_launch": "https://www.reddit.com/r/spacex/comments/h7gqlc/rspacex_starlink_8_official_launch_discussion/",
    "reddit_media": "https://www.reddit.com/r/spacex/comments/h842qk/rspacex_starlink8_media_thread_photographer/",
    "reddit_recovery": "https://www.reddit.com/r/spacex/comments/h8sx6q/starlink8_recovery_thread/",
    "video_link": "https://youtu.be/8riKQXChPGg",
    "wikipedia": "https://en.wikipedia.org/wiki/Starlink",
    "youtube_id": "8riKQXChPGg"
  },
  "mission_id": [],
  "mission_name": "Starlink-8 & SkySat 16-18",
  "rocket": {
    "fairings": {
      "recovered": null,
      "recovery_attempt": true,
      "reused": true,
      "ship": "GOMSTREE"
    },
    "first_stage": {
      "cores": [
        {
          "block": 5,
          "core_serial": "B1059",
          "flight": 3,
          "gridfins": true,
          "land_success": true,
          "landing_intent": true,
          "landing_type": "ASDS",
          "landing_vehicle": "OCISLY",
          "legs": true,
          "reused": true
        }
      ]
    },
    "rocket_id": "falcon9",
    "rocket_name": "Falcon 9",
    "rocket_type": "FT",
    "second_stage": {
      "block": 5,
      "payloads": [
        {
          "customers": ["SpaceX"],
          "manufacturer": "SpaceX",
          "nationality": "United States",
          "norad_id": [],
          "orbit": "VLEO",
          "orbit_params": {
            "apoapsis_km": null,
            "arg_of_pericenter": null,
            "eccentricity": null,
            "epoch": null,
            "inclination_deg": null,
            "lifespan_years": null,
            "longitude": null,
            "mean_anomaly": null,
            "mean_motion": null,
            "periapsis_km": null,
            "period_min": null,
            "raan": null,
            "reference_system": "geocentric",
            "regime": "very-low-earth",
            "semi_major_axis_km": null
          },
          "payload_id": "Starlink 8",
          "payload_mass_kg": 15400,
          "payload_mass_lbs": 33951.2,
          "payload_type": "Satellite",
          "reused": false
        }
      ]
    }
  },
  "ships": ["GOMSTREE", "GOMSCHIEF", "GOQUEST", "OCISLY"],
  "static_fire_date_unix": null,
  "static_fire_date_utc": null,
  "tbd": false,
  "telemetry": {
    "flight_club": null
  },
  "tentative_max_precision": "hour",
  "timeline": null,
  "upcoming": false
}

Summary

You executed a simple software robot that fetches data from an API. Congratulations! During the process, you learned some basic features of the RPA Framework.