I fight in two corners over the choice of how to do REST api
I am writing in Python + Flask + MongoDB

Option 1

@app.route("/api/settings/", methods=["POST"]) def ΠΌΠ΅Ρ‚ΠΎΠ΄1: Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ куска ΠΊΠΎΠ΄Π°. @app.route("/api/setup/", methods=["POST"]) def ΠΌΠ΅Ρ‚ΠΎΠ΄2: Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ куска ΠΊΠΎΠ΄Π°. 

Option 2

 @app.route("/api/<method>", methods=["POST"]) def ΠΌΠ΅Ρ‚ΠΎΠ΄3(method): if method == "settings": Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ куска ΠΊΠΎΠ΄Π° elif method == "setup": Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ куска ΠΊΠΎΠ΄Π° 

Help determine what to choose, and why you should choose one or another option?

  • The code in question uses the wrong syntax for Python. - jfs

1 answer 1

In different cases, you can use both options. Preference is defined absolutely the same as for any other functions in Python - in other words, your question is equivalent: in which cases it is necessary to combine two functions into one or vice versa in which cases it is necessary to split one function into two.

As usual, it is desirable to have high cohesion for code inside one function and loose coupling for code from different functions. The question is too common for Stack Overflow. The answer is more appropriate volume of the book, for example: Code Complete (Steve McConnell).