Consuming HTTP Services in Python Transcripts
Chapter: Calling SOAP services from Python
Lecture: Installing Suds for Python 3

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


Talk Python's Mastodon Michael Kennedy's Mastodon