简体   繁体   中英

How to call another service post method from another service in flask

I am developing a flask application separating API and the service layer. For example, I have one service named ServiceOne and another ServiceTwo. I want to invoke serviceTwo's post method from serviceOne. Hence serviceTwo's post method read data using flask request then what is the right way to do that?

#service 1:

service_two = ServiceTwo()
req_data = {'': '', '':''}
service_two.post()

#service 2:
from flask import request

def post():
  req_data = request.json

PS: I want to avoid going through controller layer to access the service because as far I know this is not good practice to call controller layer within the service layer.

I think it in a complex way. You can easily send to post method the number of parameters you want. So the request.json can be send to service layer from api layer as a parameter.Therfore, even if you call from another service you just need to call that method with the appropriate parameters

#service 1:

service_two = ServiceTwo()
req_data = {'': '', '':''}
service_two.post(req_data=request_data)

#service 2:
from flask import request

def post(req_data):
   ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM