RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Calling services with Python and JavaScript
Lecture: Calling services with Python clients

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now let's focus on some programmatic clients. Notice, this is the source code repository and I have created a svc_1clients
0:09 and I've created a Javascript section and a Python section, so we are going to go look at those now in PyCharm.
0:17 So I have already opened up that subfolder there, by just dropping it onto PyCharm, and we've got some pre-built
0:23 HTML, jQuery type stuff going on over here, and we'll get to that last. So let's add a Python, I'll call this svc_client here,
0:33 and so this is going to make a request up to our autos, so maybe it'll show you the various ones that will like list them out
0:43 and ask you which one would you like to work with, and then you can plot the id and they will go get that who knows, something to this effect.
0:50 Now, we could use the built in urllib stuff, but when you have something like requests available, why would you do that,
0:57 so let's say import requests, now notice there is a couple of problems PyCharm says you don't have this installed,
1:04 so we can just hit enter and it will install it, or we could have opened up here and just say oops,
1:09 open up the terminal, it's a pip install requests, either one would have worked, you can see PyCharm already beat us to the punch.
1:16 Now, when you go to run this code, realize that you're also going to have to
1:19 manually install requests, there's not like a requirements.txt for this part, there is the setup.py for the main app, but not here.
1:27 So let's go ahead and just set this as the thing that runs for now, when I say go, all right, it's doing nothing but it is doing its thing.
1:35 So let's go over here and we can make a request, so let's just add a main method really quick,
1:42 and the standard naming, the standard invocation convention there so first of all, let's list out what ones are available,
1:54 so we'll say this cars= list cars, something like that so we'll define that and down here in order to get started
2:03 we're going to need a url and again, this is going to be living at local host, like that,
2:11 so we'll just grab the url there, again adjust this for however your system is running, and you may be familiar with the requests,
2:19 if you are not, actually I have a whole class on all sorts of service clients, http clients and various other service clients
2:26 but we'll just go for the super quick version here just so you can see it in action,
2:30 now this code could be running in a script on my computer, as it is or it could be running as part of our web application,
2:37 consuming a service somewhere else right, there is no real distinction between whether this is service side or client side, for the most part.
2:44 Okay, so we're going to go here, and we want to do a get against this so we'll say a response= request.get url that's pretty easy right
2:53 and then we can print a response.status code so let's just run this, 200, oh that's good, it didn't crash, it didn't 404, things like that.
3:06 Now the next thing we want to do, we obviously want to check that status code so let's just say if there's a couple of things we can do here
3:13 we can say raise if there's some kind of status that's an error or we could come over here and we could say if the status is not equal to 200
3:21 print error contacting server, and put that out, and we can just bail.. So we won't add too much error handling to this
3:31 but you know, you get the idea right, we want to add the error handling. So here will have all of our cars, they are going to be response.
3:38 now we have text here, we could parse that out, but we can also call this json transformation function and that will give us all of the cars,
3:46 so let's just print out cars to see what we get back here and notice we have a list of these cars, and the cars have an id
3:53 and they have a name, so let's just return the name:id, maybe id:name is better ; so here, we'll return a list of car.get_id, card.get_name
4:07 and what his car well for car in cars, okay, so this is going to return them back, and we can come over here
4:18 and do something like show cars, right, we can create a little function here
4:23 and who knows, we'll just a little quick print, cars from cars for sale Opels, right for c in cars print, and remember this is coming back as a tuple,
4:35 I guess we could return the whole car, but whatever, we'll just print it like this. Now if we run this, you can see now we're getting all the cars back
4:44 and then I guess to a kind of round this out we could go and actually get an individual car
4:50 we could say car id= what car do you want to see, I'll say car id this and we'll do that as an input, so input here and no error checking
5:03 to make sure that they didn't enter whatever, we'll just say so car details for a car id and that is the last function that we have to write.
5:13 So here we have our list cars, and let's copy this real quick because what we're going to do, I can say get car, singular and car id.
5:25 So what we want to do is we want to use a slightly different url here remember, we passed the car id that's how we set up our route,
5:34 I get this and we get the single cars the json, and we can just return car, and in the case we type in something wrong
5:42 obviously this will come back as a 404 or something like that so we'll say no, no there was some kind of problem you couldn't get it.
5:48 This has got a reasonably good chance of being robust but there's probably more checking that we want to apply here.
5:54 Okay, so we're going to show the car once we ask for it, last thing, all right so we come over here, make sure we don't get the colon
6:00 it says what car do you want to see, this one here is going to be an Opel sinatra 2.2 so that's what we're hoping to get back,
6:09 and did we actually print it out, no, no, no, we did not whoops. Alright, so we can all get car but we're not actually doing it,
6:17 okay so say car= get_car ( car_id ) and then we just print out the car, we could probably do better formatting, but let's just go with that,
6:28 alright, let's get the same one, boom, look at that, how cool is this, well, let's do one more thing, let's print out a little bit better,
6:37 we can pretty print it at least, right, let's try this one more time, okay so there's all the cars, we're going to try to get this one by id
6:44 so give it the id and boom, there it is, this is the Opel sinatra 2.2 so it's pretty straightforward for us to work with this,
6:53 let's do a little clean up here, I'd love to have the main thing first and then the little like detail methods below it,
7:01 so this is like you can read that and just understand what's happening. Okay so here what have we done, let's quickly review
7:07 we import requests, we come down here doing a get against the url, checking the status code, using the json
7:14 to convert that from text into Python dictionaries here we're just doing little transformation to get just id and name out
7:20 and then given an id, we go and use the url we constructed do a get, json again but this time the body is just a single car so we're good to go,
7:30 and that's pretty much it, of course there's a little extra error handling and checking, but it's a pretty nice little Python client.


Talk Python's Mastodon Michael Kennedy's Mastodon