REST API Library Conventions

Most API libraries define one method for each endpoint. If there is an endpoint for getting user information, you might have a method like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>getUserInfo(userId);
</code>
<code>getUserInfo(userId); </code>
getUserInfo(userId);

That simple method is often a wrapper around another function that actually does the api call:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>apiCall('/user/info', {userId: userId});
</code>
<code>apiCall('/user/info', {userId: userId}); </code>
apiCall('/user/info', {userId: userId});

Another option I see less frequently is for the library to only contain the method that makes the API call.

The first method only has the advantages of code discoverability, but it isn’t that big of an issue for dynamic languages. Some say it also removes the need for the developer to read documentation for the APIs, but why would you ever want somebody using APIs without reading the documentation?

The second method has three advantages that I see:

  • The library is dramatically smaller
  • Methods don’t need to be updated if the API ever changes
  • There is no need to maintain documentation for the library; the API documentation fulfills that purpose

But I don’t see the second method used very often, so perhaps I’m missing something. When is it appropriate or inappropriate to use either method of writing API libraries?

1

I would always vote for the getUserInfo(userId) style methods to be included, for these reasons:

  • discoverability;
  • less written documentation needed (as the methods, their context, their names and their parameters document themselves mostly);
  • methods describe available functionality;
  • library may not be significanltly smaller as these methods are themselves really small;
  • hiding details such as JSON <-> Object conversions and error handling;
  • save different users from having to write similar code to use the API;
  • if the API changes, only the library has to change. Otherwise, all code ever written for the API has to change.

Furthermore, with such methods you get the ability to:

  • validate arguments before sending the query;
  • do custom formatting on input arguments and custom parsing on return values, invisible to the library user;
  • put the methods in logical classes and namespaces: cleaner API;
  • use and return custom objects that encapsulate data commonly found together (such as a User or BlogPost objects);
  • include workarounds for bugs or provide a logical/consistent API view of an illogical/inconsistent API (design);
  • convert error return values into exceptions.

getUserInfo(userId) looks like a simple local function call. You don’t care if it’s doing any REST or SOAP connection to retrieve the user, or if’s retrieved from a database. At this level of abstraction it may be valuable to do so; when you’re dealing with business rules involving users, you are not necessarily interested about the details of how this data is retrieved. Also by designing your code this way you have the possibility of changing these details (e.g. modifying the REST interface) without changing all the code that calls this function.

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