Simple Calculator API

This is a simple API which can also be used by the amended Simple Calculator.


You may want to use an API tool for testing the API.

We have provided both a Swagger UI and a redoc UI here to access the API but these may not provide the full flexibility you need to test an API.

The API has an Open API file

The API can be found at:

  • /apps/api/calculator/

There are two endpoints:

  • calculate
    • to calculate the result of a single operation (plus, minus, times, divide)
  • sequence
    • to calculate the result of a sequence of operations where the result of the previous sequence is fed through into the next operation.

Calculate

The calculate end point:

  • /apps/api/calculator/calculate

Accepts POST and GET requests.

These can be either application/json or application/x-www-form-urlencoded

Form Encoded Payload

If using the application/x-www-form-urlencoded content then the parameters are the same as those used by the Simple Calculator application:

  • number1
  • number2
  • function

JSON Payload

If using the JSON format then use the following format:

{
  "operation": "plus",
  "left": "1",
  "right": "2"
}

Operands input format

There is also an additional input format, instead of supplying a left and right value, supply a list of operands:

{
  "operation": "plus",
  "operands": [ "1", "2", "3"]
}

Query Format

For a GET request, the values need to be encoded into the request as query params:

e.g. .../calculate?operation=plus&left=1&right=2

Params:

  • operation e.g. plus, minus, etc.
  • left e.g. 1
  • right e.g. 2
  • operands e.g. [1, 2]

JSON response

Success

The above would add 1 and 2 and respond with:

{
  "type": "VALUE",
  "value": "3"
}

Error

Any errors result in an error response:

{
  "type": "INPUT_ERROR",
  "value": "Unrecognised Operation: add valid values are (plus, times, minus, divide)"
}

Sequence

The sequence end point:

  • /apps/api/calculator/sequence

Accepts POST requests.

These can be application/json

To calculate (4 - 1) * 3:

{"operations": [
    {
        "operation": "plus",
        "right": 4
    },
    {
        "operation": "minus",
        "right": "1"
    },
    {
        "operation": "times",
        "right": 3
  }
]}

would result in:

{
  "type": "VALUE",
  "value": "9"
}

Server Side Calculator Using API

A simple calculator UI submits form to server to calculate the result.

Simple Calculator API - Swagger UI

A simple calculator API

Simple Calculator API - Redoc UI

A simple calculator API with the UI rendered using Redoc

Calculator API - Instructions