-
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
SHARKS - Jande R. and Lindsey S. #7
base: main
Are you sure you want to change the base?
Changes from 2 commits
57f27d3
0efeb9a
769c4cf
6595fd2
93954be
ff2ec6e
a8fae40
cc4aec6
443e0ca
2f0d8eb
32bbf87
92d9344
645b5e6
048f1fc
d0d1fd7
88f5073
a5f903f
8fd9181
ba996be
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,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 | ||
} | ||
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. 👍 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) | ||
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. 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) | ||
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. This is where we can use our handy dandy def get_all_planets():
planets_response = []
for planet in planets:
planets_response.append(planet.to_json())
return jsonify(planets_response), 200 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 can also use list comprehension to build |
||
|
||
@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()) | ||
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. Don't forget the status code! |
||
return abort(make_response({"message":f"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. 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. |
||
|
||
|
||
|
||
|
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.
👍 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.