How to design routes for generalized/specialized resources

I hope I can explain what is troubling me.

Example: I have an object basket with a list of fruit objects. So normally I would build my routes like this baskets/{basketId}/fruits/{fruitId). But fruit is an interface and there are different implementations like apples and oranges that all require their own resource representation.

So how do I build my routes now?

Like one of those?:

  • baskets/{basketId}/fruits/{fruitId}/apple,
    baskets/{basketId}/fruits/{fruitId)/orange and so on
  • baskets/{basketId}/fruits/{fruitId}/apples/{fruitId} ,
    baskets/{basketId}/fruits/{fruitId}/oranges/{fruitId}

  • baskets/{basketId}/apples/{fruitId},
    baskets/{basketId}/oranges/{fruitId}

What would be the restful way?

4

The point of an interface is to hide the fact that there are different possible implementations. When you sit in a restaurant, basically you care only that you’ll get service from someone. You may or may not be aware that you might get service from Jose, Marcia, or Stephanie, and you definitely don’t want to have to do different things in order to get the same cheeseburger from different waiters. Your view of the transaction as a consumer is “get a cheeseburger”, not “talk to Jose”/”talk to Marcia”/”talk to Stephanie”.

If the consumer has to know about all the possible implementations, and which one he wants to access, then for purposes of your API you might just as well not have an interface, since you’re not actually saving the effort that it’s supposed to save. It can still be useful to make details of the implementation easier, but as long as the difference between the alternative object classes is externally visible, the interface ceases to be a useful API component.

1

I assume all the fruits, apples, oranges, and so one, all have fruit ids. So if you want to reference a specific fruit in a basket, then:

/baskets/{basketId}/fruits/{fruitId)

is still valid. However, if the “fruitId” is unique, it would me much better to just use

/fruits/{fruitId)

However, if you only want to see certain types of fruits in a basket, then you can take advantage of the query string in it’s role of “narrowing” the resource:

baskets/{basketId}?fruits=mango

or

baskets/{basketId}?fruits=apple&fruits=banana

Once you get that list of items, each should have it’s own ID & you can get them individually if you like.

From a RESTful point of view, there are two resources here:

  • a collection of the fruits in a given basket

  • an individual fruit

Use the query string to limit the collection resource, and use the (shorter) fruitId once you know which specific one you want.

BTW The problem remains the same if you are selecting fruits from someone’s hat:

/hat/{hatId}?fruits=bananas&fruits=pineapples

EDIT: (taken from comments so it will be more readable)

To add an apple

POST /basket/{id}

(the apple representation)

POST is often considered the “append” verb, but you could use PUT. Note that you’re appending the apple to the basket collection, so you use the collection URI.

To find the apples in the basket:

GET /basket/{id}?fruits=apple

EDIT EDIT: If you have some other code/value/attribute in mind, just support that in the query string:

GET /basket/{id}?color=red

6

The most RESTful way would probably be to define separate routes for each fruit type:

baskets/{basketId}/apples
baskets/{basketId}/apples/{appleId}
baskets/{basketId}/bananas
baskets/{basketId}/bananas/{fruitId}
... other fruits

And use a different media type for each fruit, e.g.:

application/vnd.com.example.apple+json
application/vnd.com.example.banana+json

In this case you would need to request a list of every possible type and handle them individually.

A less RESTful solution might be to treat fruit as the interface that it sounds like it is, and just get a list of them.

baskets/{basketId}/fruits
baskets/{basketId}/fruits/{fruitId}

Since JSON is usually schema-less, each item can look completely different, and you could use some identifying characteristic to map each to the specific fruit class which understands its JSON representation and contains its behavior.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật