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

Pine - Mariah and Kayla #14

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#create app function and registering blueprints
from flask import Flask


def create_app(test_config=None):
app = Flask(__name__)
from .routes import planets_bp #importing from routes
app.register_blueprint(planets_bp) #registering the route

return app
44 changes: 43 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
from flask import Blueprint
#define our class and make bluprints
from flask import Blueprint, jsonify

#class are capitalized
class Planet:
def __init__(self, id, name, description, num_moons):
self.id = id
self.name = name
self.description = description
self.num_moons = num_moons

planets = [
Planet(1, "Earth", "Green and Blue", 1),
Planet(2, "Mars", "Red and Orange", 2),
Planet(3, "Jupiter", "Gray and Brown", 79)
]

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

@planets_bp.route("", methods=["GET"]) #decorator communicates with flask
def get_planets(): #function does the work
planets_response = []
for planet in planets:
planets_response.append({
"id" : planet.id,
"name" : planet.name,
"description" : planet.description,
"num_moons" : planet.num_moons
})
return jsonify(planets_response)

Choose a reason for hiding this comment

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

👍


@planets_bp.route("/<planet_id>", methods=["GET"])
def get_planet(planet_id):
for planet in planets:
if planet.id == int(planet_id):
response = {
"id" : planet.id,
"name" : planet.name,
"description" : planet.description,
"num_moons" : planet.num_moons
}
return jsonify(response)

Choose a reason for hiding this comment

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

Nice!


return 'Planet ID not found', 404

Choose a reason for hiding this comment

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

💯