0

I am developing a REST API using python Flask framework. I am not using any external library for generating my API endpoints. I simply register a path with @app.route and make all my db calls and jsonification in a method. Here is an example from one of my api endpoints:

@app.route('/artists', methods=['GET'])
def getArtists():
    con = sqlite3.connect("art.db")
    cur = con.cursor()

    get_artists_query = '''SELECT artist_id, artist_name, profile_image FROM artwolf LIMIT 20'''
    cur.execute(get_artists_query)
    artists = []
    for row in cur:
        artist = {}
        artist['artistId'] = row[0]
        artist['artistName'] = row[1]
        artist['profilePicture'] = row[2]
        artists.append(artist)

    return jsonify({'artists': artists})
    cur.close()
    con.close()

As show above, I have implemented all my endpoints. Now, I want to add filtering parameters to my API. For example, I want to have a API endpoint for search like this: www.example.com/search?q=van or something for filtering. But I can't find any good learning resource for this purpose. I tried to register "?q=van" in @app.route like below:

@app.route('/artisto?lim=<int:artist_id>', methods=['GET'])

But it gives me an 404 error. So my question is, what is the best way of registering query parameters (not enpoints) in Flask. Thank you very much for your help.

  • Thank you very much. I was thinking of requests. But wanted to make sure it was the correct way of doing this. – Javad Knan Jul 09 '15 at 06:25
  • You could do this with 3 lines and sql-alchemy – CESCO Jul 10 '15 at 12:15
  • Why do you need the ?= ? – CESCO Jul 10 '15 at 12:18
  • @CESCO for filtering part of the API. I just tried to do it without SQLAlchemy. it is just for self learning purpose. I am planning to do the same thing with SQLAlchemy, too. – Javad Knan Jul 22 '15 at 07:55
  • try defining the route as app.route('/artists/', method=['GET']) by the way, I have built a rest framework for flask, which makes filtering super easy and decoupled form the views. you can check it out here https://github.com/sebastiandev/peach its more oriented to nosql databases but a sql proxy using sqlachemy could be added quite easily – Sebastian Apr 29 '17 at 18:12

0 Answers0