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

SHARKS - Jande R. and Lindsey S. #7

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@

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


from .routes import planets_bp
app.register_blueprint(planets_bp)

Choose a reason for hiding this comment

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

👍 Looks good! We can also move this file into a routes folder which would change the path of this import. Creating a routes folder makes sense if we have different routes for specific objects for example planet_routes, moon_routes, galaxy_routes, etc.

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


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

def to_json(self):
return {
"id" : self.id,
"name" : self.name,
"description" : self.description,
"circumference" : self.circumference,
"length_of_year" : self.length_of_year
}

Choose a reason for hiding this comment

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

👍 LGTM (looks good to me)


planets = [
Planet(1, "Mercury", "made mostly of rocks", 9522, 88),
Planet(2, "Venus", "most like Earth", 23617, 225),
Planet(3, "Earth", "you are here", 24889, 365),
Planet(4, "Mars", "the red planet", 13256, 687),
Planet(5, "Jupiter", "largest planet", 278985, 4320),
Planet(6, "Saturn", "sun's bae with all 7 rings", 235185, 10620),
Planet(7, "Uranus", "can only be seen with a telescope", 99739, 30240),
Planet(8, "Neptune", "it is an intense blue color", 96645, 59400),
Planet(9, "Pluto", "no dwarf in my book", 7144, 88920)

Choose a reason for hiding this comment

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

Yes! Pluto is a planet 💯

]

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

@planets_bp.route("", methods = ["GET"])
def get_all_planets():
planets_response = []
for planet in planets:
planets_response.append({
"id": planet.id,
"name": planet.name,
"description": planet.description,
"circumference": planet.circumference,
"length of year": planet.length_of_year
})
return jsonify(planets_response)

Choose a reason for hiding this comment

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

This is where we can use our handy dandy to_json() helper method in the Planet class. And we should also return a 200 status code.

def get_all_planets():
	planets_response = []
	for planet in planets:
		planets_response.append(planet.to_json())
	return jsonify(planets_response), 200

Choose a reason for hiding this comment

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

We can also use list comprehension to build planets_response.


@planets_bp.route("/<id>", methods = ["GET"])
def get_one_planet(id):
try:
id = int(id)
except:
return abort(make_response({"message":f"Planet {id} is invalid."}, 400))

for planet in planets:
if planet.id == id:
return jsonify(planet.to_json())

Choose a reason for hiding this comment

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

Don't forget the status code!

return abort(make_response({"message":f"Planet {id} not found."}, 404))

Choose a reason for hiding this comment

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

Nice use of a try/except. The validation for bad id's and for id's with no record looks good. We can also consider moving the validation portion of this function into a helper method.