Coding 102: Calling REST APIs from Python

In this Learning Lab you will learn the basics of how to call a REST API in Python.

Objective

Completion Time: 35 minutes

  • Understand how to call a REST API from Python
  • Explore ways to parse and use the JSON returned from the REST API

Prerequisites

Background

Access to an APIC-EM Controller

  • To run these code samples, you will need access to an APIC-EM controller.
  • If you are not using your own APIC-EM Controller, use the DevNet Sandbox Always-On APIC-EM Lab: https://devnetapi.cisco.com/sandbox/apic_em.

Python

  • To run the code samples, you need to have Python 3 installed on your machine.
  • If you are working on a DevNet Learning Lab PC at a DevNet event, Python 3.4.2 is already installed.
  • See How to Set up Your Own Computer section above for how to install Python on your own machine.

Python Requests Library

  • These code samples use the Python Requests Library to simplify making REST API calls.
  • If you are working on a DevNet Learning Lab PC at a DevNet event, the Requests Library is already installed.
  • See How to Set up Your Own Computer section above for how to install the Requests Library on your own machine.

Python Flask Library

  • Step 6 uses use the Python Flask Library to interface with NeXt UI.
  • If you are working on a DevNet Learning Lab PC at a DevNet event, the Flask Library is already installed.
  • See How to Set up Your Own Computer section above for how to install the Flask Library on your own machine.

Clone Git Repo

  • If you are working on a DevNet Learning Lab PC at a DevNet event,
    • Open the Git Command window by clicking on the Git CMD icon on the Task Bar or click on the Start button, then in the Run bar type: git cmd and press the Enter key.
  • If you are working from your own workstation, on your desktop open a command terminal.
  • Go to the root directory by typing: cd \
  • Create a directory called 'C:\DevNetCode\yourname' by typing: mkdir DevNetCode\<your-name>
    • For example: mkdir DevNetCode\brTiller
  • Go to the new directory by typing: cd \DevNetCode\<your-name>
    • For example: cd \DevNetCode\brTiller
  • Clone the coding skills sample code from GitHub. Enter the command below.
    git clone https://github.com/CiscoDevNet/coding-skills-sample-code
    


    Inside the directory you created you should now see directory 'coding-skills-sample-code'.



Step 1. Find the APIC-EM API Resources

We are going to use the APIC-EM API as an example of a REST API in this lab. So before we begin, we need to locate the APIC-EM resources on DevNet.

  1. In the browser navigate to DevNet
    • Click on the Login link located on the top right of the web page.

    • On the Log In web page enter your log in credentials (CCO ID) and click the Log In button
      • If you do not have a Cisco Connection Online ID (CCO ID) click the Register Now button and follow the instructions. When done return to the Log In web page and enter your credentials.

  2. Use the menu at the top to navigate directly to the APIC-EM developer resources.

    • Click on the Technologies link to access the Technology menu. Inside the menu click on Networking followed by APIC-EM


  3. You are now at the APIC-EM web portal. Open the API reference doc in a separate tab while you are working on this lab.