RESTful APIs in Python. What are RESTful APIs and implementing... | by Shivangi Sareen | Towards Data Science

API is a hypothetical contract between two softwares. Web APIs have made it easy for cross-language applications to work well.
Application Programming Interfaces are commonly used to retrieve data from remote websites. To make a request to a remote web server and retrieve data, we make use of the URL endpoint from where the API is being served. Each URL is called a request and the data that is sent back is called a response.
A RESTful API is an application program interface that uses HTTP requests to GET, PUT, POST and DELETE data. REST based interactions use constraints that are familiar to anyone well known with HTTP. And the interactions communicate their status using standard HTTP status codes.
Data is constantly changingYou want a small piece of a much larger pool of data
Data is constantly changing
You want a small piece of a much larger pool of data
APIs are hosted on web servers.
When we type www.google.com in the browser’s address bar, the computer is actually asking the www.google.com server for a webpage, which it then returns to the browser.
APIs work in much the same way except instead of the web browser asking for a webpage, the program asks for data.
This data is returned in JSON format.
In order to get the data, we make a request to a web server. The server then replies with the data. In Python, we make use of the requests library to do this.
Most commonly used request is the GET request which is used to retrieve data. GET requests will be the focus of this article.
The root endpoint is the starting point of the API we are requesting from. It completes the path which determines the resource that is being requested. E.g.- https://www.google.com/maps. /maps is the endpoint, https://www.google.com is the starting URL.
GET — Get a resource from a server.
POST — Create a new resource on the server.
PUT, PATCH — Update a request on the server.
DELETE — Delete a resource from the server.
1xx: Informational: Communicates transfer protocol-level information.
2xx: Success: Client’s request was accepted successfully.
3xx: Redirection: Client must take some additional action in order to complete their request.
4xx: Client Error: Client side error.
5xx: Server Error: Server side error.
JSON is the primary format in which data is passed back and forth to APIs and most API servers will send their responses in JSON format — a single string containing a JSON object.
JSON is a way to encode data structures like lists and dictionaries to strings that ensure that they are easily readable by machines.
So, lists and dictionaries can be converted to JSON and strings can be converted to lists and dictionaries.
JSON looks much like a dictionary in Python, with key-value pairs stored.
We’ll be implementing a single case scenario which makes up for most scenarios of GET requests.
import jsonimport requestsimport urllib.parse
Some GET requests are made up of endpoints that have path parameters which aren’t optional and very much a part of the endpoint, e.g.- /volunteer/{volunteerID}/badge/{badgeID}
volunteerID and badgeID need to be passed into the complete URL so as to complete the GET request.
This is where we make use of urllib.parse.
Suppose we have a list of volunteerIDs and badgeIDs in a JSON file that we want to input into the endpoint, call the API and get the resulting information.
#reading in the JSON filewith open(‘volunteer_data.json’, ‘r’) as text_file_input:    data=text_file_input.read()#loading that file as a JSON objectobj = json.loads(data)
Next step is to set the URL.
API_ENDPOINT = ‘https://registeredvolunteers.xyz.com/volunteer/{}/badge/{}'
Now, let’s get a for loop running to call the API.
#we pass on the arguments volunteerID and badgeID for i in obj:     r=requests.get(API_ENDPOINT.format(urllib.parse.quote(i[‘volunteerID’]),i[‘badgeID’]))     print(r.text)#outside for looptext_file_input.close()
Note that if you’re expecting a JSON output, r.text will give you a single string consisting of a JSON object. To extract the JSON object from it, we:
output_json = json.loads(r.text)#or directly user.json()
To get the status code and the reason of that status code of the request,
r.status_coder.reason
The above scenario covers the different cases of GET requests — whether you need to pass URL parameters or not, and if those parameters, that are passed in, are stored in a file (or not); and the different properties of GET requests that you can explore to understand the data received.
GET a head start in REST APIs!