-
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 Heather M & Jenny C #19
base: main
Are you sure you want to change the base?
Changes from 9 commits
084030d
24657bc
25906a6
4b28460
59817b9
65f3d5e
b3e6972
dd8402b
038b790
9446e23
d7ad61e
477dfc4
7d8e0e9
08ca317
b5c3698
8e8507a
5ac6a36
0d07b5e
a96a938
455e34a
10690f2
d5b4eba
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,47 @@ | ||
from flask import Blueprint | ||
from flask import Blueprint, jsonify, make_response, abort | ||
|
||
|
||
class Planet: | ||
def __init__(self, id, name, description, gravity): | ||
self.id = id | ||
self.name = name | ||
self.description = description | ||
self.gravity = gravity | ||
|
||
planets = [ | ||
Planet(1, 'Mercury', 'The closest planet to the sun! REALLY HOT!', '3.7 m/s2'), | ||
Planet(2, 'Venus', 'Another hot planet! Actually hotter than Mercury!', '8.87 m/s2'), | ||
Planet(3, 'Earth', 'Third Planet from the Sun. Maybe a little special. Much colder than the first two.', '9.8 m/s2') | ||
] | ||
|
||
planets_bp = Blueprint("planets", __name__, url_prefix="/planets") | ||
|
||
@planets_bp.route("", methods=["GET"]) | ||
def handle_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. Conventionally you would see the naming of the function be similar to the CRUD operation you are performing. Like |
||
planets_result = [] | ||
for planet in planets: | ||
planets_result.append(dict( | ||
id = planet.id, | ||
name = planet.name, | ||
description = planet.description, | ||
gravity = planet.gravity | ||
)) | ||
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 could be moved to your planets class as an instance method. Then you would call that instance method here.
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. Yup. We should refactor it like that. Thank you for the suggestion. |
||
return jsonify(planets_result) | ||
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. Here its nice to add a 200 status code. Even though it happens by default it adds readability to your code.
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. That is a good idea. We'll add this.Thank you Trenisha |
||
|
||
@planets_bp.route("/<planet_id>", methods = ["GET"]) | ||
def get_planet(planet_id): | ||
try: | ||
planet_id = int(planet_id) | ||
except: | ||
abort(make_response(jsonify(dict(details=f"planet id {planet_id} invalid")), 400)) | ||
|
||
for planet in planets: | ||
if planet.id == planet_id: | ||
return { | ||
"id": planet.id, | ||
"name": planet.name, | ||
"description": planet.description, | ||
"gravity": planet.gravity | ||
|
||
} | ||
abort(make_response(jsonify(dict(details=f"planet id {planet_id} not found")), 404)) |
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.
this looks good!