Python Forum
rest api parameter in flask - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: rest api parameter in flask (/thread-11082.html)



rest api parameter in flask - bluefrog - Jun-21-2018

Hi

How can I specify a parameter to task 1.

I have a task list, which is triggered by an integer (1 or 2). But in addition to the integer, I would like to pass in additional parameters, not necessarily number types.

This is what I currently have:

#!/usr/bin/python3
from flask import Flask, jsonify, json
import cx_Oracle

con = cx_Oracle.connect('ordb/ordb@//10.11.12.18:1521/FTNODE')

app = Flask(__name__)

tasks = [
    {
        'id': 1,
        'title': 'Customer list',
        'description': 'List of customers currently in the database', 
        'done': False
    },
    {
        'id': 2,
        'title': 'Warehouse List',
        'description': 'List of warehouses currently in the database', 
        'done': False
    }
]

@app.route('/warehouse/api/v1.0/api', methods=['GET'])
def get_tasks():
    return jsonify({'tasks': tasks})

@app.route('/warehouse/api/v1.0/api/<int:task_id>', methods=['GET'])
def get_task(task_id):
    task = [task for task in tasks if task['id'] == task_id]
    if len(task) == 0:
      abort(404)

    print(task_id)
    if task_id==1:
      c = con.cursor()
      query = 'select * from customer'
      result = list(c.execute(query))
      c.close()
      jsonData = json.dumps(result)

    print(task_id)
    if task_id==2:
      c = con.cursor()
      query = 'select * from warehouse'
      result = list(c.execute(query))
      c.close()
      jsonData = json.dumps(result)

    return jsonify({'task': jsonData})

if __name__ == '__main__':
    app.run(port=5001,debug=False)  
    con.close()
Sample output:

$ curl -i http://127.0.0.1:5001/warehouse/api/v1.0/api/1
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 332
Server: Werkzeug/0.14.1 Python/3.6.5
Date: Thu, 21 Jun 2018 13:41:30 GMT

{"task":"[[41, \"Aaron\", \"A.\", \"Aaronson\"], [42, \"Diana\", null, \"Birch\"], [43, \"David\", \"C.\", \"Pack\"], [44, \"Hakim\", null, \"Dogan\"], [45, \"Anita\", null, \"Eaton\"], [46, \"Jasmin\", null, \"Faraday\"], [47, \"Gene\", \"B.\", \"McClene\"], [48, \"Todd\", null, \"Howard\"], [49, \"Irma\", null, \"Howitzer\"]]"}



RE: rest api parameter in flask - wavic - Jun-21-2018

get_task is just a function. You can pass any arguments you want to it along with the task id.


RE: rest api parameter in flask - bluefrog - Jun-21-2018

Perhaps I should've been somewhat clearer.
My question related to the url.

Could I for example have something like this?
@app.route('/warehouse/api/v1.0/api/<int:task_id>/<string:some_value>', methods=['GET'])
In other words, what types can I have as arguments ?


RE: rest api parameter in flask - snippsat - Jun-21-2018

(Jun-21-2018, 03:57 PM)bluefrog Wrote: In other words, what types can I have as arguments ?
Variable Rules
Can make a quick test like this.
As it are now only float works.
from flask import Flask

app = Flask(__name__)
# @app.route('/hello/<name>') # Default string
# @app.route('/hello/<int:name>')
@app.route('/hello/<float:name>')
def hello(name):
    print(type(name))
    return f'Hello {name}'

if __name__ == '__main__':
    app.run(debug=True)
Output:
http://127.0.0.1:5000/hello/3.14 <class 'float'> # printed to shell Hello 3.14