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