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

C22_Phoenix_Amber & Sphinx_Rong #64

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from flask import Flask
from app.routes.planet_routes import planets_bp


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

# Register blueprints here
app.register_blueprint(planets_bp)

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

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


Planets = [
Planet(1, "RongRong", "medium sized black planet with storming seas"),
Planet(2, "BerBer", "small yellow planet with 2 moons"),
Planet(3, "GT-23", "giant purple planet with rings"),
]
2 changes: 0 additions & 2 deletions app/routes.py

This file was deleted.

Empty file added app/routes/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions app/routes/planet_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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():
planet_list = []
for planet in Planets:
planet_list.append(planet.to_dict())
return planet_list


@planets_bp.get("/<planet_id>")
def get_one_planet(planet_id):
planet = validate_planet(planet_id)

return planet.to_dict(), 200

def validate_planet(planet_id):
try:
planet_id = int(planet_id)
except:
abort(make_response({"message": f"{planet_id} is not a invalid id"}, 400))

for planet in Planets:
if planet.id == planet_id:
return planet
abort(make_response({"msg": "Planet id not found"}, 404))


6 changes: 3 additions & 3 deletions project-directions/wave_01.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
Perform following setup steps for the Solar System API repo to get started on this Flask project:

1. Create a virtual environment and activate it
1. Install the dependencies
1. Define a `Planet` class with the attributes `id`, `name`, and `description`, and one additional attribute
2. Install the dependencies
3. Define a `Planet` class with the attributes `id`, `name`, and `description`, and one additional attribute
1. Create a list of `Planet` instances

## RESTful Endpoints: Read
Create the following endpoint(s), with similar functionality presented in the Hello Books API:

As a client, I want to send a request...

1. ...to get all existing `planets`, so that I can see a list of `planets`, with their `id`, `name`, `description`, and other data of the `planet`.
1. ...to get all existing `planets`, so that I can see a list of `planets`, with their `id`, `name`, `description`, and other data of the `planet`.`
4 changes: 2 additions & 2 deletions project-directions/wave_02.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ Create the following endpoint(s), with similar functionality presented in the He
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.
2. ... 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.
3. ... 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.