RESTful and HTTP APIs in Pyramid Transcripts
Lecture: Lightning review: Content negotiation
0:00 Returning json was fun, but we looked at returning different types we returned csv, we return json, including parsing custom types as part of the json,
0:11 and images, so that also was good, but how do we negotiate that, how do we make this something the client can ask for automatically
0:19 without us being involved? That's where content negotiation comes in. So here's how we created a negotiating renderer factory.
0:25 Now the idea was this is basically a renderer itself that means that it has a _call, a dunder call method
0:34 which returns a function, and that function takes a value and a system and then it returns the serialized result or some kind of response of that.
0:42 So here we get it set up, we say look, we're going to provide you a bunch of renderers
0:46 and then we'll key them off of the content type, so will you add these renderers,
0:50 what content type does his thing render and what is it, so we'll store that here, if somebody calls us to create a new renderer to process a request
0:59 we just say yeah I just use our renderer function here and then in this one, we're going to take the value,
1:04 figure out which renderer it should go with, according to the request
1:07 and then serialize it or crash if we can't deal, so we're going to go to the request get the accept headers, do a little work, because remember,
1:14 this could be a string of like I'll take this, and if I don't have this I'll have this other one, so we got to take that apart,
1:19 loop over those, see if sort of highest priority first and say do you have a renderer that'll do this, no, ok, how about another one
1:25 and we'll just go through them and if they're all exhausted well too bad, I guess we can't help you we'll throw some kind of exception.