简体   繁体   中英

How to call python api method inside another api in same class?

I have the following api method

@app.route('/api/v1/lessons', methods=['GET'])
def api_lessons():
    if 'courseId' in request.args:
        courseId = request.args['courseId']
    else:
        return "Error: No course id provided. Please specify an course id."

    onto = get_ontology("ontology.owl")
    onto.load()

    result = onto[courseId].contains
    result2 = []

    for i in result:
        temp = "{ id : " + str(i.Identifier) + ", name : " +  str(i.Name) + "}"
        print(temp)
        result2.append(temp)

    return json.dumps(result2)

And I need to add a new method to call this api internally with same args

@app.route('/api/v1/learningPath', methods=['GET'])
def api_learningPath():

    lessons = api_lessons

    return json.dumps(result2)

How to do that?

You need to call the function instead of calling an internal API. Your api_lessons() will return a JSON string and you will need to parse it back to JSON in order to use it. Your function would be like this.

@app.route('/api/v1/learningPath', methods=['GET'])
def api_learningPath():

    lessons = json.loads(api_lessons())

    return json.dumps(result2)

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