szbrooks2017 / AirBnB_clone_v3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AirBnB Clone - Restful API

The console is the first segment of the AirBnB project at Holberton School that will collectively cover fundamental concepts of higher level programming. The goal of AirBnB project is to eventually deploy our server a simple copy of the AirBnB Website(HBnB). A command interpreter is created in this segment to manage objects for the AirBnB(HBnB) website.

Functionalities of this command interpreter:

  • Create a new object (ex: a new User or a new Place)
  • Retrieve an object from a file, a database etc...
  • Do operations on objects (count, compute stats, etc...)
  • Update attributes of an object
  • Destroy an object

Table of Content

Environment

This project is interpreted/tested on Ubuntu 14.04 LTS using python3 (version 3.4.3)

Installation

  • Clone this repository: git clone "https://github.com/szbrooks2017/AirBnB_clone_v3"
  • Access AirBnb directory: cd AirBnB_clone_v3
  • Run hbnb(interactively): ./console and enter command
  • Run hbnb(non-interactively): echo "<command>" | ./console.py

File Descriptions

console.py - the console contains the entry point of the command interpreter. List of commands this console current supports:

  • EOF - exits console
  • quit - exits console
  • <emptyline> - overwrites default emptyline method and does nothing
  • create - Creates a new instance ofBaseModel, saves it (to the JSON file) and prints the id
  • destroy - Deletes an instance based on the class name and id (save the change into the JSON file).
  • show - Prints the string representation of an instance based on the class name and id.
  • all - Prints all string representation of all instances based or not on the class name.
  • update - Updates an instance based on the class name and id by adding or updating attribute (save the change into the JSON file).

app.py - creates the Flask instance which makes the data accessible through browser commands

  • def teardown(error) - closes the storage instance between requests
  • def error_handler(error) - returns a 404 page on request error

models/ directory contains classes used for this project:

base_model.py - The BaseModel class from which future classes will be derived

  • def __init__(self, *args, **kwargs) - Initialization of the base model
  • def __str__(self) - String representation of the BaseModel class
  • def save(self) - Updates the attribute updated_at with the current datetime
  • def to_dict(self) - returns a dictionary containing all keys/values of the instance

Classes inherited from Base Model:

/models/engine directory contains File Storage class that handles JSON serialization and deserialization :

file_storage.py - serializes instances to a JSON file & deserializes back to instances

  • def all(self) - returns the dictionary __objects
  • def new(self, obj) - sets in __objects the obj with key .id
  • def save(self) - serializes __objects to the JSON file (path: __file_path)
  • def reload(self) - deserializes the JSON file to __objects

/api/v1/views directory contains all routes and their functions for the API functionality:

init.py - defines a blueprint for the Flask object that prefixes all routes with '/api/v1'

index.py - Contains the /status and /stats routes for overall info on the database

  • GET /status - returns 'status': 'OK' if working correctly
  • GET /stats - returns a dictionary of object counts by type

amenities.py - Contains all routes and functions for accessing amenities

  • GET /amenities - retuns all amenity objects as a list of dictionaries
  • GET /amenities/<amenity_id> - returns a dictionary of the object with given id
  • DELETE /amenities/<amenity_id> - removes the object with matching id
  • POST /amenities - creates a new amenity from given JSON dictionary
  • PUT /amenities/<amenity_id> - updates the object of given id with the given JSON dictionary values

cities.py - Contains all routes and functions for accessing cities

  • GET /states/<state_id>/cities - retuns all city objects linked to state with given id as a list of dictionaries
  • GET /cities/<city_id> - returns a dictionary of the object with given id
  • DELETE /cities/<city_id> - removes the object with matching id
  • POST /states/<state_id>/cities - creates a new city from given JSON dictionary linked to state with given id
  • PUT /cities/<city_id> - updates the object of given id with the given JSON dictionary values

places.py - Contains all routes and functions for accessing places

  • GET /cities/<city_id>/places - retuns all place objects linked to city with given id as a list of dictionaries
  • GET /places/<place_id> - returns a dictionary of the object with given id
  • DELETE /places/<place_id> - removes the object with matching id
  • POST /cities/<city_id>/places - creates a new place from given JSON dictionary linked to city with given id
  • PUT /places/<place_id> - updates the object of given id with the given JSON dictionary values

places_reviews.py - Contains all routes and functions for accessing reviews

  • GET /places/<place_id>/reviews - retuns all review objects linked to place with given id as a list of dictionaries
  • GET /reviews/<review_id> - returns a dictionary of the object with given id
  • DELETE /reviews/<review_id> - removes the object with matching id
  • POST /places/<place_id>/reviews - creates a new review from given JSON dictionary linked to place with given id
  • PUT /reviews/<review_id> - updates the object of given id with the given JSON dictionary values

states.py - Contains all routes and functions for accessing states

  • GET /states - retuns all state objects as a list of dictionaries
  • GET /states/<state_id> - returns a dictionary of the object with given id
  • DELETE /states/<state_id> - removes the object with matching id
  • POST /states - creates a new amenity from given JSON dictionary
  • PUT /states/<state_id> - updates the object of given id with the given JSON dictionary values

