当前位置: 动力学知识库 > 问答 > 编程问答 >

How to design REST API for non-resource entities

问题描述:

I'm working on a very simple e-commerce application where the UI/html talks to server through a set of RESTful APIs (ASP.NET Web API, to be specific).

When following REST properly, APIs expose resources. That works well when working with my domain objects like Product, User etc.

But there are some functionalities that doesn't follow this pattern. Say for example, when user enters an order number, I want to check if that is a valid order number. Now I can do a simple GET on orders API and return not found for invalid order

GET http://myhost.com/api/orders/SomeOrderNumber -> http 404

But is it correct approach? What if I want to return an error code and message? (which is obviously not part of order object)

What about entities which are not even part of my domain. e.g. I want to validate a postcode based on some parameters and conditions. I realy don't want something like

GET http://myhost.com/api/postcodes/SomePostCode -> validation message

What kind of APIs should be exposed, if I want to get the count of users currently logged on? If someone can point me to any earlier post or useful documentation, that'll be really helpful.

网友答案:
  1. A GET can't properly return a null response. Even an empty response implies the 204 No Content status. You should return 404 Not Found and you may explain why that order number is invalid on the error message body.

  2. Whether some entity is part of your domain or not, that should be transparent to the client. Again, the example you describe is the correct approach.

  3. To return the count of users currently logged on you need to store session state on the server, and doing that wouldn't be RESTful in the first place. You might want to have a collection resource that can be filtered by queries, and you can filter by the last time an user was seen. For instance:

    GET /api/users?last_seen_since=2014-10-31 0:00:00

And in the response you can have a metadata field or header indicating the total size of that query.

网友答案:

Though its an old post, still would like to post my reviews as it might help others.

What we're doing in our project is like, we've created EntityResultModel as a return type for all the request.

    /// <summary>
    /// Entity Result Model
    /// </summary>
    public class EntityResultModel
    {
        /// <summary>
        /// Gets or sets a value indicating whether this instance is success.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is success; otherwise, <c>false</c>.
        /// </value>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// Gets or sets the message.
        /// </summary>
        /// <value>
        /// The message.
        /// </value>
        public string Message { get; set; }

        /// <summary>
        /// Gets or sets the result data.
        /// </summary>
        /// <value>
        /// The result data.
        /// </value>
        public object ResultData { get; set; }
    }

All WebApi request will return an object of this EntityResultModel, and using this object you can verify if request is succeeded or not, you can even populate message and also return data in data property if required.

分享给朋友:
您可能感兴趣的文章:
随机阅读: