Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask REST API Resourceful Blueprints Register API

Register API Not Passing

I have no idea whats going on

api.py
from flask import Flask

from resources.ingredients import ingredients_api
from resources.recipes import recipes_api

app = Flask(__name__)

app.register_blueprint(ingredients_api, url_prefix='/api/v1')
app.register_blueprint(recipes_api, url_prefix='/api/v1')

if __name__ == '__main__':
    app.run()
resources/ingredients.py
from flask.ext.restful import Resource, Api
from flask import Blueprint

import models


class IngredientList(Resource):
    def get(self):
        return 'IngredientList'


class Ingredient(Resource):
    def get(self, id):
        return 'Ingredient'

ingredients_api = Blueprint('resources.ingredients', __name__)
api = Api(ingredients_api)

api.add_resource(
    IngredientList,
    '/ingredients',
    endpoint='ingredients'
)
api.add_resource(
    Ingredient,
    '/ingredients/<int:id>',
    endpoint='ingredient'
)
resources/recipes.py
from flask.ext.restful import Resource, Api
from flask import Blueprint

import models


class RecipeList(Resource):
    def get(self):
        return 'RecipeList'


class Recipe(Resource):
    def get(self, id):
        return 'Recipe'

recipes_api = Blueprint('resources.recipes', __name__)
api = Api(recipes_api)

api.add_resource(
    RecipeList,
    '/recipes',
    endpoint='recipes'
)
api.add_resource(
    Recipe,
    '/recipes/<int:id>',
    endpoint='recipe'
)

2 Answers

Ryan S
Ryan S
27,276 Points

Hey hum4n01d,

It looks like you know what's going on. I was playing around with your code and found it quite frustrating that I couldn't get it to pass because everything looks correct. I could not find any errors.

But then after a while I read the challenge again and realized that they don't want you to use the "url_prefix" method. They want the '/api/v1' to be included in api.add_resource(...).

This is because Task 3 asks you to register the resources, not the blueprints in the app (that's the last task). At this stage we haven't officially got to the point where we can add the url_prefix. So the challenge was probably looking for the full url in the add_resource() method and couldn't find it.

It is really tricky wording and when I did this challenge originally I must've just happened to have used the acceptable method without really knowing it. But I think your code is perfectly fine outside of a code challenge.

Good luck.

Yeah I went back and figured that out but thanks!

Nathan Brenner
Nathan Brenner
35,844 Points

The error messages on Step 3 could be a lot more helpful. If you are checking endpoints in a browser, or using something like Postman, you'd get back a lot more help than 'didn't get 200 on /api/v1/ingredients/1'

I already figured this out out thanks! It was just a matter of the instructions not being clear. Thanks though