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


Talk Python's Mastodon Michael Kennedy's Mastodon