Consuming HTTP Services in Python Transcripts
Chapter: The service landscape
Lecture: Service type: SOAP

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Another thing that we might run into, especially little bit older services, especially inside enterprises, inside these big companies,
0:10 they built a bunch of internet apps that they can connect together, it's very likely that what you are going to run into is a Soap web service.
0:19 Now, in the early days of the 2000s, 2001, 2002, Soap services were fairly ubiquitous for intranet communication.
0:29 And, of course, as Javascript grew and the internet grew in popularity especially with regard to services, the HTTP services and Json Restful services
0:40 more or less edged Soap out, but it's very likely that you are going to run into
0:43 a Soap service if you are working with older systems inside of a company. So it's really important that we talk about them here
0:51 because you don't want to have to switch technologies just because you are talking to a old service, right,
0:57 so what's the deal with these Soap services, how does that work? Well, it's basically HTTP traffic, the thing that is kind of an unusual is
1:03 every request, regardless of whether you are getting something or you are updating something,
1:08 every request is a post which indicates some kind of action, so here let's suppose we want to login and we are going to post to a single location
1:15 and this action header actually tells the service what to do, and what we are going to send is a big blob of XML. It looks like this.
1:22 And the service is going to respond with another big blob of XML, we are going to parse that apart back in our app. Now, what are the benefits?
1:33 One of the things that is nice about these Soap services is they typically have some kind of UI describing them,
1:39 and they also have UI that describes them for tooling, so they are really built with the expectation, it's basically required
1:46 that at least one side of these, the fence, either your app or the Soap service have some really strong tooling support to generate the stuff.
1:54 I go so far to say these are the easiest services to create and consume if,
1:59 and this is a huge if- if both sides of these arrows, your app and the service have excellent tooling support for Soap services.
2:10 that's not the case with Python, and these are certainly not the best services, that's why they are not very popular anymore,
2:18 but they do have a button press generate the server side, and client side tooling support,
2:22 and that way they make them easy but like I said, I don't think they are the best and I definitely don't recommend using them if you can avoid them.
2:29 So what else is a drawback about Soap services, we talked about post,
2:32 and this post only behavior means that it basically breaks the architecture of the internet,
2:37 the internet is built around get, post, put, various HTTP verbs, and only HTTP get is allowed to be cashed, and that makes proxies work better,
2:49 that makes local cashing work better, there is all sorts of scaleability benefits, and other semantics around being able to support HTTP get
2:57 the most common way to access resources on the internet but Soap services just entirely missed this, they just didn't even understand that HTTP verbs
3:07 meant a thing on the internet when they were created, so everything is post which means a lot of the infrastructure benefits
3:14 of being an HTTP service are lost. It requires tooling, so things like Javascript and Python
3:21 are the real disadvantage in working with these services, but that said,
3:24 we are going to cover some pretty cool stuff on working with them from Python if you must,
3:28 they are often focused on remote methods instead of remote resources, so here we have a log in method rather than accessing something about our user
3:37 and that can lead to poorly designed services. These Soap messages they exchange obviously they are bandwidth heavy
3:44 and while you technically can read them, if you must, they are not really friendly to human beings, they are friendly to computers,
3:51 and they can be read by humans, but they are not what I would call readable. Let's look at this Soap exchange, in detail.
3:59 So here I have an operation, imagine this is going to be called on a web service that is a Soap service, so I want to take a number,
4:06 and I want to take a number and I want to double it, I want to take an integer and double that integer, so how does this work?
4:12 Well, of course we want to send this as an HTTP post, so what we are going to do when we call this, the tooling that we would have had to use
4:19 to somehow generate this service, would convert this action and this data into what is called a Soap envelop.
4:26 So the Soap envelop is an XML namespace laden bit of XML that is quite dreadful and you should stay away from it if you can.
4:35 But like I said, if you have to call these services, being able to do it in Python is cool, so here you can see we have a Soap envelop,
4:41 and there is a Soap header and a Soap body, the Soap body contains some parameter, and that parameter's value you can see is 123.
4:50 So we are going to want to double the number 123. It's going to go off to the server, via post, via HTTP, some parts you don't see here,
4:58 the action is actually stored in the header, it's going to process that, and generate a response that comes back to us that is also a Soap envelope.
5:06 And this time the Soap body contains a double an integer response which also has an integer and that integer's value is 246.
5:15 So you can see, yes, awesome, we doubled the number and you can bet that the value returned from this function
5:23 very much returned 246 when we gave a 123. But it took almost a kilobyte of data exchange just send the four bytes over,
5:32 the 123, and return the four bytes back, 246. So, this is not very friendly to people, to the network,
5:40 there is lots of problems with it, but this is how Soap works.


Talk Python's Mastodon Michael Kennedy's Mastodon