-
Notifications
You must be signed in to change notification settings - Fork 61
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: Part 1 Solar System API: Hillary S. and Shannon B. #17
base: main
Are you sure you want to change the base?
Changes from 6 commits
5a1644c
0c6f9f1
43cd338
94e65be
815fa1a
98d3d0f
f979e6d
3e6d1d3
95ecda0
4f41dfd
4c525f2
9b70983
f8682b7
2715ec7
32a4b6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from flask import Flask | ||
|
||
|
||
def create_app(test_config=None): | ||
app = Flask(__name__) | ||
|
||
from .routes import planet_routes | ||
app.register_blueprint(planet_routes.bp) | ||
|
||
return app |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from flask import Blueprint, jsonify, abort, make_response | ||
|
||
class Planet: | ||
def __init__(self, id, name, description, has_moon=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it appears that you expect |
||
self.id = id | ||
self.name = name | ||
self.description = description | ||
self.has_moon = has_moon | ||
|
||
def make_dict(self): | ||
return dict( | ||
id = self.id, | ||
name = self.name, | ||
description = self.description, | ||
has_moon = self.has_moon, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 PEP8 (python formatting standard) recommends for named arguments to not but a space around the def make_dict(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) | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll be getting rid of this collection soon, but consider using named arguments here to help this be a little more self-documenting about what the various values are. Id and name are pretty clear, but it might be harder to tell that the second string is a description, and I'd be hard pressed to guess that the final boolean was about whether there was a moon there. |
||
|
||
bp = Blueprint("planets_bp",__name__, url_prefix="/planets") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
# GET /planets | ||
@bp.route("", methods=["GET"]) | ||
def list_planets(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Get all looks good. I like the function name you used too. Nice use of the list comprehension and instance method to build the dictionary for the instance. |
||
list_of_planets = [planet.make_dict() for planet in planets] | ||
|
||
return jsonify(list_of_planets) | ||
|
||
def validate_planet(id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Validation method looks good, for both detecting a bad id, as well as an id with no record. |
||
try: | ||
id = int(id) | ||
except ValueError: | ||
abort(make_response(jsonify(dict(message=f"planet {id} is invalid")), 400)) | ||
|
||
for planet in planets: | ||
if planet.id == id: | ||
return planet | ||
|
||
abort(make_response(jsonify(dict(message=f"planet {id} not found")), 404)) | ||
|
||
# GET planets/id | ||
@bp.route("/<id>", methods=["GET"]) | ||
def get_planet(id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Single planet endpoint looks good. We're able to reuse that dictionary instance method and move most of the logic into the validation method. We could get really crazy and even write the entire body as return jsonify(validate_planet(id).make_dict()) Though it's probably clearer the way you wrote it! |
||
planet = validate_planet(id) | ||
return jsonify(planet.make_dict()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