Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from flask import Flask
from app.routes.planet_routes import planets_bp


def create_app(test_config=None):
app = Flask(__name__)

app.register_blueprint(planets_bp)

return app
Empty file added app/models/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions app/models/planet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Planet:
def __init__(self, id, name, description, distance_from_sun):
self.id = id
self.name = name
self.description = description
self.distance_from_sun = distance_from_sun

Choose a reason for hiding this comment

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

How would you write the to_dict instance method so you can take an instance of Planet and turn it into a dictionary that will be returned as a response to a client's request?

planets =[

Choose a reason for hiding this comment

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

Suggested change
planets =[
planets = [

Nit: missing whitespace after =

Planet(1, "Mercury", "smallest and hottest planet", 57.9),
Planet(2, "Venus", "second planet from the sun", 108.2),
Planet(3, "Earth", "our home planet", 149.6),
Planet(4, "Mars", "the red planet", 227.9),
Planet(5, "Jupiter", "largest planet in the solar system", 778.3),
Planet(6, "Saturn", "has beautiful rings", 1437.0),
Planet(7, "Uranus", "rotates on its side", 2871.0),
Planet(8, "Neptune", "farthest planet from the sun", 4497.1)
]
2 changes: 0 additions & 2 deletions app/routes.py

This file was deleted.

Empty file added app/routes/__init__.py
Empty file.
50 changes: 50 additions & 0 deletions app/routes/planet_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from flask import Blueprint, abort, make_response
from app.models.planet import planets


planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets")

@planets_bp.get("")
def get_all_planets():
result_planets = []
for planet in planets:
result_planets.append(
{
"id" : planet.id,
"name": planet.name,
"description" : planet.description,
"distance_from_sun" : planet.distance_from_sun
}
Comment on lines +11 to +17

Choose a reason for hiding this comment

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

Inconsistent white spaces

Suggested change
result_planets.append(
{
"id" : planet.id,
"name": planet.name,
"description" : planet.description,
"distance_from_sun" : planet.distance_from_sun
}
result_planets.append(
{
"id": planet.id,
"name": planet.name,
"description": planet.description,
"distance_from_sun": planet.distance_from_sun
}

)
return result_planets

# Helper function for error handling
def validate_planet(id):
try:
input_id = int(id)
except:
abort(make_response(f"Planet id: '{id}' is invalid. Enter a valid planet number from 1 to 8.", 400))


for planet in planets:
if planet.id == input_id:
return planet
abort(make_response(f"Planet id: '{id}' not found. Enter planet id from 1 to 8", 404))



@planets_bp.get("/<id>")
def get_one_planet(id):

planet = validate_planet(id)
return {
"id" : planet.id,
"name" : planet.name,
"description" : planet.description,
"distance_from_sun" : planet.distance_from_sun
}
Comment on lines +40 to +45

Choose a reason for hiding this comment

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

Needs one more level of indentation

Suggested change
return {
"id" : planet.id,
"name" : planet.name,
"description" : planet.description,
"distance_from_sun" : planet.distance_from_sun
}
return {
"id" : planet.id,
"name" : planet.name,
"description" : planet.description,
"distance_from_sun" : planet.distance_from_sun
}