LectureV Rest WebService
LectureV Rest WebService
Goals
• Goals
– Understand the concepts and technology related to the web
service technology
– Master Restful service specification
– Develop and test Restful with Flask
3
Service
Service
Source A. Occello
6
Web Services
• Reusable
• Machine-to-machine interactions
• Loose coupling:
– In software development, coupling typically refers to the degree to
which software components/modules depend upon each other. The degree
to which components are linked defines whether they operate in a tightly
coupled relationship or in a loosely coupled relationship.
– Loosely coupled components can operate independently from one another.
• Leveraging the architecture of the World Wide Web: http
• Interoperability: Interoperability is the ability of a system or a product to
work with other systems or products without special effort on the part of the
customer.
• Independently of The language, The platform (UNIX, Windows, ...), The
implementation (VB, C #, Java, ...), The underlying architecture (.NET, J2EE, ...)
RESTful web services
HTTP/1.1 200 OK
Content-Type: text/html
Drawbacks:
<html> ….. </html>
• The client must understand both HTTP and HTML.
• The entire webpage is replaced with another one.
• Same data is usually sent in multiple responses.
• E.g. HTML code for the layout.
• What about Machine to Machine
RESTful web services
Main concepts
Resources
i.e., http://example.com/employees/12345
REST
Operations Representations
i.e., GET, POST i.e., XML, JSON
Ressource
Example of URI:
Example of codes:
2xx - success
200 OK - requests succeeded, usually contains data
201 Created - returns a Location header for new resource
202 Accepted - server received request and started processing
204 No Content - request succeeded, nothing to return
4xx - client error:
400 Bad Request { malformed syntax
401 Unauthorized { authentication required
403 Forbidden { server has understood, but refuses request
404 Not Found { resource not found
405 Method Not Allowed { specied method is not supported
Flask
https://flask.palletsprojects.com/en/3.0.x/
To install Flask in our virtual environment:
pip install flask
To create virtaul environement :
python -m venv myenv
More information: https://medium.com/@dipan.saha/managing-git-repositories-with-
vscode-setting-up-a-virtual-environment-62980b9e8106
Virtual environment
A virtual environment is a self-contained Python environment that allows you to
install and use different versions of Python and its libraries without affecting your
system Python installation.
To create a virtual environment on VSCode
1. Create a folder at any directory, example LoveFlask
2. Open LoveFlask by VSCode
3. Open powershell terminal within VSCode and create a virtual environment called,
for example myenv, using the commande: python -m venv myenv
• To pass parameters to your Rest API, you can use the route/path.
• A parameter can be a string (text) like this: /product/laptop
• Example:
@app.route('/product/<name>’, methods=['GET'])
def get_product(name):
return "The product is " + str(name)
Flask path parameters
@app.route('/product/add', methods=['POST'])
def getProduct():
# get data sent as JSON data
data = request.get_json()
return data['name'] Path Method
if __name__ == '__main__':
app.run()
Flask path parameters
app = Flask(__name__)
@app.route('/product/add', methods=['GET'])
def getProduct():
# Retrieve parameters
arg1 = request.args.get('language')
arg1 = request.args.get('framework')
return arg1
30
End