RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Customer Renderers
Lecture: Adding the JSON renderer to the ABCs (abstract base classes)

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So to get the most functionality out of these types
0:03 we want everything to be at least one of these abstract base render types here,
0:08 we want to obviously implement the protocol of being a renderer factory
0:13 and then we have our concept of adding an adapter
0:16 and then, when we get to using more than one of them
0:19 you would like to be able to ask hey, can any of you serialize this particular value,
0:23 so given some value true or false can you serialize it?
0:26 Now, specifically that functionality is not part of the built in ones, right
0:31 and we might as well even though the function already implemented as we've seen
0:35 we might as well add the constraints for these as well onto the built in one,
0:39 so let's add a json renderer, and put it here,
0:44 this is going to be a pretty simple class
0:47 so we're going to say class, let's call it just like the others json renderer factory
0:52 and it's going to need these various functions;
0:55 however, we've already seen there's a built in one over here, that, right
1:01 so let's go ahead and leverage that, let's say this thing derives from json
1:05 and that helps, let's import this right, so it derives from this,
1:09 and that gives it most of the functionality, so you'll see
1:12 that we could actually come over here now and say
1:16 we're going to implement one of these,
1:18 not a renderer abstract base class but one of these
1:20 and everything should still work, let's see,
1:22 all right so it runs, that's a good start, if I go over your postman and I say
1:25 give me all the autos, and I ask for this, look, it is still serialized as json, just fine.
1:30 Okay, but if I try to go to it and I say you are also a renderer abstract
1:38 let me just copy this, a renderer abstract base class
1:42 and we try to run it one more time, it's not going to work out so well
1:47 because it doesn't implement can serialize value, alright.
1:51 So in order for us to sort of interrogate all of our renderers,
1:54 we got to be able to ask this, can you serialize this value
1:57 and right now obviously the built in one doesn't,
2:00 that's why we're going to this whole exercises, we want this function to exist.
2:04 So we'll come down here, we'll implement this
2:09 but this is just straight up json, like how do we know whether it can serialize it,
2:13 how do we know whether inside of the json type some adapters have been registered
2:18 or whatever, right, via the base class here.
2:21 So let's just say true, yep, I can probably do this, how do we know,
2:27 I mean we could try serializing it, but that's going to have a performance cost,
2:31 I'm going to go with this for now, but maybe
2:34 there's a better option we'll find throughout this class as we work our way through it.
2:37 Okay, so now does it run, does it work, yes
2:40 let's go over here and get a first auto and we'll get the image,
2:43 we got a different one, that's a cool classic,
2:47 now this one should give us json, actually
2:51 I think I commented that one out didn't I, for the browser, let's go put it back.
2:55 Put that back, try again, we should get a different picture of the same one,
3:04 so there's only a few options, actually let's go over here and add an accept,
3:08 I think we got to be more explicit, json, there we go.
3:12 Alright, so we're getting json for this one and if we go back
3:16 ok, it looks like our custom json renderer, which is not very custom for sure right
3:23 it's just basically adding that function and those constraints to the json
3:28 that comes from pyramid, but it looks like this custom json renderer
3:32 is working just like it was before, so the next thing we need to do
3:36 is instead of doing all of this work up here, right,
3:39 like oh yeah this content type goes here, that content type goes there
3:43 we would like to be able just to say hey system, figure out what the renderer is
3:47 and give us the best option that you can,
3:51 and if you can't support any options that have been requested
3:53 or any of the defaults, give us an error,
3:56 so that's what the next chapter is about, right.
3:58 So now we have all these different renderers, let's put them together
4:01 in a way that is basically transparent to all the api methods.