RESTful APIs have become very popular in the last few years by the hand of large sites like Fakebook, Twitter and Github, who give developers the opportunity to extend their services with a wide variety of applications and services.
The HTTP protocol has made it possible to consume the REST APIs from different applications. To consume the REST APIs, one has to make a HTTP request in a specific API endpoint and provide the required data and headers and in return get the desired response.
This tutorial focuses on implementing a Python client for the GitHub API using Requests HTTP library.
Installing
Before starting make sure you have the Requests library installed. If not, you can follow this installation guide: Installation of Requests.
Importing
For using the Requests library in your Python program, first you have to import it.
>>> import requests
Making a Request & working with the Response
For making a GET request, use the get
method and pass the URL as parameter.
>>> response = requests.get('https://api.github.com/users/sandeepm96/repos')
A simple implementation of the GET request is as follows:
import requests | |
response = requests.get('https://api.github.com/users/sandeepm96/repos') | |
assert response.status_code == 200 | |
for repo in response.json(): | |
print repo['name'] |
The above is a simple client which makes a GET request to the user repositories endpoint and waits for a response. The program exits with an assertion error if the request is unsuccessful (status_code is not 200). If the request is successful, the list of repositories of the user gets printed.
For making a POST request, use post
method of the library which takes an URL, request body in data
parameter, and authentication detail in auth
parameter. An example of a POST request is as follows:
import json | |
import requests | |
response = requests.post('https://api.github.com/user/repos', | |
data=json.dumps({'name': 'foo'}), auth=('user', 'pass')) | |
assert response.status_code == 201 | |
print response.json() |
The library also provides methods for PUT and DELETE requests.
Custom Headers
For adding custom HTTP headers to a request, pass them as a dictionary to the headers
parameter.
import requests | |
headers = {'accept':'application/json'} | |
response = requests.get('https://api.github.com/users/sandeepm96/repos', headers=headers) | |
assert response.status_code == 200 | |
for repo in response.json(): | |
print repo['name'] |
Introducing GraphSpace Python client library
GraphSpace Python Client is a client library for the GraphSpace REST API. It simplifies the process of authentication, request construction and response parsing for the developers and allows them to perform all network related operations like constructing, adding nodes and edges, editing and uploading directly from Python based applications.
The code structure of the library is organised into the following:
- Client class: The most important module of the library. It helps in connecting the user with GraphSpace. It has an internal method which helps make a request to the GraphSpace API by taking in parameters such as the request method, the graph data, the URI of the endpoint.
- Endpoint classes: The library has 3 endpoint classes for the graphs, layouts and groups endpoint. Under these classes, methods related to the respective endpoints are defined. These methods call the GraphSpace API to perform the required operation via the Client class’ request making method.
- Object classes: There is an abstract class
ResponseObject
which provides methods to parse the response from the API into objects that can be easily worked upon. On top of it there are object classes for Graph, Layout and Group which encapsulate the graph, layout and group type dict in the response into objects via the parsing method provided by theResponseObject
class. The entire response from the API is encapsulated into a proper object using theAPIResponse
class. - Error Handling class: This class is responsible for raising exceptions whenever a
HTTPError
response is received from the API call. It raises the appropriate GraphSpace error based on the error code received in the response.
Currently, work is going on to improve the library and implement more features.
One thought on “Implementing a Python Client for RESTful APIs”