Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
There is an APi1(master) which will randomly select number 2 or 3. If it is 2 it should call api2 and if it is 3 it should call api3. API1 should not wait for the response from api2. It should call api3 immediately .Once after the specific task has been performed by api2 and api3 , they should respond to api1 stating that task has been performed by api2/api3

This is the below code I am trying with

What I have tried:

<pre>from flask import Flask, request
from flask_restful import Api, Resource
import requests
import time

app=Flask(__name__)
api=Api(app)

class Ms1(Resource):
  def get():
    x=random.randint(1,3)
    if x==2: #need to call api2
      
    

     
class Ms2(Resource): #performing writing to a file task by api2
  def get(self):
    L=["This is Hyderabad \n","Hello Hyderabad \n"]
    time.sleep(7)
    with open('readme.txt', 'w') as f:
      for line in lines:
        f.write(line)
        f.write('\n')    #need to respond to api1 that task is completed

class Ms3(Resource):  #performing addition of 2 numbers using post task by api3
  def post(self):
    postedData=request.get_json()
    num1=postedData["num1"]
    num2=postedData["num2"]
    res=num1+num2
    retJson={
      "status":200,
      "output"=res
     }
    return retJson

api.add_resource(Ms1,"/ms1")
api.add_resource(Ms2,"/ms2")
api.add_resource(Ms3,"/ms3")

if __name__=='__main__':
  app.run(debug=True)




Can we write the above 3 API's in a separate file and make a config.json file to include the urls so that each API runs on different server ? Anyone can provide me an idea on this ?
Posted
Updated 30-Mar-22 0:39am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900