Flask-RESTful API: multiple and complex endpoints Flask-RESTful API: multiple and complex endpoints python python

Flask-RESTful API: multiple and complex endpoints


Your are making two mistakes.

First, Flask-RESTful leads you to think that a resource is implemented with a single URL. In reality, you can have many different URLs that return resources of the same type. In Flask-RESTful you will need to create a different Resource subclass for each URL, but conceptually those URLs belong to the same resource. Note that you have, in fact, created two instances per resource already to handle the list and the individual requests.

The second mistake that you are making is that you expect the client to know all the URLs in your API. This is not a good way to build APIs, ideally the client only knows a few top-level URLs and then discovers the rest from data in the responses from the top-level ones.

In your API you may want to expose the /api/users and /api/cities as top-level APIs. The URLs to individual cities and users will be included in the responses. For example, if I invoke http://example.com/api/users to get the list of users I may get this response:

{    "users": [         {            "url": "http://example.com/api/user/1",            "name": "John Smith",            "city": "http://example.com/api/city/35"        },        {            "url": "http://example.com/api/user/2",            "name": "Susan Jones",            "city": "http://example.com/api/city/2"        }    ]}

Note that the JSON representation of a user includes the URL for that user, and also the URL for the city. The client does not need to know how to build these, because they are given to it.

Getting cities by their name

The URL for a city is /api/city/<id>, and the URL to get the complete list of cities is /api/cities, as you have it defined.

If you also need to search for cities by their name you can extend the "cities" endpoint to do that. For example, you could have URLs in the form /api/cities/<name> return the list of cities that match the search term given as <name>.

With Flask-RESTful you will need to define a new Resource subclass for that, for example:

    class CitiesByNameAPI(Resource):        def __init__(self):            # ...            def get(self, name):            # ...    api.add_resource(CitiesByNameAPI, '/api/cities/<name>', endpoint = 'cities_by_name')

Getting all the users that belong to a city

When the client asks for a city it should get a response that includes a URL to get the users in that city. For example, let's say that from the /api/users response above I want to find out about the city of the first user. So now I send a request to http://example/api/city/35, and I get back the following JSON response:

{    "url": "http://example.com/api/city/35",    "name": "San Francisco",    "users": "http://example/com/api/city/35/users"}

Now I have the city, and that gave me a URL that I can use to get all the users in that city.

Note that it does not matter that your URLs are ugly or hard to construct, because the client never needs to build most of these from scratch, it just gets them from the server. This also enables you to change the format of the URLs in the future.

To implement the URL that gets users by city you add yet another Resource subclass:

    class UsersByCityAPI(Resource):        def __init__(self):            # ...            def get(self, id):            # ...    api.add_resource(UsersByCityAPI, '/api/cities/<int:id>/users', endpoint = 'users_by_city')

I hope this helps!


you can do the id/name thing without duplicating the resource:

api.add_resource(CitiesByNameAPI, '/api/cities/<name_or_id>', endpoint = 'cities_by_name')class CitiesByNameAPI(Resource):    def get(self, name_or_id):        if name_or_id.isdigit():            city = CityModel.find_by_id(name_or_id)        else:            city = CityModel.find_by_name(name_or_id)        if city:            return city.to_json(), 200        return {'error': 'not found'}, 404

not sure if there are any negative effects from this.