0

I have these 2 routes:

api.add_resource(X, "/<string:stage>/api/sales/by-type")
api.add_resource(Y, "/<string:stage>/api/sales/filters/by-type")

Should it be /by_type or /by-type? /by/type would be weird because /by would be a route by itself, which makes no sense. Can't find any docs about it.

Ericson Willians
  • 7,606
  • 11
  • 63
  • 114
  • 3
    Your question strikes me as more of a API design issue rather than being a Flask related convention. That being said, please have a look at this question => https://stackoverflow.com/questions/10302179/hyphen-underscore-or-camelcase-as-word-delimiter-in-uris. It seems to address the larger design issue. – Nitul Sep 23 '20 at 14:03

1 Answers1

1

@nitul was right, it's about API design in general, but hyphens are commonly used in urls even it's not standard or official but seen as the best practice, seo friendly and urls are more elegant and pretty.

in other hand, i would like to drew your attention about some particular/extra parameters in urls like filters, sorting and pagination, it does more sens to use them as extra arguments ?type=TYPE along with your base/canonical url /<string:stage>/api/sales because the two routes you mentioned are logically the same at the end. Have a look at this good post https://www.moesif.com/blog/technical/api-design/REST-API-Design-Filtering-Sorting-and-Pagination/ it elaborates more the topic with good patterns to adopt. that's been said, you'll need only one route :

api.add_resource(X, "/<string:stage>/api/sales")

and then depending on extra arguments eg: ?type=TYPE in the url you return the appropriate set of objects and this way your API are more compact (you avoid redundancy) and maintainable and extensible.

and as bonus, since you are using Flask and depending on your need (if any), think of Custom URL Converter (this topic https://exploreflask.com/en/latest/views.html#custom-converters will help you)

cizario
  • 3,995
  • 3
  • 13
  • 27