REST API Design Optimization for Multiple Collection in Single Call Mode

I have 2 resources /usersand /products, and I can get user products with /users/{id}/products. It is simple enough and I am returning a JSON response as follows:

{
   "items": {
      ...
   }
}

Now, if I wanted to get userthem productsat the same time? I understand that I could use ?expand=productsto call /users/{id}and return data as follows:

{
   "item": {
      ...
      "products": {
          ...
      }
   }
}

But is this the best practice? Or is it better to return something like this:

{
   "user": {
      "item": {
         ...
      }
   }
   "products": {
      "items": {
         ...
      }
   }
}

So my questions are:

  • What is the best practice of returning multiple data collections in a single call?
  • , API, , , . user, products, currencies .. api , ?
+4
3

, /users/{id}/products /users/{id}?expand=products - , ( ), URL- . :

{
    username: "Dave",
    userid: 123,
    products: [
        {
            productname: "amazing product",
            productid: "ab123"
        }
    ]
}

-

2, , , ( /, "",), , ..

+3

- - ( ):

{
    "users: [
        {
            ...
            "products": [
                {
                    ...
                }
            ]
        }
    ]
}

"items", , , . , , . , , "item" / "items", . , , .

. , , , , , , , . (, ) , , , , (, ), , - .

+1

, 75 77 -, , (http://www.slideshare.net/stormpath/elegant-rest-design-webinar).

, () (, " ", " " ..), -, , . :

{
    "account" : {
       "id" : "123",
       "user": {
          "items": {
             ...
          }
       }
       "products": {
          "items": {
             ...
          }
       }
    }
}

- , -, , , , .

, , . , /users/ {id}/products , ( , ), :

{
    "user" : {
       "id" : "123",
       "products": [{
          "id" : "A456",
          "owns" : true,
          "details": {
             ...
          }
       }]
    }
}

, , " " , :

/users/{id}/products?ownership=all

, " , , , ".

/users/{id}/products?ownership=owned

Similarly, "give me only those products that this user really owns."

In a resource-oriented architectural style for REST, query parameters are suitable for selection (for example, search, which rows), pagination (how many), sorting (what order), and forecast (which columns).

The request parameter "property" fits into the selection / search category.

0
source

All Articles