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

Cedar: Rae Elwell & Khandice Schuhmann #8

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
4 changes: 4 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from flask import Flask



def create_app(test_config=None):
app = Flask(__name__)

from .routes import planets_bp
app.register_blueprint(planets_bp)

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

class Planet:
def __init__(self, id, name, description, matter):
self.id = id
self.name = name
self.description = description
self.matter = matter


planets = [Planet(1, "Mercury", "small and red", "solid"),
Planet(5, "Jupiter", "big and swirly", "gaseous"),
Planet(6, "Saturn", "rings and swirls", "gaseous")]


planets_bp = Blueprint("planets", __name__, url_prefix="/planets")

Choose a reason for hiding this comment

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

Minor note: to enhance readability, consider removing some blank lines and put each Planet instance on it's own line

Suggested change
self.matter = matter
planets = [Planet(1, "Mercury", "small and red", "solid"),
Planet(5, "Jupiter", "big and swirly", "gaseous"),
Planet(6, "Saturn", "rings and swirls", "gaseous")]
planets_bp = Blueprint("planets", __name__, url_prefix="/planets")
self.matter = matter
planets = [
Planet(1, "Mercury", "small and red", "solid"),
Planet(5, "Jupiter", "big and swirly", "gaseous"),
Planet(6, "Saturn", "rings and swirls", "gaseous")
]
planets_bp = Blueprint("planets", __name__, url_prefix="/planets")


@planets_bp.route("", methods=["GET"])
def get_all_planets():
planet_list = []
for planet in planets:
planet_list.append(vars(planet))

Choose a reason for hiding this comment

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

Great use of vars!

Choose a reason for hiding this comment

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

I just learned that vars will cause problems when we have a class that connects to our database. We can achieve the same effect by writing a to_json(self) instance method on the Planet class.

return jsonify(planet_list)

@planets_bp.route("/<planet_id>", methods=["GET"])
def get_one_planet(planet_id):
if not planet_id.isdigit():
return("Not a number!")

Choose a reason for hiding this comment

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

Nice work handling an invalid parameter. I learned from Auberon for some strange pythonic reasons, isdigit actually shouldn't be used here, and we prefer try/except

Suggested change
if not planet_id.isdigit():
return("Not a number!")
try:
planet_id = int(planet_id)
except:
return ("Not a number!", 400)

planet_id = int(planet_id)
for planet in planets:
if planet.id == planet_id:
return jsonify(vars(planet))
return ("Not Found!")

Choose a reason for hiding this comment

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

Nice work handling a planet that was not found. We can return a 404 response code with this:

Suggested change
return ("Not Found!")
return ("Not Found!", 404)