Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sea Turtles - Esther A. & Danielle W. #11

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
def create_app(test_config=None):
app = Flask(__name__)

from .routes import planets_bp
app.register_blueprint(planets_bp)

return app
49 changes: 48 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
from flask import Blueprint
from flask import Blueprint, jsonify, abort, make_response

class Planets:
kelsey-steven-ada marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, id, name, description):
self.id = id
self.name = name
self.description = description


# create our instances
planets = [
Planets(1, "Mercury", "Small hot planet"),
Planets(2, "Venus", "A gaseous planet"),
Planets(3, "Earth", "Has lots of life")
]

# create the blueprint
planets_bp = Blueprint("planets", __name__, url_prefix="/planets")

# do the decorator
@planets_bp.route("", methods=["GET"])
def get_all_planets():
return jsonify([
to_dict(planet)
for planet in planets
])
kelsey-steven-ada marked this conversation as resolved.
Show resolved Hide resolved

@planets_bp.route("/<planet_id>", methods = ["GET"])
def get_one_planet(planet_id):

planet = validate_planet(planet_id)

return jsonify(to_dict(planet))

def validate_planet(planet_id):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider renaming this function. To me, validate_planet sounds like it should return a true/false based on if the planet passed as a parameter is valid. What name might better describe what the function is doing?

try:
planet_id = int(planet_id)
except ValueError:
abort(make_response({"message": f"invalid id {planet_id}"}, 400))

for planet in planets:
if planet.id == planet_id:
return planet
abort(make_response({"message": f"planet id {planet_id} not found"}, 404))
kelsey-steven-ada marked this conversation as resolved.
Show resolved Hide resolved

def to_dict(planet):
kelsey-steven-ada marked this conversation as resolved.
Show resolved Hide resolved
return {"id": planet.id,
"name": planet.name,
"description": planet.description}