This is a simple calculator API built with Sanic.
This is a simple calculator API built using the Sanic framework in Python. It provides endpoints for performing basic arithmetic operations, calculating square roots and powers, calculating mean, harmonic mean and moda.
Technology used within the project:
- Sanic: Version ^19.6.0
-
Clone the repository:
git clone https://github.com/joao-coimbra/sanic-calculator.git
-
Install the dependencies:
cd calculator-api pip install -r requirements.txt -
Start the API server:
python server.py
sanic CLI
sanic server.app
The server will start running at
http://localhost:8000.
Takes list of numbers and returns the sum.
POST /api/add| Parameter | Type | Description |
|---|---|---|
nums |
array |
Required. list of numbers |
Request Body:
{
"nums": [2, 3, 7]
}Response Body:
{
"result": 12
}Takes list of numbers and returns the subtract.
POST /api/subtract| Parameter | Type | Description |
|---|---|---|
nums |
array |
Required. list of numbers |
Request Body:
{
"nums": [10, 7]
}Response Body:
{
"result": 3
}Takes list of numbers and returns the multiply.
POST /api/multiply| Parameter | Type | Description |
|---|---|---|
nums |
array |
Required. list of numbers |
Request Body:
{
"nums": [10, 7, 3]
}Response Body:
{
"result": 210
}Divides one number by another.
POST /api/divide| Parameter | Type | Description |
|---|---|---|
num |
float |
Required. number to be divided |
divisor |
int |
Required. divisor number |
Request Body:
{
"num": 7,
"divisor": 2
}Response Body:
{
"result": 3.5
}Calculates the square root of a number.
POST /api/sqrt| Parameter | Type | Description |
|---|---|---|
num |
int |
Required. number to get square root |
Request Body:
{
"num": 9
}Response Body:
{
"result": 3
}Raises one number to the power of another.
POST /api/power| Parameter | Type | Description |
|---|---|---|
base |
float |
Required. number to get its power |
exponent |
int |
Required. calculation exponent |
Request Body:
{
"base": 5,
"exponent": 3
}Response Body:
{
"result": 125
}Calculates the arithmetic mean of a list of numbers.
POST /api/mean| Parameter | Type | Description |
|---|---|---|
nums |
array |
Required. list of numbers |
Request Body:
{
"nums": [5, 3, 7, 2]
}Response Body:
{
"result": 4.25
}Calculates the harmonic mean of a list of numbers.
POST /api/hmean| Parameter | Type | Description |
|---|---|---|
nums |
array |
Required. list of numbers |
Request Body:
{
"nums": [5, 3, 7, 2]
}Response Body:
{
"result": 3.40080971659919
}Calculates the mode (most frequent values) of a list of numbers.
POST /api/moda| Parameter | Type | Description |
|---|---|---|
nums |
array |
Required. list of numbers |
Request Body:
{
"nums": [5, 3, 7, 2, 5, 2]
}Response Body:
{
"result": [5, 2]
}To use this API, you can send HTTP requests to the appropriate endpoints. For example, to add two numbers together, you can send a POST request to the /api/add endpoint with a JSON body like this:
{
"nums": [5, 2]
}And the API will respond with a JSON body like this:
{
"result": 7
}This project is licensed under the terms of the MIT license.