users.py - Contains all routes and functions for accessing users

  • GET /users - retuns all user objects as a list of dictionaries
  • GET /users/<user_id> - returns a dictionary of the object with given id
  • DELETE /users/<user_id> - removes the object with matching id
  • POST /users - creates a new user from given JSON dictionary
  • PUT /users/<user_id> - updates the object of given id with the given JSON dictionary values

/tests directory contains all unit test cases for this project:

/test_models/test_base_model.py - Contains the TestBaseModel and TestBaseModelDocs classes TestBaseModelDocs class:

  • def setUpClass(cls)- Set up for the doc tests
  • def test_pep8_conformance_base_model(self) - Test that models/base_model.py conforms to PEP8
  • def test_pep8_conformance_test_base_model(self) - Test that tests/test_models/test_base_model.py conforms to PEP8
  • def test_bm_module_docstring(self) - Test for the base_model.py module docstring
  • def test_bm_class_docstring(self) - Test for the BaseModel class docstring
  • def test_bm_func_docstrings(self) - Test for the presence of docstrings in BaseModel methods

TestBaseModel class:

  • def test_is_base_model(self) - Test that the instatiation of a BaseModel works
  • def test_created_at_instantiation(self) - Test created_at is a pub. instance attribute of type datetime
  • def test_updated_at_instantiation(self) - Test updated_at is a pub. instance attribute of type datetime
  • def test_diff_datetime_objs(self) - Test that two BaseModel instances have different datetime objects

/test_models/test_amenity.py - Contains the TestAmenityDocs class:

  • def setUpClass(cls) - Set up for the doc tests
  • def test_pep8_conformance_amenity(self) - Test that models/amenity.py conforms to PEP8
  • def test_pep8_conformance_test_amenity(self) - Test that tests/test_models/test_amenity.py conforms to PEP8
  • def test_amenity_module_docstring(self) - Test for the amenity.py module docstring
  • def test_amenity_class_docstring(self) - Test for the Amenity class docstring

/test_models/test_city.py - Contains the TestCityDocs class:

  • def setUpClass(cls) - Set up for the doc tests
  • def test_pep8_conformance_city(self) - Test that models/city.py conforms to PEP8
  • def test_pep8_conformance_test_city(self) - Test that tests/test_models/test_city.py conforms to PEP8
  • def test_city_module_docstring(self) - Test for the city.py module docstring
  • def test_city_class_docstring(self) - Test for the City class docstring

/test_models/test_file_storage.py - Contains the TestFileStorageDocs class:

  • def setUpClass(cls) - Set up for the doc tests
  • def test_pep8_conformance_file_storage(self) - Test that models/file_storage.py conforms to PEP8
  • def test_pep8_conformance_test_file_storage(self) - Test that tests/test_models/test_file_storage.py conforms to PEP8
  • def test_file_storage_module_docstring(self) - Test for the file_storage.py module docstring
  • def test_file_storage_class_docstring(self) - Test for the FileStorage class docstring

/test_models/test_place.py - Contains the TestPlaceDoc class:

  • def setUpClass(cls) - Set up for the doc tests
  • def test_pep8_conformance_place(self) - Test that models/place.py conforms to PEP8.
  • def test_pep8_conformance_test_place(self) - Test that tests/test_models/test_place.py conforms to PEP8.
  • def test_place_module_docstring(self) - Test for the place.py module docstring
  • def test_place_class_docstring(self) - Test for the Place class docstring

/test_models/test_review.py - Contains the TestReviewDocs class:

  • def setUpClass(cls) - Set up for the doc tests
  • def test_pep8_conformance_review(self) - Test that models/review.py conforms to PEP8
  • def test_pep8_conformance_test_review(self) - Test that tests/test_models/test_review.py conforms to PEP8
  • def test_review_module_docstring(self) - Test for the review.py module docstring
  • def test_review_class_docstring(self) - Test for the Review class docstring

/test_models/state.py - Contains the TestStateDocs class:

  • def setUpClass(cls) - Set up for the doc tests
  • def test_pep8_conformance_state(self) - Test that models/state.py conforms to PEP8
  • def test_pep8_conformance_test_state(self) - Test that tests/test_models/test_state.py conforms to PEP8
  • def test_state_module_docstring(self) - Test for the state.py module docstring
  • def test_state_class_docstring(self) - Test for the State class docstring

/test_models/user.py - Contains the TestUserDocs class:

  • def setUpClass(cls) - Set up for the doc tests
  • def test_pep8_conformance_user(self) - Test that models/user.py conforms to PEP8
  • def test_pep8_conformance_test_user(self) - Test that tests/test_models/test_user.py conforms to PEP8
  • def test_user_module_docstring(self) - Test for the user.py module docstring
  • def test_user_class_docstring(self) - Test for the User class docstring

Examples of use

vagrantAirBnB_clone$./console.py
(hbnb) help

