Custom functions in a REST API

Looking at two of our entities Company and Address. A company has a billingAddress and a profileAddress.

I’m unsure of how to implement a function to set the billing address versus the profile. Here are the options I can see:

  1. Create a new address using a POST to /address. Update the company object at /company with profileAddress = {/* the new id of the address */}

  2. Execute /company/:id?function=updateBillingAddress&{/* rest of parameters go here */}

  3. Execute /updateBillingAddress?company={companyId}&{/* rest of parameters go here */}

  4. Execute /company/:id?billingAddress={ /* address data here */ }

The first method requires two calls and more responsibility on the part of the developer hooking up to the API. However, I’m not sure the second two methods are appropriate structure.

The last one seems like it might be okay, it gives flexibility for updates without holding the responsibility of managing pointers or executing two calls.. still unsure.

Has anyone seen use of methods 2 or 3 before. Are they okay to use? Why / why not? Which would you suggest to use and why?

In my mind, you have two different ways to handle this:

1) One resource to rule them all:

To create a new company:

 `POST /company`

POST body has full details of the company, including both profile address and billing address. As noted by v0idnull, return value should be a 201 Created with a Location header with the URL of the newly created resource.

To set the billing address:

Fetch the current company representation:

GET /company/123

Update the representation with the new billing address, and do:

PUT /company/123

With the full new representation of the company.

PROS

  • Reduce the number of requests for some workflows.
  • Caching is “easier” as you need to only worry about the caching of the one resource.
  • Easier to understand for more API customers.

CONS

  • If you have frequent updates to the company resource for different reasons, you can hit issues with concurrent updates
  • No ability to have fine grained cache control for the different aspects of the company.

2) Separate sub-resources for billing address and profile address

To create a new company:

POST /company

Response looks something like:

201 Created
Location: /company/123
Link: </company/123/billing_address>;rel=billing_address
Link: </company/123/profile_address>;rel=profile_address

Clients can then follow the billing_address or profile_address link relations to update those attributes respectively.

PROS

  • Allows fine grained updates to different aspects of the company with potentially less concurrent update failures.
  • Fine grained caching of each resource.
  • By using hypermedia/links, can restructure URLs/application later.

CONS

  • Requires multiple requests for many workflows (Mobile clients?)
  • Harder to grok for some API consumers not familiar with hypermedia APIs.

Depends…

How I would do it:

PUT /company

The paramters of the company are in the body of the request in any format you wish to support. Should return a 201 Created with the URL of the new company in the Location header.

PUT /address

Same as PUT /company. Same functionality.

Billing address is a sub resource of /company/[company-id] so:

PUT /company/[company-id]/billingAddress

The parameters of the new billing address could be just the URL of the address you created initially.

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