Blockchain Nanodegree 预修知识之三:RESTful APIs(3)

Lesson 3: API Endpoints

API Endpoints with Flask

Hello world

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello, world!"
if __name__ == '__main__':
    app.run()

Json

from flask import jsonify
@app.route('/_get_current_user')
def get_current_user():
    return jsonify(username=g.user.username, email.g.user.email, id=g.user.id)

This will send a JSON response like this to the browser:

{
    "username": "admin",
    "email":"admin@localhost",
    "id":42
}

Making an Endpoint with Flask

  1. Download the endpoints.py file provided in the instructor notes.
  2. Create the appropriate app.route() functions
  3. Test and see if they work using the endpoints_tester.py file

Responding to Different Kinds of Requests

@app.route('/login', methods=['GET', 'POST'])

  1. Add POST, PUT and DELETE methods for the appropriate endpoints in this new version of endpoints_project2.py
  2. Run the endpoint_tester2.py file to check and see if these requests successfully executed.

View the starter code here.

View the solution code here.

Serializing Data from the Database

  1. Download endpoints_project3.py and model.py
  2. Add a serialize decorator to the database model in order to properly create json objects from the results of database queries
  3. Run the endpoint_tester3.py file to see if your serializer worked properly.

View the starter code for this exercise.

View the solution code for this exercise.

Adding Features to your Mashup

  1. Download the starter files

  2. Inside views.py you will add the following routes and python code:

    • /restaurants to view all the restaurants in your database and post a new restaurant(GET, POST)
    • /restaurants/<int:id> to get the specific information about a restaurant, update its name, address, or image or delete it from the database. (GET, PUT, DELETE)
  3. When you are finished , use the tester.py file to test the functionality of your web app’s API endpoints.

View the starter code for this exercise.

View the solution code for this exercise.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值