CTS 208: Introduction to APIs




Clear To Send: Wireless Network Engineering show

Summary: <br> Application Programming Interface: communication protocol or interface used to interact between different pieces of software.<br> <br> <br> <br> In networking, you could have an API available to interact between a Wireless Controller and a script coded by the network admin.<br> <br> <br> <br> APIs make it very easy to interact with our networking equipment to configure, operate and monitor them.<br> <br> <br> <br> In the networking world, these API communications are often established over the HTTPS protocol and are categorized as <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">RESTful APIs</a>. It stands for REpresentational State Transfer. It is a very popular API framework for web services built on HTTP.<br> <br> <br> <br> REST is popular because it is fast, simple, scalable and easy to work with.<br> <br> <br> <br> Why use an API?<br> <br> <br> <br> * Custom dashboards* Automation* Installs* Configuration* Testing* Troubleshooting<br> <br> <br> <br> How can I use an API?<br> <br> <br> <br> Vendors usually provide extensive API documentation that will tell you how to use them and which data to expect. Sometimes, you have to enable this access (Meraki).<br> <br> <br> <br> You can then use applications such as Postman that will help you to design your API calls. <br> <br> <br> <br> Communications are secured and will require some sort of authorization (using a token or API Key). This key will be referenced in every request via a request header.<br> <br> <br> <br> Methods (API Calls)<br> <br> <br> <br> * GET: returns the value of a resource or a list of resources (Ex: get a list of online APs)* PUT: adds a new resource (Ex: create a new site)* POST: updates a resource (EX: updating the name of an AP)* DELETE: remove a resource (Ex: removing a user)<br> <br> <br> <br> Responses – Status and Error Codes<br> <br> <br> <br> You will get a response from your API calls. These responses use the standard HTTP status codes:<br> <br> <br> <br> * 400: Bad Request* 401: Unauthorized* 403: Forbidden* 404: Not found* 429: Too Many Requests* 500: Internal Server Error* 503: Service Unavailable<br> <br> <br> <br> If everything goes well, you should receive a 200 status code as well as the response to your query (if it was a GET query). This response is generally organized with the JSON format. It becomes easier to read and parse it in a script. <br> <br> <br> <br> This format can also be specified in the headers: Content-Type: application/json<br> <br> <br> <br> Important Things to consider<br> <br> <br> <br> * Limitation of number of calls* 5 calls per second for Meraki* 5000 calls per hour for Mist (to validate)<br> <br> <br> <br> Sample Scripts<br> <br> <br> <br> Rowell’s Script<br> <br> <br> <br> import json<br> import requests<br> site_id = '&lt;your-site-id&gt;'<br> url = "https://api.mist.com/api/v1/sites/{}/wlans".format(site_id)<br> headers = {<br> 'Authorization': 'Basic &lt;your-token&gt;'<br> }<br> response = requests.request("GET", url, headers=headers)<br> r = json.loads(response.text)<br> for wlans in r:<br> wlan_name = wlans['ssid']<br> wlan_enabled = str(wlans['enabled'])<br> wlan_vlan = str(wlans['vlan_id'])<br> print("SSID: " + wlan_name, "\t Enabled: " + wlan_enabled, "\t VLAN: " + wlan_vlan )<br> <br> <br> <br> Sample Output<br> <br> <br> <br> % python3 get_wlans.py<br> SSID: D-NET Enabled: True VLAN: 2<br> SSID: CTS Enabled: True VLAN: 2<br> <br> <br> <br> François’ Script<br> <br> <br> <br> #!/usr/bin/env python3<br> import time<br> import json<br> import requests<br> <br> <br> def main():<br> org_id = '&lt;org-id&gt;'<br> site_id = '&lt;site-id&gt;'<br> token = '&lt;your-token&gt;'<br>