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

Sarah and Brooke - Pine - Part 1 #16

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
25 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
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 import planets_bp
app.register_blueprint(planets_bp)

return app
13 changes: 13 additions & 0 deletions app/list_of_planets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .planets import Planet

planets = [
Planet("Mercury", "Closest to the sun, hot.", False),
Planet("Venus", "Spins the opposite direction of other planets.", False),
Planet("Earth", "You are here."),
Planet("Mars", "Red planet."),
Planet("Jupiter", "Gas Giant."),
Planet("Saturn", "Has rings."),
Planet("Uranus", "Controversy over pronunciation."),
Planet("Neptune", "Smallest gas giant, with faint rings."),
Planet("Pluto", "Brooke still believes!")
]
20 changes: 20 additions & 0 deletions app/planets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Planet:
number_of_planets = 0
def __init__(self, name, description, has_moons=True):
self.name = name
self.description = description
self.has_moons = has_moons
self.id = Planet.number_of_planets
Planet.increase_number_of_planets()

Choose a reason for hiding this comment

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

Neat idea!


@classmethod
def increase_number_of_planets(Planet):
Planet.number_of_planets += 1

def create_planet_dictionary(self):
return {
"id": self.id,
"name": self.name,
"description": self.description,
"has moons": self.has_moons
}

Choose a reason for hiding this comment

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

Great helper function!

19 changes: 18 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
from flask import Blueprint
from flask import Blueprint, jsonify
from .list_of_planets import planets

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

@planets_bp.route("", methods=["GET"])
def handle_planets():

list_of_planets = []
for planet in planets:
list_of_planets.append(planet.create_planet_dictionary()), 200

return jsonify(list_of_planets)

@planets_bp.route("/<planet_id>", methods=["GET"])
def handle_planet(planet_id):
for planet in planets:
if int(planet_id) == planet.id:
return jsonify(planet.create_planet_dictionary())

Choose a reason for hiding this comment

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

💯

3 changes: 3 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Notes
Follow the directions in Learn - Building an API part 1 activity
This will be very similar to the 'books' API