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

Sharks - Sana Pournaghshband and Camilla #20

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
5 changes: 4 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
def create_app(test_config=None):
app = Flask(__name__)

return app
from .routes.planet_routes import planet_bp
app.register_blueprint(planet_bp)

return app
2 changes: 0 additions & 2 deletions app/routes.py

This file was deleted.

Empty file added app/routes/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions app/routes/planet_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from flask import Blueprint, jsonify, make_response, abort
'''
Defined a Planet class with the attributes id, name,
and description, and moons. Also, Created a list of Planet instances.
'''


class Planet():
def __init__(self, id, name, description, moons):
self.id = id
self.name = name
self.description = description
self.moons = moons

def to_json(self):
return {
"id": self.id,
"name": self.name,
"description": self.description,
"moons": self.moons
}

planets = [
Planet(1, "Mercury", ["Grey", "closest to the sun", "smallest planet"], False),
Planet(2, "Venus", ["Brown and grey", "hottest planet"], False),
Planet(3, "Earth", ["Blue, brown green and white", "water world","1 moon"], True),
Planet(4, "Mars", ["Red, brown and tan", "2 moons"], True),
Planet(5, "Jupiter", ["Brown, orange and tan, with white cloud stripes","largest planet", "79 moons"], True),
Planet(6, "Saturn", ["Golden, brown, and blue-grey"," large and distinct ring system" ,"82 moons"], True),
Planet(7, "Uranus", ["Blue-green", " holds the record for the coldest temperature ever measured in the solar system ","27 moons"], True),
Planet(8, "Neptune", ["Blue"," on average the coldest planet" ,"14 moons"], True)
]

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

#Get all planets
@planet_bp.route("", methods=["GET"])
def read_all_planets():

Choose a reason for hiding this comment

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

Nice job naming the method to describe what this route does.

Since you put your routes in planet_routs.py in routes directory, you can even call this read_all() since we know that all the routes in planet_routes.py are related to the Planet class.

planets_response = []

for planet in planets:
planets_response.append(planet.to_json())

return jsonify(planets_response)

Choose a reason for hiding this comment

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

Adding 200 status code adds clarity even though it happens by default

return jsonify(planets_response), 200


'''
Created the following endpoint(s). This API can handle requests such as the following:
...to get one existing planet, so that I can see the id, name, description,
and other data of the planet.
... 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.
... 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.
'''

def validate_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 planet

return abort(make_response({"message": f"planet {id} is not found"}, 404))

Choose a reason for hiding this comment

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

LGTM 👍🏽


#Get one planet
@planet_bp.route("/<id>", methods=["GET"])
def read_one_planet(id):
planet = validate_planet(id)

return jsonify(planet.to_json(), 200)