Skip to content

mdsrosa/routes_api_python

Repository files navigation

Routes API

Build Status Coverage Status

API to calculate the shortest path and the cost to a given route (origin point and destination point), based on fuel price and vehicle's autonomy.

This application is live on Heroku: https://routes-api-python-prod.herokuapp.com

Installation

Considering you already have a Python development environment setup.
$ git clone https://github.com/mdsrosa/routes_api_python.git
$ cd routes_api_python
$ mkvirtualenv routes-api-dev
$ pip install -r requirements.txt

Running Locally

$ python manage.py db upgrade
$ python run.py

Endpoints

GET /routes

This endpoint lists all routes in the database.

cURL Example

$ curl -i https://routes-api-python-prod.herokuapp.com/routes

Response Example

{
    "routes": [
        {
            "destination_point": "B",
            "distance": 10,
            "origin_point": "A",
            "uri": "/routes/1"
        },
        {
            "destination_point": "C",
            "distance": 20,
            "origin_point": "A",
            "uri": "/routes/2"
        },
        {
            "destination_point": "D",
            "distance": 15,
            "origin_point": "B",
            "uri": "/routes/3"
        },
        {
            "destination_point": "D",
            "distance": 30,
            "origin_point": "C",
            "uri": "/routes/4"
        },
        {
            "destination_point": "E",
            "distance": 50,
            "origin_point": "B",
            "uri": "/routes/5"
        },
        {
            "destination_point": "E",
            "distance": 30,
            "origin_point": "D",
            "uri": "/routes/6"
        }
    ]
}

GET /routes/pk

This endpoint returns a route.

cURL Example
$ curl -i https://routes-api-python-prod.herokuapp.com/routes/1
Response Example
{
    "route": {
        "destination_point": "B",
        "distance": 10,
        "origin_point": "A",
        "uri": "/routes/1"
    }
}

POST /routes

This endpoint creates a new route.

Fields

Name Type Description Example
origin_point string The point of origin "A"
destination_point string The point of destination "D"
distance integer The vehicle's autonomy 10
cURL Example
$ curl -i -H "Content-Type: application/json" -X POST https://routes-api-python-prod.herokuapp.com/routes '{"origin_point":"A","destination_point":"D","distance":10}'
Response Example
{
    "route": {
        "destination_point": "D",
        "distance": 10,
        "origin_point": "A",
        "uri": "/routes/1"
    }
}

PUT /routes/pk

This endpoint updates a route.

Fields

Name Type Description Example
origin_point string The point of origin "A"
destination_point string The point of destination "D"
distance integer The vehicle's autonomy 10
cURL Example
$ curl -i -X PUT -H "Content-Type: application/json" https://routes-api-python-prod.herokuapp.com/routes/1 -d '{"destination_point": "B"}'
Response Example
{
    "route": {
        "destination_point": "B",
        "distance": 10,
        "origin_point": "A",
        "uri": "/routes/1"
    }
}

DELETE /routes/pk

This endpoint deletes a route.

cURL Example
$ curl -X DELETE https://routes-api-python-prod.herokuapp.com/routes/1
Response Example
{
    "result": true
}

POST /routes/calculate-cost

This endpoint calculates the cost based on distance, autonomy and fuel_price.

Fields

Name Type Description Example
origin_point string The point of origin "A"
destination_point string The point of destination "D"
autonomy integer The vehicle's autonomy 10
fuel_price float The fuel price 2.5
cURL Example
$ curl -i -H "Content-Type: application/json" -X POST https://routes-api-python-prod.herokuapp.com/routes/calculate-cost -d '{"origin_point":"A","destination_point":"D","autonomy":10,"fuel_price":2.5}'
Response Example
{
    "cost": 6.25,
    "path": "A B D"
}

Testing

python tests.py

Thank you! :-)

About

Python version of http://github.com/mdsrosa/routes_api. Live at https://routes-api-python-prod.herokuapp.com

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors