This is an API for hotels, guest houses or any other housing listing. Includes different roles for posting, updating and deleting housing. Implemented using python and FLASK Framework with auth0 for authentication and authorization, under Udacity Capstone project. Could be a starter boilerplate for a booking engine.
Follow instructions to install the latest version of python for your platform in the python docs
We recommend working within a virtual environment whenever using Python for projects. This keeps your dependencies for each project separate and organaized. Instructions for setting up a virual enviornment for your platform can be found in the python docs
Once you have your virtual environment setup and running, install dependencies by running:
pip install -r requirements.txt
This will install all of the required packages we selected within the requirements.txt
file.
-
Flask is a lightweight backend microservices framework. Flask is required to handle requests and responses.
-
SQLAlchemy is the Python SQL toolkit and ORM we'll use handle the lightweight sqlite database. You'll primarily work in app.py and can reference models.py.
-
Flask-CORS is the extension we'll use to handle cross origin requests from our frontend server.
-
Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic.
-
python-jose A JOSE implementation in Python. Used for working with JWT tokens.
-
Gunicorn is a Python WSGI HTTP Server for UNIX. Used for Heroku production.
With Postgres running, in terminal run:
createdb housing
flask db upgrade
First ensure you are working using your created virtual environment.
To run the server, execute:
python app.py
The application is hosted on heroku under the url: https://sky-fsnd-capstone.herokuapp.com/
For testing the RBAC controls, there is an postman_collection JSON file. You can import it in your postman and find the authorization tokens. Also valid tokens included into tets_app.py for testing purposes.
API requires authentication. There are 3 available roles:
- Viewer: Can view housing list, housing data, search housing.
- Manager Can do all view operations Can create new housing Can update housing information
- Administrator Can do all described above operations Can delete housing
- Fetches an array of localities
- Request Arguments: None
- Returns: An object with array of localities objects, total localities length.
{
"localities": [
{
"id": 1,
"name": "London"
},
{
"id": 2,
"name": "New York"
}
],
"success": true,
"total_localities": 2
}
- Fetches an array of housing based on locality
- Request Arguments: locality ID
- Returns: An object with array of housing objects, total housing length, requested locality id, locality name
{
"housing": [
{
"id": 2,
"locality": "London",
...
}
...
],
"locality_id": 1,
"locality_name": "London",
"success": true,
"total_housing": 1
}
- Fetches an array of housing
- Request Arguments: None
- Returns: An object with object of housing objects in format 'id': housing, total housing length
{
{
"housing": {
"2": {
"category": "Guest house",
"id": 2,
"name": "test hotel with contacts",
...
}
},
"success": true,
"total_housing": 1
}
}
- Creates new housing using provided housing object
- Request Arguments: housing object including: name, description, locality_id, cathegory_id, photos, room_types, contacts
- Returns: created housing object
{
"housing": {
"category": "Guest house",
"contacts": {
"adress": "street line 1",
"email": "[email protected]",
"facebook": "http://facebook.com/somehotel",
"instagram": "@somehotel",
"tel_number": "123456"
},
"description": "test description 2",
"id": 3,
"locality": "London",
"name": "test 2",
"photos": [
"https://placeimg.com/640/480/any",
"https://placeimg.com/640/480/any"
],
"room_types": [
{
"id": 7,
"name": "Single",
"price": 100
},
{
"id": 8,
"name": "Double",
"price": 200
}
]
},
"success": true
}
- Fetches housing object if ID exists
- Request Arguments: housing ID
- Returns: A housing object
{
"housing": {
"category": "Guest house",
"contacts": {
"adress": "street line 1",
"email": "[email protected]",
"facebook": "http://facebook.com/somehotel",
"instagram": "@somehotel",
"tel_number": "123456"
},
"description": "test description 2",
"id": 3,
"locality": "London",
"name": "test 2",
"photos": [
"https://placeimg.com/640/480/any",
"https://placeimg.com/640/480/any"
],
"room_types": [
{
"id": 7,
"name": "Single",
"price": 100
},
{
"id": 8,
"name": "Double",
"price": 200
}
]
},
"success": true
}
- Fetches an array of housing for whom the search term is a substring of the housing name or description
- Request Arguments: q - query term
- Returns: An object with object of housing objects in format 'id': housing, total housing length
{
"housing": {
"2": {
"category": "Guest house",
"name": "test hotel with contacts",
...
}
},
"success": true,
"total_housing": 1
}
- Update housing using provided housing object
- Request Arguments: housing object including: name, description, locality_id, cathegory_id, photos, room_types, contacts
- Returns: updated housing object
{
"housing": {
"description": "test description 2",
"id": 3,
"locality": "London",
"name": "test 2",
...
},
"success": true
}
- Deletes the housing of the given ID if it exists
- Request Arguments: housing ID
{
'success': true,
'housing_id': 3
}
To run the tests, run
createdb housing_test
export __ENV__=testing
python test_app.py