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