RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Content negotiation
Lecture: What is content negotiation?
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Now let's see how content negotiation is going to work. We've seen this picture before, we have our car listing method,
0:08
we have our database, a request is going to come in but now we have a set of renderers to select from,
0:13
you can see at the bottom we have a json one, a csv one, an xml one, maybe some others that we're not going into the details about.
0:19
So when a request comes in, the request is going to say I am accepting this type text/ csv, whatever, and just like before,
0:26
our code is going to run unchanged, we're going to do a little bit of work we're going to go ask the database for object
0:31
we're going to get our Python objects back maybe the sqlalchemy or mongo engine type of object; whatever it is, we have this object.
0:38
And now, the system is going to ask do I have a renderer for text/ csv, and we're going to look at our set of renders and say yes, I do
0:46
let's bring that one into play, feed our object to it and return the response there.
0:51
Of course, that renderer sets the content time to text/ csv and gives you all the values.
0:56
Okay, so this is really nice, and what's going to be even better is all of this do I have this renderer, what are the available renderers
1:04
bringing into play, that's going to be transparent to us, we're just going to return that green object
1:09
underlying system, pyramid is going to work out what renderer it needs to be used.
1:15
Now, before we move on, I just want to give a quick shot out to Cecil Phillip,
1:19
he wrote a nice article called "Content Negotiation With The Pyramid Web Framework" and he and I were talking about these different renderers
1:26
and he's like you know, does it do content negotiation and I'm like I don't really, I think so, he's like I bet we could make it,
1:31
and he's right, it's not that hard, he's written a really cool article here, the link is at the bottom, so if you want to go through this in detail,
1:36
and see what he wrote, he can explain a bunch of it there, and you can see the steps, of course we're about to go through much of what was talked about
1:42
slightly adapted and evolved but original idea is out to Cecil, so thank you Cecil for that.