Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that the models exist, it's time to create the resources for the API.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
All right, so now it's time to
actually make the resources.
0:00
And instead of creating
them as a single file or
0:04
whatever, I'm gonna create
them as a directory.
0:08
And gotta click on that and
I'm going to call those resources
0:11
and then inside of here,
I need to make dunder and init.py.
0:17
And we don't need edit that file,
all right and
0:23
so now instead of here I'm going to
make a file for each of the resources.
0:26
It's a little easier to just break these
things up in multiple files because
0:29
they're a little bit bulky and you don't
have to do this but I highly suggest
0:33
doing this just because it makes
things a little easier to deal with.
0:37
So okay,
we're going to call this courses.py,
0:43
this is gonna be for our courses resource.
0:44
All right, so, from flask import jsonify,
0:49
which we'll use to turn
these into json responses.
0:54
And from flask.ext.restful
import Resource.
0:57
I'm actually going to break
those up just a little bit.
1:03
And then import models.
1:06
All right, cool.
1:07
So those are all of our basic
imports out of the way.
1:09
Class CourseList is a resource.
1:12
And this is the resource for
a list of courses.
1:16
And we're just going to define
the get method on this.
1:19
And so get handles whenever somebody
sends in a get request to the resource.
1:22
And we're going to return just jsonify,
courses and
1:28
then make up a fake course here,
we'll say the title is Python Basics.
1:34
And I know that we have URL in our model.
1:41
We're not actually dealing with the model
right now, so we can just leave that off.
1:43
We're just doing this to make sure that we
get a response that we like and expect.
1:46
All right, so
now we'll make another one here for
1:52
the course resource, and
this will be very similar.
1:54
But we're gonna create three methods
on here instead of just one.
1:59
We'll just do self and ID because
individual courses will always get an ID
2:03
or should, and we'll return jsonify,
title is Python Basics.
2:08
So basically,
I'm just making this stuff match.
2:17
And since, this is what I'm gonna
to use for the other two methods.
2:19
I'm just gonna copy them, and
this will be put and this will be delete.
2:25
All right.
2:32
Really I should have a post up here for
course list as well but
2:33
we'll build that one later whenever we
get to working with actual resources.
2:37
So jsonify, if you haven't seen it before,
2:41
turns whatever this thing is whatever's
in here into a json response.
2:44
So it turns it into a json string.
2:50
And it sets the content
type to applications /json.
2:52
It does all the things that you
need to do to make a json response.
2:57
And I know that I imported models here and
I'm not using it.
3:02
I will get to that, I promise.
3:06
We'll get to the database
lookups a little bit later.
3:08
All right, so now, let's actually,
let's actually take all of this,
3:10
copy it and make a new file in that folder
and we're gonna call this reviews.py.
3:15
So this is going to be
our reviews resource
3:21
and so we got to change a few things here,
3:26
we want to change this to ReviewList and
I wanna change this to Review.
3:28
And then here, I wanna send back reviews
instead of courses, I want to send back course.
3:35
Which is gonna be a 1, and I wanna send
back rating which is gonna be a 5.
3:44
All right, so copy that.
3:50
And we're gonna paste and
we don't need to copy that actually.
3:55
We're just gonna change this.
3:59
So this will just be course,
4:00
is 1 and rating is 5.
4:05
All right, and this will copy.
4:10
And paste.
4:15
All right, cool.
4:18
So, a lot of copying and
pasting just to get things up and running.
4:19
But there we go, so
we have everything up and working.
4:24
Now, there's not really anything
different about the reviews thing
4:28
other than the courses thing except for
names, courses list, reviews list,
4:32
things like that.
4:37
And these resources aren't really useful,
yet, because they don't do anything yet.
4:38
But this gives me a really
solid base to build my API.
4:43
And some more complete resources
a little bit later on.
4:48
We have resources but now we need
to tie them into our flask app.
4:52
So it'll know all about
the routes that we defined.
4:55
This can be a little tricky to do cleanly.
4:58
They gave me a lot of trouble actually
trying to get ready for this, but
5:00
trouble just means that you have something
else to learn, so let me show you a great
5:04
flask feature known as Blueprints
that solves exactly this problem.
5:07
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up