RESTful and HTTP APIs in Pyramid Transcripts
Chapter: A nearly RESTful service
Lecture: Introducing renderers
0:01 Now let's talk about how we can vary the response type and create different types of renderers,
0:07 the json one has been really great, but what if we want something like csv or xml or even images or binary data— all of these things are possible
0:15 in exactly the same way that we're able to render json. But let's look at the overall architecture and role of these renderers.
0:22 Imagine here that we have our car details view method so this is one that returns all the details about a single car given its id
0:29 and so a request is going to come in an d it says get/api/auto/ whatever the id of the car they're looking for is.
0:36 We got to do a little bit of work to juggle things in the web framework like go to the matchdict and pull out the car id and things like that,
0:42 but pretty quickly we're going to go to our database and return an object, and very likely especially for using an orm or we have a larger application
0:51 where we've put a proper architecture in place, this is going to be a native Python object or maybe even a full object graph
0:59 in the sense of like it could be a car, the car could have a list of previous owners,
1:03 it could have a list of like repair history, it could be quite a complex thing that comes back from the database.
1:10 Now we know that the client that requested this wants that back and for now let's say they want it back in json form,
1:18 we can't just return this object right, you know, maybe pyramid can convert it to a string, but it would just say you know
1:25 type name at memory address like that's completely useless we need something that converts this potentially complex object graph into proper json type.
1:35 We'd also like that thing to maybe set the response type to application/json as well so that's where the renderer comes in,
1:42 so the renderer is the thing that we're going to hand this object it does its magic and then responds in the actual
1:50 whatever it's supposed to render as, this one renders as json, so it's going to convert this object graph to json,
1:55 set the content type, our client gets the response and is happy and everything's working great. So that's how renderers work.
2:02 Now let's look at the few options that we do have as built in renderers. They fall on basically two categories,
2:08 the first I would call template oriented renderers, now these are really meant for the web side of things
2:14 the web site not the web service side of things, so these would include Chameleon, Jinja 2, Mako, right,
2:22 I'm writing some view method it's going to process a request maybe go to the database, get some stuff
2:28 and then it wants to return an HTML page to a browser, right so we can set up the renderer to process a Chameleon template
2:34 or a particular Jinja 2 template and give that model off to the Ji nja 2 engine and then it will do its magic and outcomes HTML.
2:42 These are great, they are super important but they're not very relevant for our services in the actual api methods.
2:49 Next, we have json, we've played with json exclusively so far and json works great, that one is built in and we are happy to have it
2:57 we'll see how to configure it in just a little bit we haven't taken it as far as we can, but json is great.
3:02 There's also json p which is json with padding it's commonly used to bypass cross domain policies in web browsers
3:09 but remember, we already set up cors the permissions on our site so we don't really care or need to use that in our particular application,
3:16 but it's available if you need it in yours. And that's it, these are the built in ones, right, basically HTML templates and json.
3:22 If you want something else like let's say csv, xml, images well, are we out of luck, no of course not,
3:31 we'll see really quickly how to build our own for each of those types and more.