3 ms·
> The Web is the platform. I am not sure to get exactly what you mean with that. But I was thinking about APIs recently and I will explain how I think it shoul
by gbog 14y ago
> The Web is the platform.
I am not sure to get exactly what you mean with that. But I was thinking about APIs recently and I will explain how I think it should be done in the best of the worlds, it may have some relation with this assertion.
First a note: I don't mean APIs can be like what I describe, nor that businesses should do like that, nor that it is easy or has no problems. It is just a point drawn in the plane for better distance measures.
When I point my browser to http://someurlfoo.bar/user/A http://someurlfoo.bar/user/A, the server knows I am using a browser, knows who I am, and answers with a mix of HTML, javascript and other resources.
Now if you check any API available you almost always have an equivalent GET http://api.someurlfoo.bar/user/A http://api.someurlfoo.bar/user/A, and the server will answer with some XML or JSON blob containing the information required (and often more).
Setting authorization, optimization and bandwith issues aside for 5mn, one could just wonder why the ### those two interfaces are different. Why not read the ACCEPT header and answer with JSON, XML or HTML accordingly, serving apps, direct users and other client through the same dumbwaiter?
Why not having just one interface?
Benefits would be interesting:
- user A would be able to present itself ("A, the name's A") in different languages: html, xml, json, whatever.
- the information about a given topic should have the same content in every languages. Just take the data from the store and filter it through a jsonizer, an xmlizer or a htmlizer.
- API client code would not have to read awful API docs that are obviously written by robots for robots: they would just change point their browser to the data they want, change the accept header to json and get their answer.
It seems to me that, APIs having historically grown after normal web pages, they have been conceived as a separate service, but they are just the same core thing.
Now let's go with possible objections:
- Authorization should be handled the same way, but it is possible that the server would need to ensure that you are coming from which mobile app, or web app. It is easy to do with a double authentication: the app authenticate itself and passes along end user authentication token. This way the server knows who it is speaking to, through which intermediaries.
- Bandwith: An important use for APIs nowadays, from the server's point of view, is the ability to avoid being flooded by bogus requests. An API key comes with limitations on the maximum amount of requests per day or the like. If both the end user and the client app authenticate themselves, then there should be no difficulty in giving priority to some clients over others (usually the direct browser access is preferred).
- Optimization: This is maybe a bigger issue. From the APIs I have seen they seem carefully crafted to reduce the number of roundtrips required. For instance, a GET request for user/A might answer with A's name, number of followers, list of recent followers, same with followings, A's recent posts, creation date, last dog-walking time and culinary preferences. All of this makes no sense if all you need is A's full name. I think this optimization issue should be solved differently, even if we were to keep the current isolated status of APIs, but this if for another post.
Edit: s/hatch/dumbwaiter/
- lmm 14y agoThis has been tried; the big idea of xhtml+xsl was that your server would answer any request with an xml api response and some xslt that transformed it into a webpage. Now admittedly part of the reason that failed is that xslt is a pile of crap, but even if it hadn't been the aproach is wholly unsuitable for building modern, responsive UIs. The behavior you want from an API - consistency, separation of concerns -is completely at odds with what you want on a page, and there are a whole lot of concerns you want to hande server-side (e.g. pagination) but that don't make sense for APIs. It's not that no-one's thought of this, it's that the current approach, where you have a thin but distinct server-side presentation layer (or even client-side javascript - the point is that you need a full-eatured program to turn go from API calls to good UI), works better in practice.
- gbog 14y agoOk, but what if you build a JS client that is calling an API to build the pages? Then there should be only one API, I guess.
- lmm 14y agoThat works (though you might have trouble with older browsers), but you need to treat it as a "real" program, and you wouldn't want to serve it from the same endpoint as your API using accept headers like you're suggesting. And it's not really any different from the current way of doing things; it's always been good practice to build your web tier on top of your API.