Documented commands (type help <topic>):
========================================
EOF  all  create  destroy  help  quit  show  update

(hbnb) all MyModel
** class doesn't exist **
(hbnb) create BaseModel
7da56403-cc45-4f1c-ad32-bfafeb2bb050
(hbnb) all BaseModel
[[BaseModel] (7da56403-cc45-4f1c-ad32-bfafeb2bb050) {'updated_at': datetime.datetime(2017, 9, 28, 9, 50, 46, 772167), 'id': '7da56403-cc45-4f1c-ad32-bfafeb2bb050', 'created_at': datetime.datetime(2017, 9, 28, 9, 50, 46, 772123)}]
(hbnb) show BaseModel 7da56403-cc45-4f1c-ad32-bfafeb2bb050
[BaseModel] (7da56403-cc45-4f1c-ad32-bfafeb2bb050) {'updated_at': datetime.datetime(2017, 9, 28, 9, 50, 46, 772167), 'id': '7da56403-cc45-4f1c-ad32-bfafeb2bb050', 'created_at': datetime.datetime(2017, 9, 28, 9, 50, 46, 772123)}
(hbnb) destroy BaseModel 7da56403-cc45-4f1c-ad32-bfafeb2bb050
(hbnb) show BaseModel 7da56403-cc45-4f1c-ad32-bfafeb2bb050
** no instance found **
(hbnb) quit

API Example

First Terminal

vagrantAirBnB_clone_v3$ HBNB_MYSQL_USER=hbnb_dev HBNB_MYSQL_PWD=hbnb_dev_pwd HBNB_MYSQL_HOST=localhost HBNB_MYSQL_DB=hbnb_dev_db HBNB_TYPE_STORAGE=db HBNB_API_HOST=0.0.0.0 HBNB_API_PORT=5000 python3 console.py
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
...

Second Terminal

vagrantAirBnB_clone_v3$ curl -X GET http://0.0.0.0:5000/api/v1/status
{
  "status": "OK"
}
vagrantAirBnB_clone_v3$ curl -X GET http://0.0.0.0:5000/api/v1/stats
{
  "amenities": 0, 
  "cities": 0, 
  "places": 0, 
  "reviews": 0, 
  "states": 0, 
  "users": 0
}
vagrantAirBnB_clone_v3$ curl -X POST http://0.0.0.0:5000/api/v1/states/ -H "Content-Type: application/json" -d '{"name": "California"}' -vvv
*   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 5000 (#0)
> POST /api/v1/states/ HTTP/1.1
> Host: 0.0.0.0:5000
> User-Agent: curl/7.51.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 22
> 
* upload completely sent off: 22 out of 22 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 201 CREATED
< Content-Type: application/json
< Content-Length: 195
< Server: Werkzeug/0.12.1 Python/3.4.3
< Date: Sat, 15 Apr 2017 01:30:27 GMT
< 
{
  "__class__": "State", 
  "created_at": "2017-04-15T01:30:27.557877", 
  "id": "feadaa73-9e4b-4514-905b-8253f36b46f6", 
  "name": "California", 
  "updated_at": "2017-04-15T01:30:27.558081"
}
* Curl_http_done: called premature == 0
* Closing connection 0
vagrantAirBnB_clone_v3$ curl -X PUT http://0.0.0.0:5000/api/v1/states/feadaa73-9e4b-4514-905b-8253f36b46f6 -H "Content-Type: application/json" -d '{"name": "California is so cool"}'
{
  "__class__": "State", 
  "created_at": "2017-04-15T01:30:28", 
  "id": "feadaa73-9e4b-4514-905b-8253f36b46f6", 
  "name": "California is so cool", 
  "updated_at": "2017-04-15T01:51:08.044996"
}
vagrantAirBnB_clone_v3$ curl -X GET http://0.0.0.0:5000/api/v1/states/feadaa73-9e4b-4514-905b-8253f36b46f6
{
  "__class__": "State", 
  "created_at": "2017-04-15T01:30:28", 
  "id": "feadaa73-9e4b-4514-905b-8253f36b46f6", 
  "name": "California is so cool", 
  "updated_at": "2017-04-15T01:51:08"
}
vagrantAirBnB_clone_v3$ curl -X DELETE http://0.0.0.0:5000/api/v1/states/feadaa73-9e4b-4514-905b-8253f36b46f6
{}
vagrantAirBnB_clone_v3$ curl -X GET http://0.0.0.0:5000/api/v1/states/feadaa73-9e4b-4514-905b-8253f36b46f6
{
  "error": "Not found"
}
vagrantAirBnB_clone_v3$ curl -X GET http://0.0.0.0:5000/api/v1/states
[]

Bugs

No known bugs at this time.

Authors

Alexa Orrico - Github / Twitter
Jennifer Huang - Github / Twitter Stratton Brooks - Github Jacob Chavera - Github

Second part of Airbnb: Joann Vuong

License

Public Domain. No copy write protection.

About


Languages

Language:Python 80.0%Language:HTML 12.1%Language:CSS 7.5%Language:Shell 0.5%