diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..1123a559e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,4 +4,7 @@ def create_app(test_config=None): app = Flask(__name__) + from .routes import bp + app.register_blueprint(bp) + return app diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..96afcfda3 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,69 @@ -from flask import Blueprint +from flask import Blueprint, jsonify, abort, make_response +class Planet: + def __init__(self, id, name, description, has_moon=None): + self.id = id + self.name = name + self.description = description + self.has_moon = has_moon + + def to_json(self): + return dict( + id = self.id, + name = self.name, + description = self.description, + has_moon = self.has_moon + ) + + +planets = [ + Planet(1, "Mercury", "terrestrial", False), + Planet(2, "Jupiter", "gaseous", True), + Planet(3, "Earth", "terrestrial", True) +] + +#instantiate blueprint object +bp = Blueprint("planets_bp",__name__, url_prefix="/planets") + +#design endpoint with blueprint tag +"""..to get all existing planets, so that I can see a list of planets, +with their id, name, description, and other data of the planet.""" + +# @bp.route("", methods=["GET"]) +# def list_planets(): +# list_of_planets = [dict( +# id = planet.id, +# name = planet.name, +# description = planet.description, +# has_moon = planet.has_moon, +# ) for planet in planets] + +# return jsonify(list_of_planets) + +def validate_planet(planet_id): + try: + planet_id = int(planet_id) + except: + abort(make_response({"message":f"planet {planet_id} invalid"}, 400)) + + for planet in planets: + if planet.id == planet_id: + return planet + + abort(make_response({"message":f"planet {planet_id} not found"}, 404)) + +@bp.route("", methods=["GET"]) +def handle_planets(): + planet_response = [planet.to_json() for planet in planets] + + return jsonify(planet_response) + +@bp.route("/", methods=["GET"]) +def handle_planet(planet_id): + planet = validate_planet(planet_id) + return jsonify(planet.to_json()) + + +# FLASK_ENV=developer flask run + + diff --git a/requirements.txt b/requirements.txt index a506b4d12..3697cdc0f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,27 +1,8 @@ -alembic==1.5.4 -autopep8==1.5.5 -blinker==1.4 -certifi==2020.12.5 -chardet==4.0.0 -click==7.1.2 -Flask==1.1.2 -Flask-Migrate==2.6.0 -Flask-SQLAlchemy==2.4.4 -idna==2.10 -itsdangerous==1.1.0 -Jinja2==2.11.3 -Mako==1.1.4 -MarkupSafe==1.1.1 -psycopg2-binary==2.8.6 -pycodestyle==2.6.0 -pytest==6.2.3 -pytest-cov==2.12.1 -python-dateutil==2.8.1 -python-dotenv==0.15.0 -python-editor==1.0.4 -requests==2.25.1 -six==1.15.0 -SQLAlchemy==1.3.23 -toml==0.10.2 -urllib3==1.26.4 -Werkzeug==1.0.1 +attrs==21.4.0 +iniconfig==1.1.1 +packaging==21.3 +pluggy==1.0.0 +py==1.11.0 +pyparsing==3.0.8 +pytest==7.1.2 +tomli==2.0.1