RESTful API: Where should I code my workflow?

后端 未结 4 1939
栀梦
栀梦 2021-02-05 18:29

I am developing a RESTful API. This is my first API, but also my first really big coding project. As such, I\'m still learning a lot about architecture etc.

Currently, I

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 18:56

    You touch on the workflow (aka business logic) part of an API. Technically this is a separate concern from the API part which is the interface. Sure, as you mention, HATEOAS allows you to suggest certain actions which are valid, but you should be careful to maintain statelessness.

    In REST applications, there must not be session state stored on the server side. Instead, it must be handled entirely by the client.

    So, if there's session state on the server, it's not REST.

    For your shopping cart example, you can save state in a separate caching layer like Redis. As for your workflows. You wouldn't want to put business logic like calculating their shopping cart or total bill in a domain model. That would be added to service layer.

    You talked about mischievous users guessing URLs. This is always a concern and should be handled by your security. If the URL to delete a user is DELETE /user/3782 ... they can easily guess how to delete all the users. But you shouldn't rely only on obfuscating the URLs. You should have real security and access checks inside your endpoints checking if each request is valid.

    This is the same solution for your shopping cart concerns You'll need to grant a token which will attach their shopping information and use that to validate each action, regardless if they knew the right URL or not. There are no shortcuts when it comes to security.

提交回复
热议问题