🧳 Part 1: Day 1 - 3
🧳 Part 2: Day 4 - 6
🧳 Part 3: Day 7 - 9
🧳 Part 4: Day 10 - 12
🧳 Part 5: Day 13 - 15
🧳 Part 6: Day 16 - 18
🧳 Part 7: Day 19 - 21
🧳 Part 8: Day 22 - 24
🧳 Part 9: Day 25 - 27
🧳 Part 10: Day 28 - 30
- 📘 Day 28
- Application Programming Interface(API)
- API
- Building API
- HTTP(Hypertext Transfer Protocol)
- Structure of HTTP
- Initial Request Line(Status Line)
- Initial Response Line(Status Line)
- Header Fields
- The message body
- Request Methods
- 💻 Exercises: Day 28
- Day 29
- Building API
- Structure of an API
- Retrieving data using get
- Getting a document by id
- Creating data using POST
- Updating using PUT
- Deleting a document using Delete
- 💻 Exercises: Day 29
- Day 30
- Conclusions
📘 Day 28
Application Programming Interface(API)
API
Application Programming Interface(API). The kind of API will cover in this section is going to be Web APIS. Web APIs are the defined interfaces through which interactions happen between an enterprise and applications that use its assets, which also is a Service Level Agreement (SLA) to specify the functional provider and expose the service path or URL for its API users. In the context of web development, an API is defined as a set of specifications, such as Hypertext Transfer Protocol (HTTP) request messages, along with a definition of the structure of response messages, usually in an XML or a JavaScript Object Notation (JSON) format. Web API has been moving away from Simple Object Access Protocol (SOAP) based web services and service-oriented architecture (SOA) towards more direct representational state transfer (REST) style web resources. Social media services, web APIs have allowed web communities to share content and data between communities and different platforms. Using API, content that is created in one place dynamically can be posted and updated to multiple locations on the web. For example, Twitter's REST API allows developers to access core Twitter data and the Search API provides methods for developers to interact with Twitter Search and trends data. Many applications provide API end points. An example API. In this section, we will cove a RESTful API that uses HTTP request methods to GET, PUT, POST and DELETE data.
Building API
RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. In the previous sections, we have learned about python, flask and mongoDB. We will use the knowledge we acquire to develop a RESTful API using python flask and mongoDB. Every application which has CRUD(Create, Read, Update, Delete) operation has an API to create data, to get data, to update data or to delete data from database.
To build an API, it is good to understand HTTP protocol.
HTTP(Hypertext Transfer Protocol)
HTTP is an established communication protocol between a client and a server. A client in this case is a browser and server is the place where you access data. HTTP is a network protocol used to deliver resources which could be files on the World Wide Web, whether they're HTML files, image files, query results, scripts, or other file types.
A browser is an HTTP client because it sends requests to an HTTP server (Web server), which then sends responses back to the client.
Structure of HTTP
HTTP uses client-server model. An HTTP client opens a connection and sends a request message to an HTTP server and the HTTP server returns message which is the requested resources. When the request response cycle completes the server closes the connection.
The format of the request and response messages are similar. Both kinds of messages have * an initial line, * zero or more header lines, * a blank line (i.e. a CRLF by itself), and * an optional message body (e.g. a file, or query data, or query output).
Let's an example of request and response messages by navigating this site:https://thirtydaysofpython-v1-final.herokuapp.com/
Initial Request Line(Status Line)
The initial request line is different from the response. A request line has three parts, separated by spaces: * method name(GET, POST, HEAD) * path of the requested resource, * the version of HTTP being used. eg GET / HTTP/1.1 * GET is the most common HTTP to get or read resource and POST a common request method to create resource.
Initial Response Line(Status Line)
The initial response line, called the status line, also has three parts separated by spaces: * HTTP version * Response status code that gives the result of the request, and a reason which describes the status code. Example of status lines are: HTTP/1.0 200 OK or HTTP/1.0 404 Not Found Notes:
The most common status codes are: 200 OK: The request succeeded, and the resulting resource (e.g. file or script output) is returned in the message body. 500 Server Error A complete list of HTTP status code can be found here
Header Fields
As you have seen in the above screenshot, header lines provide information about the request or response, or about the object sent in the message body.
GET / HTTP/1.1
Host: thirtydaysofpython-v1-final.herokuapp.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36
Sec-Fetch-User: ?1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Referer: https://thirtydaysofpython-v1-final.herokuapp.com/post
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,fi-FI;q=0.8,fi;q=0.7,en-CA;q=0.6,en-US;q=0.5,fr;q=0.4
The message body
An HTTP message may have a body of data sent after the header lines. In a response, this is where the requested resource is returned to the client (the most common use of the message body), or perhaps explanatory text if there's an error. In a request, this is where user-entered data or uploaded files are sent to the server.
If an HTTP message includes a body, there are usually header lines in the message that describe the body. In particular,
The Content-Type: header gives the MIME-type of the data in the body(text/html, application/json, text/plain, text/css, image/gif). The Content-Length: header gives the number of bytes in the body.
Request Methods
The GET, POST, PUT and DELETE are the HTTP request methods which we are going to implement an API or a CRUD operation application. 1. GET: GET method is used to retrieve and get information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
-
POST: POST request is used to create data and send data to the server, for example, creating a new post, file upload, etc. using HTML forms.
-
PUT: Replaces all current representations of the target resource with the uploaded content and we use it modify or update data.
-
DELETE: Removes data
💻 Exercises: Day 28
- Read about API and HTTP
Day 29
Building API
An example API. In this section, we will cove a RESTful API that uses HTTP request methods to GET, PUT, POST and DELETE data.
RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. In the previous sections, we have learned about python, flask and mongoDB. We will use the knowledge we acquire to develop a RESTful API using python flask and mongoDB. Every application which has CRUD(Create, Read, Update, Delete) operation has an API to create data, to get data, to update data or to delete data from database.
The browser can handle only get request. Therefore, we have to have a tool which can help us to handle all request methods(GET, POST, PUT, DELETE). Postman is a very popular tool when it comes to API development. So, if you like to do this section you need to download postman
Structure of an API
An API end point is a URL which can help to retrieve, create, update or delete a resource. The structure looks like this: Example: https://api.twitter.com/1.1/lists/members.json Returns the members of the specified list. Private list members will only be shown if the authenticated user owns the specified list. The name of the company name followed by version followed by the purpose of the API. The methods: HTTP methods & URLs
The API uses the following HTTP methods for object manipulation:
GET Used for object retrieval
POST Used for object creation and object actions
PUT Used for object update
DELETE Used for object deletion
To implement this API, we will use: * Postman * Python * Flask * MongoDB
Retrieving data using get
In this step, let's use dummy data and return it as a json. To return it as json, will use json module and Response module.
# let's import the flask
from flask import Flask, Response
import json
app = Flask(__name__)
@app.route('/api/v1.0/students', methods = ['GET'])
def students ():
student_list = [
{
'name':'Asabeneh',
'country':'Finland',
'city':'Helsinki',
'skills':['HTML', 'CSS','JavaScript','Python']
},
{
'name':'David',
'country':'UK',
'city':'London',
'skills':['Python','MongoDB']
},
{
'name':'John',
'country':'Sweden',
'city':'Stockholm',
'skills':['Java','C#']
}
]
return Response(json.dumps(student_list), mimetype='application/json')
if __name__ == '__main__':
# for deployment
# to make it work for both production and development
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
When you request the http://localhost:5000/api/v1.0/students url on the browser you will get this:
In stead of displaying dummy data let's connect the flask application with MongoDB and let's get data from mongoDB database.
# let's import the flask
from flask import Flask, Response
import json
import pymongo
app = Flask(__name__)
#
MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
client = pymongo.MongoClient(MONGODB_URI)
db = client['thirty_days_of_python'] # accessing the database
@app.route('/api/v1.0/students', methods = ['GET'])
def students ():
return Response(json.dumps(student), mimetype='application/json')
if __name__ == '__main__':
# for deployment
# to make it work for both production and development
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
[
{
"_id": {
"$oid": "5df68a21f106fe2d315bbc8b"
},
"name": "Asabeneh",
"country": "Finland",
"city": "Helsinki",
"age": 38
},
{
"_id": {
"$oid": "5df68a23f106fe2d315bbc8c"
},
"name": "David",
"country": "UK",
"city": "London",
"age": 34
},
{
"_id": {
"$oid": "5df68a23f106fe2d315bbc8e"
},
"name": "Sami",
"country": "Finland",
"city": "Helsinki",
"age": 25
}
]
Getting a document by id
We can access signle document using an id, let's access Asabeneh using his id. http://localhost:5000/api/v1.0/students/5df68a21f106fe2d315bbc8b
# let's import the flask
from flask import Flask, Response
import json
from bson.objectid import ObjectId
import json
from bson.json_util import dumps
import pymongo
app = Flask(__name__)
#
MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
client = pymongo.MongoClient(MONGODB_URI)
db = client['thirty_days_of_python'] # accessing the database
@app.route('/api/v1.0/students', methods = ['GET'])
def students ():
return Response(json.dumps(student), mimetype='application/json')
@app.route('/api/v1.0/students/<id>', methods = ['GET'])
def single_student (id):
student = db.students.find({'_id':ObjectId(id)})
return Response(dumps(student), mimetype='application/json')
if __name__ == '__main__':
# for deployment
# to make it work for both production and development
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
[
{
"_id": {
"$oid": "5df68a21f106fe2d315bbc8b"
},
"name": "Asabeneh",
"country": "Finland",
"city": "Helsinki",
"age": 38
}
]
Creating data using POST
We use the POST request method to create data
# let's import the flask
from flask import Flask, Response
import json
from bson.objectid import ObjectId
import json
from bson.json_util import dumps
import pymongo
from datetime import datetime
app = Flask(__name__)
#
MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
client = pymongo.MongoClient(MONGODB_URI)
db = client['thirty_days_of_python'] # accessing the database
@app.route('/api/v1.0/students', methods = ['GET'])
def students ():
return Response(json.dumps(student), mimetype='application/json')
@app.route('/api/v1.0/students/<id>', methods = ['GET'])
def single_student (id):
student = db.students.find({'_id':ObjectId(id)})
return Response(dumps(student), mimetype='application/json')
@app.route('/api/v1.0/students', methods = ['POST'])
def create_student ():
name = request.form['name']
country = request.form['country']
city = request.form['city']
skills = request.form['skills'].split(', ')
bio = request.form['bio']
birthyear = request.form['birthyear']
created_at = datetime.now()
student = {
'name': name,
'country': country,
'city': city,
'birthyear': birthyear,
'skills': skills,
'bio': bio,
'created_at': created_at
}
db.students.insert_one(student)
return ;
def update_student (id):
if __name__ == '__main__':
# for deployment
# to make it work for both production and development
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
Updating using PUT
# let's import the flask
from flask import Flask, Response
import json
from bson.objectid import ObjectId
import json
from bson.json_util import dumps
import pymongo
from datetime import datetime
app = Flask(__name__)
#
MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
client = pymongo.MongoClient(MONGODB_URI)
db = client['thirty_days_of_python'] # accessing the database
@app.route('/api/v1.0/students', methods = ['GET'])
def students ():
return Response(json.dumps(student), mimetype='application/json')
@app.route('/api/v1.0/students/<id>', methods = ['GET'])
def single_student (id):
student = db.students.find({'_id':ObjectId(id)})
return Response(dumps(student), mimetype='application/json')
@app.route('/api/v1.0/students', methods = ['POST'])
def create_student ():
name = request.form['name']
country = request.form['country']
city = request.form['city']
skills = request.form['skills'].split(', ')
bio = request.form['bio']
birthyear = request.form['birthyear']
created_at = datetime.now()
student = {
'name': name,
'country': country,
'city': city,
'birthyear': birthyear,
'skills': skills,
'bio': bio,
'created_at': created_at
}
db.students.insert_one(student)
return
@app.route('/api/v1.0/students/<id>', methods = ['PUT']) # this decorator create the home route
def update_student (id):
query = {"_id":ObjectId(id)}
name = request.form['name']
country = request.form['country']
city = request.form['city']
skills = request.form['skills'].split(', ')
bio = request.form['bio']
birthyear = request.form['birthyear']
created_at = datetime.now()
student = {
'name': name,
'country': country,
'city': city,
'birthyear': birthyear,
'skills': skills,
'bio': bio,
'created_at': created_at
}
db.students.update_one(query, student)
# return Response(dumps({"result":"a new student has been created"}), mimetype='application/json')
return
def update_student (id):
if __name__ == '__main__':
# for deployment
# to make it work for both production and development
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
Deleting a document using Delete
# let's import the flask
from flask import Flask, Response
import json
from bson.objectid import ObjectId
import json
from bson.json_util import dumps
import pymongo
from datetime import datetime
app = Flask(__name__)
#
MONGODB_URI='mongodb+srv://asabeneh:your_password@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
client = pymongo.MongoClient(MONGODB_URI)
db = client['thirty_days_of_python'] # accessing the database
@app.route('/api/v1.0/students', methods = ['GET'])
def students ():
return Response(json.dumps(student), mimetype='application/json')
@app.route('/api/v1.0/students/<id>', methods = ['GET'])
def single_student (id):
student = db.students.find({'_id':ObjectId(id)})
return Response(dumps(student), mimetype='application/json')
@app.route('/api/v1.0/students', methods = ['POST'])
def create_student ():
name = request.form['name']
country = request.form['country']
city = request.form['city']
skills = request.form['skills'].split(', ')
bio = request.form['bio']
birthyear = request.form['birthyear']
created_at = datetime.now()
student = {
'name': name,
'country': country,
'city': city,
'birthyear': birthyear,
'skills': skills,
'bio': bio,
'created_at': created_at
}
db.students.insert_one(student)
return
@app.route('/api/v1.0/students/<id>', methods = ['PUT']) # this decorator create the home route
def update_student (id):
query = {"_id":ObjectId(id)}
name = request.form['name']
country = request.form['country']
city = request.form['city']
skills = request.form['skills'].split(', ')
bio = request.form['bio']
birthyear = request.form['birthyear']
created_at = datetime.now()
student = {
'name': name,
'country': country,
'city': city,
'birthyear': birthyear,
'skills': skills,
'bio': bio,
'created_at': created_at
}
db.students.update_one(query, student)
# return Response(dumps({"result":"a new student has been created"}), mimetype='application/json')
return
@app.route('/api/v1.0/students/<id>', methods = ['PUT']) # this decorator create the home route
def update_student (id):
query = {"_id":ObjectId(id)}
name = request.form['name']
country = request.form['country']
city = request.form['city']
skills = request.form['skills'].split(', ')
bio = request.form['bio']
birthyear = request.form['birthyear']
created_at = datetime.now()
student = {
'name': name,
'country': country,
'city': city,
'birthyear': birthyear,
'skills': skills,
'bio': bio,
'created_at': created_at
}
db.students.update_one(query, student)
# return Response(dumps({"result":"a new student has been created"}), mimetype='application/json')
return ;
@app.route('/api/v1.0/students/<id>', methods = ['DELETE'])
def delete_student (id):
db.students.delete_one({"_id":ObjectId(id)})
return
if __name__ == '__main__':
# for deployment
# to make it work for both production and development
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
💻 Exercises: Day 29
- Implement the above example and develop this
Day 30
Conclusions
In the process of preparing this material I I have learning quite a lot and you have inspired me to do more. Congratulations for making it to this level. If you have done all the exercise and the projects, now you are capable to go a data analysis, data science, machine learning or web development.
GIVE FEEDBACK: http://thirtydayofpython-api.herokuapp.com/feedback