0

I've created a simple API using Flask, my question is how do i make it list all of the resources or endpoints it has been assigned

from flask import Flask, request
from flask_restful import Resource, Api
from sqlalchemy import create_engine
from json import dumps
import os, random

app = Flask(__name__)
api = Api(app)

class cuddle(Resource):
    def get(self):
        file = random.choice(os.listdir("/path/to/dir/of/images"))
        out = "https://example.com/path/" + file
        return out

api.add_resource(cuddle, '/api/cuddle')

if __name__ == '__main__':
        app.run(host="0.0.0.0", port=33, debug=True)

I'm wanting to be able to go to example.com/api/endpoints and for it to list all of the available endpoints, such as in the snippet cuddle

  • Check out the similar question: https://stackoverflow.com/questions/13317536/get-list-of-all-routes-defined-in-the-flask-app – Chuan Apr 08 '19 at 20:56

1 Answers1

5

You can use app.url_map.iter_values()

Following is an example

@app.route('/')
def hi():
    return 'Hello World!'

@app.route('/hi')
def hi():
    return 'Hi!'


@app.route('/hello')
def hello():
    return 'Hello World!'


def routes():
   routes = []
   for route in app.url_map.iter_rules():
    routes.append('%s' % route)
print(routes)

['/hello', '/hi', '/']

badri
  • 438
  • 3
  • 11