Introduction to REST

and its implementation using Flask in Python.
Murtadha Bazli Tukimat

Developer/Founder at Wekanta

bazli@wekanta.com
Wekanta
REST
๏ Background
๏ HTTP Method
๏ Path Format
๏ Response Code
Wekanta
What is REST?
• REpresentational State Transfer (REST)

• An architectural style for standards communication
between computer system over the web

• RESTful application is reliable, quick and scalable.
Wekanta
Request
Response
Client Server
HTTP Method
• GET – retrieve a specific resource (by id) or a
collection of resources. Most common method.

• POST – create a new resource once.

• PUT – similar to POST but might overwriting old
values multiple times due to lost connection.

• DELETE – remove a specific resource by id

• Essential for Create, Read, Update and Delete
operations (CRUD)
Wekanta
Path Format
• Should be designed to help client know what is going on.

• First path should be plural form such as products,
customers, books etc.
Wekanta
# Example

http://api.webapp.com/products/engineering/20
IDCollection CategoryBase Path
Response Code
• GET – return 200 (OK)

• POST – return 201 (Created)

• PUT – return 200 (OK)

• DELETE – return 204 (if document not exist)
Wekanta
Read it more here … https://goo.gl/gfFxsu
FLASK
๏ Background
๏ Hello World!
๏ HTTP Method
๏ Route
๏ Variable
๏ Logging
๏ Try it!
Wekanta
What is Flask?
• Micro web framework written in Python

• Lightweight and little dependency

• Simple to get started

• Built-in server for debugger
Wekanta
Read it more here … https://goo.gl/5zocHd
Hello World!
#!/usr/local/bin/python3



from flask import Flask

app = Flask(__name__)
@app.route('/') # bind 127.0.0.1:5000/ to route '/'

def hello():

return "Hello World!"


if __name__ == '__main__':

app.run() # run local server at port 5000
Wekanta
HTTP Method
Wekanta
from flask import request
@app.route('/hello', methods=['GET', 'POST'])

def hello():

if request.method == 'POST':

return "Do insert here…"
• Default access for GET request

• Common methods GET, HEAD, POST, PUT, DELETE, OPTIONS

• Return 405 if method request is not allowed
Route
Wekanta
@app.route('/hello')

def hello():

return "Hello World!"
• route( ) decorator used to bind URL to a method

• Take care of plural and singular in route (books or book?)

• Route name must be in lowercase
Variable
Wekanta
@app.route('/products/<int:doc_id>')

def get_products(doc_id):

return "Products # %d Informations." % doc_id
• <your_variable_name> here

• Accept integer and floating point value

• Unmet variable type will return status 404
Logging
Wekanta
if __name__ == '__main__':

handler = RotatingFileHandler(

'foo.log', maxBytes=1000000, backupCount=5)



log = logging.getLogger('werkzeug')

log.addHandler(handler) # save to log file

app.logger.addHandler(handler) # see in terminal

app.run()
• FileHandler vs. RotatingFileHandler

• Optional log levels for Debug, Info, Warning and Error

• Split log file for easy debugging
Try It!
Wekanta
# Download Link

https://goo.gl/aVtQJd
Q&A
Wekanta

Introduction to rest using flask

  • 1.
    Introduction to REST
 andits implementation using Flask in Python. Murtadha Bazli Tukimat
 Developer/Founder at Wekanta
 bazli@wekanta.com Wekanta
  • 2.
    REST ๏ Background ๏ HTTPMethod ๏ Path Format ๏ Response Code Wekanta
  • 3.
    What is REST? •REpresentational State Transfer (REST) • An architectural style for standards communication between computer system over the web • RESTful application is reliable, quick and scalable. Wekanta Request Response Client Server
  • 4.
    HTTP Method • GET– retrieve a specific resource (by id) or a collection of resources. Most common method. • POST – create a new resource once. • PUT – similar to POST but might overwriting old values multiple times due to lost connection. • DELETE – remove a specific resource by id • Essential for Create, Read, Update and Delete operations (CRUD) Wekanta
  • 5.
    Path Format • Shouldbe designed to help client know what is going on. • First path should be plural form such as products, customers, books etc. Wekanta # Example http://api.webapp.com/products/engineering/20 IDCollection CategoryBase Path
  • 6.
    Response Code • GET– return 200 (OK) • POST – return 201 (Created) • PUT – return 200 (OK) • DELETE – return 204 (if document not exist) Wekanta Read it more here … https://goo.gl/gfFxsu
  • 7.
    FLASK ๏ Background ๏ HelloWorld! ๏ HTTP Method ๏ Route ๏ Variable ๏ Logging ๏ Try it! Wekanta
  • 8.
    What is Flask? •Micro web framework written in Python • Lightweight and little dependency • Simple to get started • Built-in server for debugger Wekanta Read it more here … https://goo.gl/5zocHd
  • 9.
    Hello World! #!/usr/local/bin/python3
 
 from flaskimport Flask
 app = Flask(__name__) @app.route('/') # bind 127.0.0.1:5000/ to route '/'
 def hello():
 return "Hello World!" 
 if __name__ == '__main__':
 app.run() # run local server at port 5000 Wekanta
  • 10.
    HTTP Method Wekanta from flaskimport request @app.route('/hello', methods=['GET', 'POST'])
 def hello():
 if request.method == 'POST':
 return "Do insert here…" • Default access for GET request • Common methods GET, HEAD, POST, PUT, DELETE, OPTIONS • Return 405 if method request is not allowed
  • 11.
    Route Wekanta @app.route('/hello')
 def hello():
 return "HelloWorld!" • route( ) decorator used to bind URL to a method • Take care of plural and singular in route (books or book?) • Route name must be in lowercase
  • 12.
    Variable Wekanta @app.route('/products/<int:doc_id>')
 def get_products(doc_id):
 return "Products# %d Informations." % doc_id • <your_variable_name> here • Accept integer and floating point value • Unmet variable type will return status 404
  • 13.
    Logging Wekanta if __name__ =='__main__':
 handler = RotatingFileHandler(
 'foo.log', maxBytes=1000000, backupCount=5)
 
 log = logging.getLogger('werkzeug')
 log.addHandler(handler) # save to log file
 app.logger.addHandler(handler) # see in terminal
 app.run() • FileHandler vs. RotatingFileHandler • Optional log levels for Debug, Info, Warning and Error • Split log file for easy debugging
  • 14.
    Try It! Wekanta # DownloadLink
 https://goo.gl/aVtQJd
  • 15.