RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Customer Renderers
Lecture: Generating image-based responses with renderers (v2)

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So we've seen that our car renderer, our car image renderer works really well.
0:06 You can see the image is here, but if you actually look at the network request it goes and it bounces just over to some other web server.
0:13 And you know, that may or may not be okay with you, but it's probably not really okay if you've got data as a binary blob in a database
0:20 because then what's the url for that, right? So, let's go and write a second version that actually pulls that data in
0:28 and streams it straight from our server, so as far as the clients can tell, it's our data coming through;
0:34 regardless of whether it comes out of our database, out of our file system or maybe even off of the internet, from somewhere else.
0:40 So let's make image, call this direct renderer, and redirect calls direct. Now you will know what the redirect part was about there.
0:50 Okay, so this is all going to work pretty much the same except for now that we're sending a response directly
0:55 we're going to need to have this type, so let's go and delete it out of the other. And because system is not used if you want,
1:03 you can put underscore and PyCharm will stop complaining and because we started using some pieces, also the static bit is not necessary, okay.
1:10 So, we get our request, we send a response type, we're going to try to adapt it, we're going to try to get the image
1:15 but instead of doing this, we're going to do something entirely different.
1:19 So now that we have the url, let's go over here and let's actually download it, let's go and say I'm going to download this off of the internet
1:27 if this was a binary blob, you would just have the data say out of sqlalchemy or something like that
1:32 but it's really off the internet, in this case we're going to go and say the response,
1:35 we'll use request, like who wouldn't want to use request in this class right. So let's make sure that we've got this registered in our system,
1:44 anytime you have a dependency like an external package like request you want to make sure that you put it here
1:49 so we install it in production or somewhere else, on new dev machine it automatically has that, so we could click the button, we could open this up,
1:56 and notice it's got our virtual environment active so pip install requests, just you can imagine how to click the button,
2:03 this also happened, just fine, so now give it a second, and that goes away.
2:07 So now we have requests, the one we're actually looking for not this pip thing, or we can do a get image url, and then we probably should check
2:16 so if so response.raise for status if something is wrong raising exception,
2:22 and then we want to return like a bytes array, some kind of stream that we can use, so we'll say return, and we are going to return bytes io from io,
2:32 and what are we going to do we're just going to give it the response content, like that. Now this one should go, have I changed this car one here—
2:42 let's turn this off for just a minute so we can test it. Let's click here and let's go get an individual car that's not going to work
2:51 but if we put first, there, so you can see that we got the car, right let me copy this, google wants to keep changing that around, okay,
3:00 so if we go there, you can see the car shows up, but notice it redirected, right,
3:04 there's a three or two redirect to this location, we want to stream it directly,
3:08 so let's go and switch here, by the way, the reason I commented this out is it's not so easy to set the accept type to png in the browser, right,
3:18 so we're just having one request, like you can do it, but it's not as easy. So let's go over here and say I want to get a different image renderer,
3:26 this is going to be direct, import that, so we can pick, right, which one do you want turn on the redirecting one, turn on the direct one.
3:34 So we'll run this again, notice we're using direct one, so if I go back over here and I enter this address, it should just stay here and show us image,
3:41 it shouldn't redirect like it just did, right, there's the redirect to opel, it stays, why does it stay, because as far as we're concerned
3:50 we make a request to the server, the server says you're getting png back and here's a bunch of bites, perfect, right?
3:57 Go and view the network, reload the page, you can see get right here set domain and when I restart it I got a different car, yeah perfect.
4:10 So it's just going straight here, right, it should stay the same now. Right if we switch this back, one more time so you can see the difference here,
4:18 hit this again, notice we did this request, we got it through to redirect and then we went over there, right.
4:25 So now we're here it's just staying there, but any time we go to this url you can see we're getting this redirect.
4:31 Like I said, this may or may not be okay, if you want to stream it straight out the database or suck it through off the internet
4:37 and stream it back through, then we can switch to this direct renderer and it stays right there.
4:44 Now we've got a wide variety of options for our renderers, we've got a csv renderer, we've got these two image renderers
4:51 let's go ahead and add one more before we kind of call this thing done, let's go ahead and add a json renderer,
4:57 and you might say Michael, why would we add a json renderer, we already have one right here, a built in one;
5:02 well, we're going to want to use some ability of grouping these together as a common set of objects, and let the api negotiator determine
5:11 which renderer is best, so having a renderer that exactly adapts to the same functionality as ours, and we can just of course delegate to this one,
5:19 it will be nice, so we want to add one more of these in our real app and then we'll be ready to talk about content negotiation.


Talk Python's Mastodon Michael Kennedy's Mastodon