Are you new to coding? This session will get you started using REST, Python. We will cover coding basics and create some simple examples that retrieve, parse and display JSON data using the Cisco APIC-EM REST APIs. Bring your laptop and we’ll help you get your own development environment set up so you can code with us.
CNIC Information System with Pakdata Cf In Pakistan
DEVNET-1001 Coding 101: How to Call REST APIs from a REST Client and Python
1. Coding Skills 101: How to
call REST APIs from
Python
Amanda Whaley – DevNet Community Manager
amwhaley@cisco.com
@mandywhaley
2. Agenda
• Who is this session for?
• REST Web Service Basics
• Try the APIC-EM APIs from
Postman
• Python Environment Setup
• Python Examples
• NEXT UI Toolkit
3. Who is Coding 101 for?
New coders
Returning coders
NetOps
DevOps
4. Congratulations! You have learned:
4
• REST Web Service Basics
• Anatomy of a REST API Request
• GET and POST
• How to call the APIC-EM APIs from Postman
• How to call REST APIs from Python
• Requests Library
5. Disclaimer: Sample Code vs. Real Code
5
The examples and code in this presentation are for Learning and
Educational purposes.
The samples were created with the goals of clarity and ease of
understanding.
If you are writing code for a real application, you would write the code
in a more efficient and structured style.
6. Follow along with the Learning Lab
6
• Login with DevNet/Cisco.com ID
• Coding 101 Lab
• Coding 102 Lab
• Taking it further: Coding 201-207 + APIC-EM Learning Labs
Follow Coding 102 instructions to setup your system
• Chrome
• A text editor (text wrangler, notepad ++, sublime text etc.)
• Postman REST Client -- http://www.getpostman.com/
• Python
• Go to command prompt and type python3 – did it work?
• http://learnpythonthehardway.org/book/ex0.html
• Python Requests library -- http://docs.python-requests.org
https://learninglabs.cisco.com
7. DevNet APIC-EM Always On Sandbox
7
Always there for you to use…
• Register on Cisco DevNet (http://developer.cisco.com ) to get the latest
information about APIs as they are release
• You can use the Always-On APIC-EM Sandbox at anytime
• You can use https://learninglabs.cisco.com at anytime
https://sandboxapic.cisco.com/
9. So what is a REST Web Service?
9
What is a Web Service?
– A way for two systems to communicate through a defined interface.
– Two major types of Web Services – REST or SOAP
What is a REST Web Service?
– REST is an architecture style for designing networked applications.
– A REST web service is a web service that is as easy to call as making an
HTTP request.
– RESTful interfaces often offer the CRUD operations
10. What is so great about REST?
10
• Hosts
• Devices
• Policies
• Applications
Easy to use:
• In mobile apps
• In console apps
• In web apps
How does this work?
Cisco APIC-EM REST APIs
11. How does this work?
8
3rd Party
App
3rd Party
App
Request
Response
Get Hosts
List of Hosts
Cisco APIC-EM
Cisco APIC-EM
12. APIC-EM Example: Get Hosts
12
3rd Party
App
GET http://{APIC-EMController}/api/v0/host/{start}/{no. rec}
List of Hosts returned in JSON
Cisco APIC-EM
APIC-EM Learning labs
Application Policy Infrastructure Controller (APIC) Enterprise Module (EM)
Request
Response
13. Anatomy of a REST Request
13
Method
– GET, POST, PUT, DELETE
URL
– Example: http://{APIC-EMController}/api/v0/host/1/3
Authentication
– Basic HTTP, OAuth, none
Custom Headers
– HTTP Headers
– Example: Content-Type: application/json
Request Body
– JSON or XML containing data needed to complete request
JSON -- JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data
interchange.
16. And what is in the Response?
16
HTTP Status Codes
– http://www.w3.org/Protocols/HTTP/HTRESP.html
– 200 OK
– 201 Created
– 500 Internal Error
Headers
Body
– JSON
– XML
17. REST in Action: How can I try it?
17
HTTP clients can help you quickly test web services
Postman - http://www.getpostman.com/
Firefox RestClient - https://addons.mozilla.org/en-US/firefox/addon/restclient/
Command Line using curl - http://curl.haxx.se/docs/httpscripting.html#GET
SOAPUI
Many IDEs have consoles for testing REST Services built in
We are going to use Postman as an example.
20. REST Demo – Using Postman
20
Get Hosts
– Method: GET
– URL: http://APIC-EMController/api/v0/host/{startIndex}/{recordsToReturn}
Get Devices
– Method: GET
– URL: http://APIC-EMController/api/v0/network-device/{startIndex}/{recordsToReturn}
Get Policies
– Method: GET
– URL: http://APIC-EMController/api/v0/policy/{startIndex}/{recordsToReturn}
Get Applications
– Method: GET
– URL: http://APIC-EMController/api/v0/application/{startIndex}/{recordsToReturn}
21. REST DEMO – Using the POST or PUT Method
21
To send data to a REST service and either create or update data, you will need
to use POST or PUT.
Create Policy Example
– Method: POST
– URL: http://APIC-EMController/api/v0/policy
– Custom Headers: Content-Type: application/json
– Request Body: JSON that specifies details of new policy
What if the Content-Type header is missing?
What if there is a mistake in the JSON Request Body?
Handy tool for validating JSON -- http://jsonlint.com/
23. If you didn't receive a 202 Accepted message, check the following:
Is your URI correct?
Did you choose the method to be Post?
Did you remember to specify the Content-Type header as
application/json?
Did you get a message indicating that the policy already exists?
If so, try it again with a different port (please use only high ports) or host.
Wait that didn’t work!??!
24. Verify that your new policy exists
Delete the policy you created
Next Steps
25. Setup your Python Environment
25
See https://learninglabs.cisco.com : Coding 102 Set up your system instructions
A text editor
– Text Wrangler, Notepad++, Sublime Text, PyCharm Community Edition etc.
Python installed on your system
– Preinstalled on many Linux distributions and Mac OS
– Windows may need to install - http://python.org/download.
– You can check to see if it is installed by typing “python” at a command prompt.
Python Requests library
– http://docs.python-requests.org/en/latest/user/install/#install
Here are some sites that can help you setup your system:
– https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
– http://learnpythonthehardway.org/book/ex0.html
– http://www.codecademy.com/tracks/python
27. Python Examples
27
Coding 102
– apic-em1.py – simple example to get list of hosts
– apic-em-helloworld.py – “hello world” type example to show list of devices
– learning-lab-basics.py – Retrieves device list and pretty prints JSON
– learning-lab-basics-step2.py – Retrieves network device list and parses JSON
to display networkDeviceId values
– learning-lab-basics-step3.py – Retrieves and lists all devices, hosts, policies
and configured applications
– learning-lab-create-policy.py – Shows how to create a new policy using the
POST Method
https://github.com/CiscoDevNet/coding-skills-sample-code
Download Sample Code
28. Python Examples
28
Create a Policy –
– Basic steps
– 1. Get Hosts
– 2. Get Policies
– 3. Create a new Policy
– 4. Get Policies again to show new one that was added
Use POST Method
Set Header
– Content-Type: application/json
What if policy already exists?
A unique combination of hostIP, policy name, and ports is required to add a new
policy.
Source code file: learning-lab-create-policy.py
29. learninglabs.cisco.com
Coding 201 – Coding 207
Next steps with Python
developer.cisco.com
Youtube Videos
Cisco DevNet Github
Get your DevNet Loot reward points!
Where to go from here?
32. About Me
Amanda Whaley
• DevNet Community Manager
• amwhaley@cisco.com
• @mandywhaley
• Lives in Austin, TX.
• developer.cisco.com
• learninglabs.cisco.com
“Those who never fail
are those who never
try.”
-- Ilka Chase