Let us say that I want to create a workout app and a website, the user enters his workout stats and the app display his stats with graphs percentage and such.
This is an example I don’t want to create a workout app, just to understand how platforms work.
Assuming that I want to have an Android, iOS app and a website. All with the same functionalities.
From what I read, REST APIs are used for back end.
This is an easy way of exchanging data between an app and the database.
But what about operations on the data ?
Let us say I want to compute the average workout time (or something much more complex). Do I have to compute it on the device so create the algorithm 3 times (Android, iOS and web) or run all computations on the server ?
I tried to read the documentation of a REST api (Eve for Python) but I only found data operations like get update and delete.
Does REST APIs provides only data operation, is it possible to call a function or a script with a REST API ?
Maybe I misunderstood everything so could someone explain me how back ends works for a multi device platform ?
4
Is this RESTful?
public PlanksTotalController : Controller
{
public double Get(int reps, TimeSpan duration)
{
return reps * duration;
}
}
Maybe, maybe not. I don’t really care if it meets the criteria for some ideal architecture. I care that I’ve created a service that is useful across platforms. I’ve deduplicated my code base by centralizing it someplace where it’s available everywhere I need it to be.
WebAPIs aren’t just for storing and retrieving raw data. Nothing says it can’t do a calculation.
By the way, I do think that so long as your end points are pure functions (given the same input, they’ll always return the same output), that they can indeed be RESTful. I believe this because a given resource path (~/PlankTotal?reps=2&duration=3.5
) will always return that specific resource (the number 7
).
Of course, there are other things to consider, like discoverability, when declaring an API to be RESTful, but that’s besides the point. Yes, you can (and should) centralize business logic in a service.
You forgot about POST.
You need to learn a bit more about REST (you actually need to learn a whole bunch of other stuff as well), but suffice it to say that if you want to do any other operation besides a GET, PUT, PATCH or DELETE, it’s probably going to be a POST.
Also, note that there are other ways to communicate with a server besides REST. Stand up a WebSocket, and you can pretty much do whatever you want.
In any case, a GET suffices for what you want to do, if you want to stay with REST.
2
You can use cross-origin resource sharing and send a script as a variable that a script engine evaluates and returns the evaluated function.