How do you transfer the method name and parameter value dynamically and then call in Python?

60 views Asked by At

I have write 3 methods,Now I want to implement a function where when the caller passes a method named getResult, I call the getResult method. I tried to use openapi-generator to generate client code and then call it.

import json 
import quart
import quart_cors
from quart import request
app = quart_cors.cors(quart.Quart(__name__))
@app.post("/getResult")
def get_result():
  a = int(request.args.get("a"))
  b = int(request.args.get("b"))
  result = a + b
  return str(result)

 @app.route("/getResult2", methods=["POST"])
  async def get_result2():
    data = await request.get_json()
    a = data.get("a")
    b = data.get("b")
    result = a + b
   return str(result)

  @app.route("/getResult3", methods=["POST","GET"])
    def get_result3(a,b):  
    result = a + b
    return str(result)  
    
 def main():
   app.run(debug=True, host="0.0.0.0", port=5003)

if __name__ == "__main__":
  main()

I tried three methods but could not call them dynamically. How can I change it?The error message is:

TypeError: GetResult2.get_result2() got an unexpected keyword argument 'a'

operation_id = "get_result2" 
operation_func = getattr(api_instance, operation_id, None)

 if operation_func is not None and callable(operation_func):
   try: 
     params = {
        "a": 11,
        "b":45
        }    
    response = operation_func(**params) 
    print("Response:", response)
  except openapi_client.ApiException as e:
    print("Error:", e)
 else:
   print(f"Operation '{operation_id}' not found or not callable.")
0

There are 0 answers