RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Web application building blocks
Lecture: Pyramid building block: Views
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
The first building block that we want to look at are views or in mvc nomenclature controllers.
0:07
Now, these can be individual functions or they can be methods on a class and we can set those up either way in Pyramid.
0:14
Right now, we're keeping it as simple as possible, here is just a function called album list and onto this we're going to add a decorator
0:22
that says this is actually a view method and we state the name of the route, so In this case we are going to register a url
0:29
and we're going to give it a name called albums and when we return something, we need to tell it how to translate that data.
0:37
Often in Pyramid or many web apps, you'll see this being some kind of template engine, Chameleon or Jinja 2 or something like that.
0:44
In this case, we're using a custom json serializer called readable json. So think of it as a json serializer that does indentation, more or less.
0:53
So we've set up this function to respond to a particular url, which is possibly passing data to us, in this case it's not but it could be,
1:02
and we're telling it to render json back to the user. We'll look at this render stuff in detail, but let's focus on the method.
1:09
So all of these Pyramid methods take a request object and this has all the things that you could possible want to learn about the Request-
1:17
cookies, headers, url query string, user agent, all that kind of stuff. And now we're just going to return some kind of dictionary
1:25
that can be converted back into json, so we're turning the dictionary it has one key called albums
1:31
and in there it has a set basically the value is a list of albums and by default those individual items either need to be
1:41
fundamental types like strings and numbers or in this case probably sub dictionary, so each album, album 1, album 2 represents a dictionary.
1:49
However, we can do more interesting stuff with a serializer or renderer later
1:52
by default that doesn't happen, so we're just going to return this json object, so somebody does it get requests to let's say /api/albums
1:59
we return a json object based on this data. The way it works is we define our view method taking a request object
2:08
and then we're going to return a model in the form of a dictionary and pass that to some render, in this case a json serializer.
2:16
That last view, while representing many of the pieces of what goes into creating a view wasn't very realistic, let's look at a more realistic one here.
2:26
So, in this case we're going to let the consumer of this API pass in some kind of id for a car, and we're going to return the details about that car,
2:36
so again we have the view config with the route name and the renderer, but notice this time we're only responding to get methods,
2:43
that means if we wanted to let them say update via a put method to the same car with the same id,
2:50
that could be a separate method, so this one just shows the details of the car. Now, we need some way to pull that car id out of the url,
2:58
and when you define the routes, you can put a little place holders, little route cutouts for values that match, and then
3:05
those keys and values show up in this thing called the match dict. So here we're asking for the car id, we're going to go to our data layer
3:13
via the repository and ask for the car by id and notice that if there is no car rather than returning none or something silly like that,
3:21
we're going to respond the way the web should respond, a web server should respond to something that doesn't exist
3:27
we should return a 404, and just for a good measure we're going to go and throw in some kind of error message as a json response as well,
3:35
but, if we do find the car, we're going to return that car back to the caller. And like I said, unless we go and do something special,
3:43
this car must be a dictionary or something that's natively serializable to json, but as we get further into this class,
3:50
we'll see that those can actually be rich objects if we set up the renderer correctly.