I'm looking for best-practise styles for wrapping a REST API in a light-weight JavaScript client.
In the past I've seen libraries which are implemented in a style like:
var request = new SearchRequest(apikey);
request.query = "search term";
request.send(function(results) {
console.log(results);
});
Or that embrace HTTP more explicitly like:
api.get("search", "search term", function(results) {
console.log(results);
});
api.post("comment", comment, function(results) {
console.log(results);
});
Or that wrap at an even higher level:
api.search("search term", function(results) {
console.log(results);
});
api.createComment(comment, function(results) {
console.log(results);
});
What good examples of modern JavaScript client libraries wrapping REST APIs have you seen recently. Not worried about implementation details, just the API design.
Thanks!
Go Up to DataSnap REST. The Embarcadero REST Library is a framework for accessing REST-based web services (REST stands for Representational State Transfer). The library is available for all platforms that are supported by Delphi. The framework focuses on JSON as the representation format.
For example, a REST API would use a GET request to retrieve a record, a POST request to create one, a PUT request to update a record, and a DELETE request to delete one. All HTTP methods can be used in API calls. A well-designed REST API is similar to a website running in a web browser with built-in HTTP functionality.
A REST API is a way of easily accessing web services. When a RESTful API is called, the server will transfer to the client a representation of the state of the requested resource.
I watched a really good video on good API Design. Definitely worth the watch.
RESTful API Design - Second Edition
You can also get a free book Web API Design - Crafting Interfaces that Developers Love in addition to the above video by going to http://bit.ly/M28lOu
Regarding wrappers it may be worth thinking about the following:
Adopt the standards, naming conventions, etc of the programming language or community you are working with to make the wrapper feel as natural as possible to those using it.
By Wrapping you mimic the REST API methods and structure as much as possible but simply make some of the hard things easier. One of the biggest advantages of this approach is that it makes it easier to upgrade when the target REST API upgrades.
Abstracting is useful when the target REST API is complex or behaves and looks in non-standard ways. In this case your wrapper methods and calls may not resemble your target REST API methods at all but will ultimately simplify the life ( hopefully ) of those using your wrapper.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With