An HTTP calculator web service, just for fun.

Published on Oct 04, 2012

Last week, I was listening to the Net Rocks episode 807 with Bill Wagner in witch Carl Franklin joked about exposing the back end of a calculator as a REST web service. I was walking to the subway when he made the joke and I couldn’t stop laughing.

While at the platform, waiting for the train, I started to think about how to implement it.

I’m not suggesting this is a proper REST implementation, just an intellectual exercise during a subway ride. Let’s call it an HTTP web service if your wish.

A calculator resource

The first thing is to figure out what resources to expose.
We could certainly expose a calculator resource and in that case we could map the verb as follow:

METHOD URL
GET calculators List of available calculators (smell, smell)
GET calculator/:id Get the calculator by :id
POST calculator/:id Creates an existing calculator
PUT calculator/:id Modifies a calculator.
DELETE calculator/:id Remove the calculator
The payload for the calculator could be a series of operations that could look something like this.

  [
    ['+', 6, 7, 22],
    ['-',['*', 2, 5]],
    ['*' ['/', 10, 2]]
  ]

And translates to: (6 + 7 + 22) – (2 * 5) * (10 / 2)

Calculator, calculations.

I don’t think that having a series of calculators makes any sense.
It looks like what we need is a series of calculations instead:

METHOD URL
GET calculations
GET calculation/:id
POST calculation/:id
PUT calculation/:id
DELETE calculation/:id
Much better.

Operations as resources.

So, it works but you need to do a PUT of a complete new document to add new operations to the calculation.

Unless you implement PATCH and use it to modify it, what is perfectly fine but I think we can do better.

While discussing the use of the calculation and the payload we keep talking about operations.

What if we express operations as resources?

Let’s take the basic arithmetic operations: addition, subtraction, multiplication and division.

METHOD URL
GET calculation/:id/addition/:id
POST calculation/:id/addition/:id
PUT calculation/:id/addition/:id
DELETE calculation/:id/addition/:id
It may look ok but I think we don’t need to “name” each operation since we already have a notation in the operation itself.
We don’t want an addition or a subtraction resource, just an operation resource.

Like this:

METHOD URL
GET calculation/:id/operation/:id
POST calculation/:id/operation/:id
PUT calculation/:id/operation/:id
DELETE calculation/:id/operation/:id
This allow us to modify any of the operations in the calculation. Ex: we can change an addition for a subtraction with a simple PUT.

A simple calculation

I think that the operations as resources approach provides another advantage.

The calculation payload is now simpler, something like this.


  {
    id: "xxxx",
    total: 10.5,
    operations: "http://calculator.domain.com/calculation/xxxx/operations"
  }

Where operations is a link to the resources.

A fun exercise.

Well, this was a fun exercise for a short subway ride.