4

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
  • Have you ever created and manipulated a database before?
    – JeffO
    May 5, 2016 at 18:55
  • 1
    Have you ever created a website? May 5, 2016 at 19:23
  • 1
    REST is not a standard, so strictly speaking it is possible to do whatever you want. People speak of "RESTful" APIs like they speak of good grammar. The rules are soft, and can be broken. May 5, 2016 at 19:38
  • 2
    Do you want to ask the server "give me the average workout time", or do you want to ask "calculate the average workout time"? The first would be a request that fits the REST ideas very well: You ask for some information and you don't care how or when it gets calculated, as long as the information is accurate. May 6, 2016 at 7:52

3 Answers 3

2

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.

2

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
  • Calculating an average of values is idempotent and side effect free, so meets the requirements for a GET. May 6, 2016 at 9:29
  • 1
    @PeteKirkham: Yes, that's true. My answer still holds, however. I never said he couldn't use a GET. May 6, 2016 at 14:11
0

You can use cross-origin resource sharing and send a script as a variable that a script engine evaluates and returns the evaluated function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.