Skip to content
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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
def create_app(test_config=None):
app = Flask(__name__)

from .routes import planets_bp
app.register_blueprint(planets_bp)


return app
47 changes: 46 additions & 1 deletion app/routes.py
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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks good!

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():
Copy link

Choose a reason for hiding this comment

The 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 get_planets or get_all_planets

planets_result = []
for planet in planets:
planets_result.append(dict(
id = planet.id,
name = planet.name,
description = planet.description,
gravity = planet.gravity
))
Copy link

Choose a reason for hiding this comment

The 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.

 def make_dict(self):
        return dict(
            id=self.id,
            name=self.name,
            description=self.description,
            gravity=self.gravity,  
        )

Choose a reason for hiding this comment

The 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)
Copy link

Choose a reason for hiding this comment

The 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.

return jsonify(planets_result), 200

Choose a reason for hiding this comment

The 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))
3 changes: 3 additions & 0 deletions project-directions/wave_02.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ As a client, I want to send a request...
1. ...to get one existing `planet`, so that I can see the `id`, `name`, `description`, and other data of the `planet`.
1. ... such that trying to get one non-existing `planet` responds with get a `404` response, so that I know the `planet` resource was not found.
1. ... such that trying to get one `planet` with an invalid `planet_id` responds with get a `400` response, so that I know the `planet_id` was invalid.