Understanding REST: Implementation
April 25, 2013 3 Comments
REST web APIs
A ReST web API is a web implementaion of principles of REST using HTTP. It is collection of resources, with defined facet:
- the base URI for the API, such as http://www.iLearnStack.com/phonebook/UserDetails/
- the internet media type of the data supported by the web API, such as JSON, XML
- the set of operations supported by the API, such as GET, PUT, POST, DELETE ( known as request methods for HTTP)
- the API must be hypertext driven
Considering a generic example, the standard output format of request methods can be consolidated as:
Resource: http://www.iLearnStack.com/phonebook/UserDetails/ (Collection URI)
GET: List the details of addressed methods (in given scenario; UserDetails)
PUT: Replace the enire addressed method with another addressed method
POST: Create a new entry in the addressed method
DELETE: Delete the entire addressed method
Resource: http://www.iLearnStack.com/phonebook/UserDetails/12345
GET: Retrieve the representation of the addressed member (in given scenario; 12345) of collection
PUT: Replace the addressed member of the collection, or if it doesn’t exist, create it.
POST: Not generally used. Treat the addressed member as a collection in its own right and create a new entry in it.
DELETE: Delete the addressed member of the collection.
Using REST in Python
Issuing GET Requests
The Python module urllib2 is used to read URLs:
import urllib2 url = 'http://www.iLearnStack.com/phonebook/UserDetails/12345' response = urllib2.urlopen(url).read()
Issuing POST Requests
A POST Request is simply passing request data as an encoded parameter to urlopen, hence:
import urllib2
import urllib
url = 'http://www.iLearnStack.com/phonebook/UserDetails/12345'
parameters = urllib.urlencode({
'name': 'alpha',
'surname;: 'beta'
})
response = urllib2.urlopen(url, parameters).read()
REST: Openstack Implementaion
Getting Credentials
Credentials are a combination of user name, password, and what tenant (or project) cloud is running under. An additional token need to be generated if interacting with cloud directly with API endpoints, and not with a client. The Cloud administrator can provide a user name and a password as well as tenant identifier so that authorization tokens could be generated.
The tokens generated are typically good for 24 hours. The work flow goes like:
- Begin API requests by asking for an authorization token from the endpoint your cloud administrator gave you. You send your user name, password, and what group or account you are with (the “tenant” in auth-speak).
curl -k -X 'POST' -v https://arm.trystack.org:5443/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "joecool", "password": "coolword"}, "tenantId":"5"}}' -H 'Content-type: application/json'
- The server returns a response in which the 24-hours token is contained. Use that token to send API requests with the X-Auth-Token included as an header field.
curl -k -D - -H "X-Auth-Token: 7d2f63fd-4dcc-4752-8e9b-1d08f989cc00" -X 'GET' -v https://arm.trystack.org:9774/v1.1/296/extensions -H 'Content-type: application/json'
- Repeatedly send API requests with that token in the X-Auth-Token header until either: 1) the job’s done or 2) you get a 401 (Unauthorized) code in return.
- Request a token again when you get 401 (Unauthorized response) response.
Sending Requests to the API
A couple of options for sending requests to OpenStack through an API is available. Developers and testers may prefer to use cURL, the command-line tool. With cURL HTTP requests can be sent and responses are received back from the command line.
For a graphical interface, the REST client for Firefox also works well for testing and trying out commands.
Tokens need to be generated if cURL or a REST client are used.
Following example illustrates the curl commands that can check the list of servers.
curl -v -H "X-Auth-Token:tokengoeshere" http://127.0.0.1:8774/v2/tenantnnnnnn/servers
The output returned for the number of running servers looks like:
{
"servers": [
{
"id": "server***",
"links": [
{
"href": "http://127.0.0.1:8774/v2/tenantnnnnnn/servers/server***",
"rel": "self"
},
{
"href": "http://127.0.0.1:8774/tenantnnnnnn/servers/server***",
"rel": "bookmark"
}
],
"name": "Test Server"
}
]
}
For detailed APIs, available for Openstack this link can be referred to.
References