Consuming HTTP Services in Python Transcripts
Chapter: Calling SOAP services from Python
Lecture: Blog explorer: Via SOAP and suds

Login or purchase this course to watch this video and the rest of the course contents.
0:02 I think it's time to start calling some soap services from Python, what do you think?
0:06 Over here in the service, if we scroll down we can get to the soap section.
0:09 there you can see our operations here, and we have our wsdl,
0:12 so let me copy our wsdl here, and let's go over to PyCharm here
0:16 and write the soap suds version of the blog explorer.
0:20 Okay, first thing we have to do to get started is
0:23 we are going to have to import this thing called a client,
0:26 so let's import like this, from suds.client,
0:30 we are going to import this thing called client like so,
0:33 and then we are going to use that and this little function
0:36 that we are going to use to create the client,
0:38 and we are going to do a little local cashing because we are not going to want to
0:42 regenerate this service description or pull down that service description
0:46 and parse it each time, okay,
0:49 so this sort of global suds client and if we've already got it,
0:53 we are just going to roll with that, but we are going to use this wsdl here,
0:56 that's the same url I just copied, and it's going to be super easy,
1:00 watch this, client wsdl, let's not allocate that string if we don't have to and for now,
1:06 let's just print out, well, let's just print out up here, we want to do print client,
1:13 like so, call create_client, now let's see what we get when we run this,
1:20 oh cool, look at that, first of all, we're running in Python 3 in our virtual environment,
1:25 and here is the client that says okay, it's this fedora hosted thing,
1:29 with this temp uri that comes from the service description,
1:31 and it says we have two ports, we can talk on BlogSoap or BlogSoap version 1.2,
1:38 it doesn't really matter which one, and we'll come in and we have 5 methods,
1:41 check this out, all posts create posts,
1:45 which takes a title, which is string, content and view count,
1:48 it has a delete post, it just takes an id,
1:50 a get post it just takes and id,
1:52 and an update post, which takes all of the values like this,
1:54 it also has two types that it exchanges,
1:57 for example, all posts returns this thing called an array of posts which is super annoying,
2:02 I'd really like it to return just an array but it does what it does,
2:05 and then, that's going to contain a bunch of posts which is actually what we want;
2:09 so, we can actually discover the details about this client by just creating it,
2:15 pointing at the wsdl and then print it out, okay, so that's pretty cool,
2:19 let's comment that out, I'll leave it for a moment, I'll comment it out in the end,
2:22 okay, so let's go ahead and implement one of these,
2:25 so we are going to go down here to show posts, noting to say there,
2:28 for get post notice, this is the requests version, this is the http restful version,
2:33 we are going to construct the url, construct the headers, construct the response,
2:37 check the status code, and then we are going to need to actually parse these ourselves,
2:42 I mean, it's not much work because it's a named tuple,
2:45 and we're doing dictionary unpacking but still, all of that stuff we had to do ourselves,
2:48 let's do it this way, so we are going to call create client and then,
2:54 the way we call functions I'll call this like so, we'll say client. service
3:00 and then it's going to have all those operations that we saw listed down here,
3:04 so for example all posts, I come down here and do all posts like this,
3:08 let me just print out post of zero, okay because what you are going to see,
3:12 let's just print out post for a minute, first of all, this should work straight away,
3:16 let me comment out this part right here, and tell it just for now, return something empty,
3:21 let's see what we get, I want a list so that should call all posts, and check that out,
3:25 it worked perfectly, it says sorry no posts, because we haven't parsed it yet,
3:29 so this looks great, post, post, post, rich data type- we have an array of posts,
3:34 which then has a thing which is posts,
3:37 so if we are actually going to get a hold of that array right there,
3:42 we are going to access it like this, which is too bad, let's do a list,
3:45 alright, so now what we got, it's basically like a tuple the first value
3:49 that we are going to get is actually the array, so this array is kind of a weird thing,
3:54 but, what we get back is an array of these post objects,
3:58 and it's great that they have a nice string representation as well,
4:00 so what that means is what we really want to do is
4:04 we want to kind of say posts= posts [0]
4:07 we should probably do some error handling to make sure
4:10 that this came back like we expected, but, we could do that,
4:11 we could also even go like so but that seems a bit too much, right,
4:16 so if this doesn't work out, I think we are going to get an exception,
4:19 and then we want to return this array, so what we return is posts, right,
4:24 that's it, create the client, call the function, return.
4:29 Alright, now this is actually pretty clean, right, the network level stuff is nasty,
4:33 no doubt about it, the fact you can't cash it is nasty, but this,
4:37 like this is the best version we've seen yet, isn't it?
4:40 Okay, so let's go over here and do a list, and it has no attribute id,
4:45 that is true, because in the wsdl, if you go over here, where is out thing,
4:51 we have a capital Id, capital Title, capital Publish, this is not a Python service,
4:56 it's not a Python class so its naming is non Pythonic
4:59 and so we just have to be aware of that right there, let's to this again,
5:02 list, ta- da, now we are back in business, updating for the various case differences,
5:08 working like this, we are going to go like that, okay,
5:12 so nice and clean create the client, get the post, and return it.
5:16 Well that worked pretty well actually, didn't it, let's see what happens
5:19 if we want to do add a post, okay, so down here, again we want to create the client,
5:23 the client.service.AddPost, let's see what this is called, it's actually CreatePost,
5:31 and I am just going to copy this, right here, so we know what to do,
5:34 this is all runtime, so PyCharm is not going to be able to help us out here,
5:38 so we are going to call CreatePost and it's going to take a title,
5:41 it's going to take the content and it's going to take the view count,
5:46 alright, and what comes back is an actual post object, and this particular API
5:51 we're not sending that along, but I am going to leave it here
5:54 just because I want it to be comparable to the size of what you saw before,
5:57 deleting that would kind of be unfair, okay, so this should add a post,
6:01 let's see, if we can add a post,
6:03 the title would be "My service is shiny and clean, it uses Soap!",
6:10 and the view count is 101, boom, look at that,
6:15 we created the service, My service is shiny and clean, it uses Soap,
6:19 and now let's list My service is shiny and clean it uses Soap, 101, okay.
6:24 Very cool, right, there is something to these Soap services,
6:27 even if they are not great in a lot of ways, there is something to be said for the simplicity.
6:31 Okay, so we are going to get the post, show the post, update the post,
6:34 and here we are just going to do this thing again, we are going to say
6:37 client=create_client, and then I just need to update this,
6:40 so client.UpdatePost and again, let me get this signature from up here,
6:45 so it's going to be id, title, content, view count,
6:53 alright, so that is going to update it, and let's run and update our post here,
6:57 so if I do a list, we'll see my service is shiny and clean that's number 3,
7:01 so let's do an update on number 3, and of course post has no title, does it, too bad,
7:08 let's try it again. I want to do a list, let's do an update, let's update number 3,
7:16 and of course I forgot my service thing here, service.UpdatePost,
7:23 okay, so we have to again adjust for the fact that
7:26 now our post objects have capital T title not lower case t title, alright,
7:31 let's run update, we'll do My shiny Soap service take the same title, same content,
7:36 but now it's pretty popular, let's say it's been viewed that many times.
7:39 Well, that didn't work so well, did it, I think maybe
7:43 because of what I am actually passing here is not the post id,
7:47 I am actually updating, I am actually sending the function id and of course,
7:53 it's not going to be able to find a post by that,
7:56 so last time let's do this update number 3,
7:59 same title same id but lots of views now.
8:02 Ta- da, it's updated that post successfully, do we believe it,
8:05 let's see, it's had 10 million views, ok, apparently it did
8:09 take that crazy number I typed into it,
8:12 cool, so my service is shiny and clean, it uses Soap.
8:16 Alright, last thing, if we want to delete a post,
8:19 okay, so we need to delete the post by id so again,
8:22 client=create_client() client.service.DeletePost(post.Id)
8:33 and again the title here, we'll say print delete it, let's create another doomed post,
8:42 so we'll say add your doomed Soapy, alright, so we can list it,
8:48 we can see that your doomed soapy is number 3,
8:51 and it's not getting too many views it's not a good sign, I think it's time to delete it,
8:54 it says which one do you want how about we delete number 3, deleting Soapy,
8:59 should be gone and of course, it is.
9:03 So that is how we use suds, and the real essence of it is we go down here,
9:07 we grab the wsdl, we call client and create an instance of the client,
9:11 passing the wsdl and then we just use that throughout our application,
9:15 object style as they say, so once we have this object,
9:18 we say client.service and we start calling functions
9:20 and it could take even a post object here as a parameter
9:24 but it doesn't and it just takes basic types like string string and int,
9:29 but it does return in our particular example a rich type which is the post,
9:33 alright, I am actually going to leave, this little print thing up here
9:36 because I think that is helpful for you as you explore this, but that's suds,
9:40 you see how you can use it to work with Soap services
9:43 and you can even set up authentication inside of your Soap service
9:47 and I'll show you that in the concept section.