2

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

P.S: 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.

  • 1
    Does this answer your question? https://stackoverflow.com/questions/25149493/how-to-call-another-webservice-api-from-flask – ngShravil.py Mar 28 '20 at 07:53
  • @ngShravil.py unfortunately not because it calls a service through a 'url' which means it will first go to the controller layer and then come into the service layer which I dont wanna do. If I call an external service then it is fine but hence I am calling from my app service that's why I want to avoid this whole way. – Khan Hafizur Rahman Mar 28 '20 at 08:08
  • OK, I am sorry about that, I am not much familiar with this kind of scenario. Anyway. I am up voting this question and I will keep a watch on it. – ngShravil.py Mar 28 '20 at 08:21
  • Nothing to worry. Thanks! One solution can be: to write another method in service class to accept the request body as a parameter and work on those values. But I am curious that whether with one particular method can I solve it or not? like can I create flask request body by myself or any other way which I don't know – Khan Hafizur Rahman Mar 28 '20 at 08:55

1 Answers1

1

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):
   ...