- Hands-On Docker for Microservices with Python
- Jaime Buelta
- 148字
- 2021-06-24 12:35:51
Performing the action
Finally, we get to the specific part where the input data is clean and ready to use, and we know how to return the result. This part likely involves performing some database query or queries and composing the results. Let's look at the following as an example:
@api_namespace.route('/thoughts/')
class ThoughtList(Resource):
@api_namespace.doc('list_thoughts')
@api_namespace.marshal_with(thought_model, as_list=True)
@api_namespace.expect(search_parser)
def get(self):
'''
Retrieves all the thoughts
'''
args = search_parser.parse_args()
search_param = args['search']
# Action
query = ThoughtModel.query
if search_param:
query =(query.filter(
ThoughtModel.text.contains(search_param)))
query = query.order_by('id')
thoughts = query.all()
# Return the result
return thoughts
You can see here, after parsing the parameters, we use SQLAlchemy to retrieve a query that, if the search parameter is present, will apply a filter. We obtain all the results with all(), returning all the ThoughtModel objects.
Returning the objects marshals (encodes them into JSON) them automatically, as we specified in the marshal_with decorator.
推薦閱讀
- 通信網(wǎng)絡(luò)基礎(chǔ)與設(shè)備
- Cisco OSPF命令與配置手冊
- 工業(yè)控制網(wǎng)絡(luò)安全技術(shù)與實(shí)踐
- 網(wǎng)絡(luò)故障現(xiàn)場處理實(shí)踐(第4版)
- 網(wǎng)絡(luò)創(chuàng)新指數(shù)研究
- 智慧城市中的移動(dòng)互聯(lián)網(wǎng)技術(shù)
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- 世界互聯(lián)網(wǎng)發(fā)展報(bào)告·2019
- 網(wǎng)絡(luò)環(huán)境中基于用戶視角的信息質(zhì)量評(píng)價(jià)研究
- 移動(dòng)物聯(lián)網(wǎng):商業(yè)模式+案例分析+應(yīng)用實(shí)戰(zhàn)
- 現(xiàn)代通信系統(tǒng)(第5版)
- 圖神經(jīng)網(wǎng)絡(luò)前沿
- 物聯(lián)網(wǎng)基礎(chǔ)及應(yīng)用
- 移動(dòng)互聯(lián)網(wǎng)新思維
- 現(xiàn)場綜合化網(wǎng)絡(luò)運(yùn)營與維護(hù):運(yùn)營商數(shù)字化轉(zhuǎn)型技術(shù)與實(shí)踐