Since microservices is the silver bullet of today (or yesterday?) we have a lot of REST requests going forth and back between these services, often with libraries like Ktor. We define routes and check if they match the path and HTTP verb. But why do we need this hassle? Why do the developer need to care about HTTP and REST at all? Why can’t I just mark a class as a “resource” (or whatever we will call it), and then be able to use its methods from outside.
I have been asking about statically typed REST APIs before. A huge problem with REST APIs is that they are dynamically typed. I can send a String when a Boolean is required. The REST API would have to return a HTTP 4xx code for illegal input (I don’t even remember these codes). Why can’t we have static type checking on these APIs?
If we on the backend implement
@Resource("/companies")
class CompanyResource {
fun getBankruptCompanies(from: Date, to: Date): List<Company> {
//blabla
return bankruptCompanies;
}
}
When I develop the front-end, I want to be able to write companyResource
, then type a dot and get suggested getBankrupCompanies
with from and to as parameters. I don’t want to communicate with backend-developers or go to Swagger to see what to call and which types it expects. I don’t want to care about HTTP or REST or 404, 406 or any other HTTP code that I don’t even remember. I just want to interact with the backend or other services as I would interact within the same service.
So the question is: Why not make this part of the language? If something is marked as a resource, the call gets executed behind the scenes, and the types are statically validated. Common types will be returned. On the frontend we would have the Company-type available and use it directly instead of parsing JSON and all that stuff. Just let things happen behind the scenes.
A call to companyResource.getBankruptCompanies(fromDate, toDate)
could be translated to something like
GET /v10/companies/bankruptCompanies/2020-12-01T15:30:00Z/2020-12-20T15:30:00Z
.
The developer would be super happy by not having to think about HTTP and all these error codes. Error codes could be translated to exceptions. Then when front-end (or another service) receives a 404, that is translate to the appropriate Exception.
What do you think?