API Details are mentioned in following section:
Technologies Used in Application:
- Docker
- Programming Language: Python using Flask Framework
- Backend Database: Postgres
- Redis for Authentication storage
- SQL Toolkit or ORM: SQLAlchemy
- Marshmallow for serialization
- Flake8 for maintaining code conventions
- Supervisor for process Management
- nose2 for test discovery
- Prerequisites: You’ll need at least docker 1.10.
If you don’t already have it installed, follow the instructions for your OS:
a) On Mac OS X/Windows, you’ll need Docker Toolbox
b) On Linux, you’ll need docker-engine
-
Create the Machine:
a) docker-machine create --driver virtualbox default
b) eval $(docker-machine env default) # replace default with name of your docker machine -
Build the Stack
a) docker-compose -f dev.yml build -
Create Database Tables
a) docker-compose -f dev.yml run flask python manage.py syncdb -
Copy CSV Data to postgres database:
a) Run script scripts/data_sync.sh with parameters defined in script
b) Usage: ./scripts/data_sync.sh ../flixbus_data/homework_segments.csv ../flixbus_data/homework_route_segments.csv ../flixbus_data/homework_rides.csv ../flixbus_data/homework_tickets.csv -
Run the server
a) docker-compose -f dev.yml up -
Check the ip where the server is running:
a) docker-machine ip default
- docker-compose -f dev.yml run flask nose2 # runs all test cases
- docker-compose -f dev.yml run flask nose2 test_promise # runs all test cases in test_segment_list_api.py
Application API's is protected using the authentication decorator.
'AUTH_KEY' and 'ACCESS_TOKEN' are stored and checked in redis for authenticating the user.
APP_CLIENT
is used to identify a system. Send this across in headers to authorize internal calls.
Segment API is used to perform CRUD operations on segment resource:
- Fetch list of segments: Returns list of segments in the system based on the query parameters.
API Endpoint: GET /segment?{query_parameters}
Allowed query_parameters:
get_pax Bool
get_revenue Bool
from_stop int
destination_stop int
distance int
###Request:
curl -X GET -H "Content-Type: application/json" -H "AUTH_KEY: AUTH_KEY1" -H "ACCESS_TOKEN: ACCESS_TOKEN_1" -H "APP_CLIENT: flixbus_data_app" http://192.168.99.100:5000/v1/segment?get_pax=true&get_revenue=false&from_stop=1
###Response:
[{
"id": 1
"from_stop": 1,
"destination_stop": 10,
"distance": 5.3
"pax_count": 533
"revenue": 233
"currency": "EURO"
},
{
"id": 2
"from_stop": 1,
"destination_stop": 2,
"distance": 3.3
"pax_count": 53
"revenue": 234
"currency": "EURO"
}
]
- POST a new segment
API Endpoint: POST /segment
###Request:
curl -X POST -H "Content-Type: application/json" -H "AUTH_KEY: AUTH_KEY1" -H "ACCESS_TOKEN: ACCESS_TOKEN_1" -H "APP_CLIENT: flixbus_data_app" -d '{"from_stop":-11,"destination_stop":-2, "distance": 5.3}' http://192.168.99.100:5000/v1/segment
- Get a specific Segment
API Endpoint: GET /segment/<segment_id>?{query_parameters}
Allowed query_parameters:
get_pax Bool
get_revenue Bool
from_stop int
destination_stop int
distance int
###Request:
curl -X GET -H "Content-Type: application/json" -H "AUTH_KEY: AUTH_KEY1" -H "ACCESS_TOKEN: ACCESS_TOKEN_1" -H "APP_CLIENT: flixbus_data_app" http://192.168.99.100:5000/v1/segment/1?get_pax=true&get_revenue=false&from_stop=1
###Response:
{
"id": 1
"from_stop": 1,
"destination_stop": 10,
"distance": 5.3
"pax_count": 533
"revenue": 233
"currency": "EURO"
}
- UPDATE Existing segment
API Endpoint: PUT /segment/<segment_id>
###Request:
curl -X PUT -H "Content-Type: application/json" -H "AUTH_KEY: AUTH_KEY1" -H "ACCESS_TOKEN: ACCESS_TOKEN_1" -H "APP_CLIENT: flixbus_data_app" -d '{"from_stop":-11,"destination_stop":-2, "distance": 5.3}' http://192.168.99.100:5000/v1/segment/1
- DELETE existing segment
API Endpoint: DELETE /segment/<segment_id>
###Request:
curl -X DELETE -H "Content-Type: application/json" -H "AUTH_KEY: AUTH_KEY1" -H "ACCESS_TOKEN: ACCESS_TOKEN_1" -H "APP_CLIENT: flixbus_data_app" http://192.168.99.100:5000/v1/segment/1?get_pax=true&get_revenue=false&from_stop=1
Route API is used to perform CRUD operations on route resource:
- Fetch list of routes: Returns list of routes in the system based on the query parameters.
API Endpoint: GET /route?{query_parameters}
Allowed query_parameters:
start int : offset
limit int : no. of results to be returned from offset
segment_id int: segment_id used for filtering
###Request:
curl -X GET -H "Content-Type: application/json" -H "AUTH_KEY: AUTH_KEY1" -H "ACCESS_TOKEN: ACCESS_TOKEN_1" -H "APP_CLIENT: flixbus_data_app" http://192.168.99.100:5000/v1/route?start=0
###Response:
[{
"id": 1
"segments": [
{
"segment_id": 1,
"segment_sequence": 1
},
{
"segment_id": 2,
"segment_sequence": 2
}
]
},
{
"id": 2
"segments": [
{
"segment_id": 3,
"segment_sequence": 1
},
{
"segment_id": 4,
"segment_sequence": 2
}
]
}
]
-
POST a new route
API Endpoint: POST /route -
Get a specific Route
API Endpoint: GET /route/<route_id>?{query_parameters}
Allowed query_parameters:
Allowed query_parameters:
start int : offset
limit int : no. of results to be returned from offset
segment_id int: segment_id used for filtering
###Request:
curl -X GET -H "Content-Type: application/json" -H "AUTH_KEY: AUTH_KEY1" -H "ACCESS_TOKEN: ACCESS_TOKEN_1" -H "APP_CLIENT: flixbus_data_app" http://192.168.99.100:5000/v1/route/1
###Response:
{
"id": 1
"segments": [
{
"segment_id": 1,
"segment_sequence": 1
},
{
"segment_id": 2,
"segment_sequence": 2
}
]
}
-
UPDATE Existing segment
API Endpoint: PUT /segment/<segment_id> -
DELETE existing segment
API Endpoint: DELETE /segment/<segment_id>
###Request:
curl -X DELETE -H "Content-Type: application/json" -H "AUTH_KEY: AUTH_KEY1" -H "ACCESS_TOKEN: ACCESS_TOKEN_1" -H "APP_CLIENT: flixbus_data_app" http://192.168.99.100:5000/v1/segment/1?get_pax=true&get_revenue=false&from_stop=1
Ride API is used to perform CRUD operations on ride resource:
- Fetch list of Ride: Returns list of rides in the system based on the query parameters.
API Endpoint: GET /route/<route_id>/ride?{query_parameters}
Allowed query_parameters:
from_stop int
destination_stop int
###Request:
curl -X GET -H "Content-Type: application/json" -H "AUTH_KEY: AUTH_KEY1" -H "ACCESS_TOKEN: ACCESS_TOKEN_1" -H "APP_CLIENT: flixbus_data_app" http://192.168.99.100:5000/v1/route/1/ride
-
POST a new ride
API Endpoint: POST /route/<route_id>/ride -
Get a specific Ride
API Endpoint: GET /route/<route_id>/ride/<ride_id>?{query_parameters} -
UPDATE Existing ride
API Endpoint: PUT /route/<route_id>/ride/<ride_id>
Ticket API is used to perform CRUD operations on ticket resource:
-
Fetch list of tickets: Returns list of tickets in the system based on the query parameters.
API Endpoint: GET /ticket?{query_parameters} -
POST a new ticket
API Endpoint: POST /ticket -
Get a specific ticket
API Endpoint: GET /ticket/<ticket_id>?{query_parameters} -
UPDATE Existing ticket
API Endpoint: PUT /ticket/<ticket_id> -
DELETE existing ticket
API Endpoint: DELETE /ticket/<ticket_id>