-
Notifications
You must be signed in to change notification settings - Fork 36
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
f02b6d0
1c46357
5bd2806
896363b
919cb19
3cd2e11
52b5374
c30e7fa
4300cb6
caba01d
b22b2fb
a3fea77
24466c2
aa9e5bb
e4223d7
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,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 |
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") | ||||||||||||||||
|
||||||||||||||||
@planets_bp.route("", methods=["GET"]) | ||||||||||||||||
def get_all_planets(): | ||||||||||||||||
planet_list = [] | ||||||||||||||||
for planet in planets: | ||||||||||||||||
planet_list.append(vars(planet)) | ||||||||||||||||
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. Great use of vars! 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. I just learned that |
||||||||||||||||
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!") | ||||||||||||||||
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. Nice work handling an invalid parameter. I learned from Auberon for some strange pythonic reasons,
Suggested change
|
||||||||||||||||
planet_id = int(planet_id) | ||||||||||||||||
for planet in planets: | ||||||||||||||||
if planet.id == planet_id: | ||||||||||||||||
return jsonify(vars(planet)) | ||||||||||||||||
return ("Not Found!") | ||||||||||||||||
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. Nice work handling a planet that was not found. We can return a 404 response code with this:
Suggested change
|
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.
Minor note: to enhance readability, consider removing some blank lines and put each
Planet
instance on it's own line