-
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
C17 Sea turtles, Celina Barron, Shelby Faulconer, and Tiffini Hyatt #13
base: main
Are you sure you want to change the base?
Changes from 7 commits
0d6e418
d3635a2
0d8bbec
c59cbbd
b9e3617
174a417
4c96a0d
aef2191
e4ae7ad
dfe4065
7f2ed54
0b3a9c7
c58002c
43e4d2d
8d6d02d
9c95797
f3c2a74
bd3ba79
7473c59
72fd43a
0509329
2df8138
de695d4
71ae69d
6ac2887
48837ce
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,2 +1,65 @@ | ||
from flask import Blueprint | ||
import json | ||
from unicodedata import name | ||
from flask import Blueprint, jsonify, abort, make_response | ||
|
||
|
||
class Planet: | ||
def __init__(self, id, name, description, dist_from_sun): | ||
self.id = id | ||
self.name = name | ||
self.description = description | ||
self.dist_from_sun = dist_from_sun | ||
|
||
def to_dict(self): | ||
return { | ||
"id" : self.id, | ||
"name" : self.name, | ||
"description" : self.description, | ||
"distance from sun" : self.dist_from_sun | ||
} | ||
|
||
planets = [ | ||
Planet(1, "Mercury", "rocky", 1), | ||
Planet(2, "Venus", "rocky", 2), | ||
Planet(3, "Earth", "water", 3), | ||
Planet(4, "Mars", "red", 4), | ||
Planet(5, "Jupiter", "big", 5), | ||
Planet(6, "Saturn", "rings", 6), | ||
Planet(7, "Uranus", "butt", 7), | ||
Planet(8, "Neptune", "ice", 8), | ||
Planet(9, "Pluto", "dwarf", 9) | ||
] | ||
|
||
planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") | ||
|
||
@planets_bp.route("", methods = ["GET"]) | ||
def planet_data(): | ||
planet_list = [] | ||
for planet in planets: | ||
planet_list.append({ | ||
"id" : planet.id, | ||
"name" : planet.name, | ||
"description" : planet.description, | ||
"distance from sun" : planet.dist_from_sun | ||
} | ||
) | ||
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. If the opening brackets are stacked together, I'd recommend doing the same with the closing brackets to be consistent. 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. To reduce repetition, we could reuse Planet's planet_list.append(planet.to_dict()) |
||
return jsonify(planet_list) | ||
|
||
@planets_bp.route("/<planet_id>", methods = ["GET"]) | ||
def get_planet_by_id(planet_id): | ||
planet = validate_planet(planet_id) | ||
return planet.to_dict() | ||
|
||
|
||
def validate_planet(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. I'd consider renaming this function. To me, |
||
try: | ||
planet_id = int(planet_id) | ||
except: | ||
abort(make_response({"message": f"planet {planet_id} is invalid"}, 400)) | ||
|
||
for planet in planets: | ||
if planet.id == planet_id: | ||
return planet | ||
|
||
abort(make_response({"message": f"planet {planet_id} not found"}, 404)) | ||
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 error handling in the helper function! |
||
|
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.
I see what you did there 😆