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.