1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
|

# Overview
Suds is a lightweight SOAP-based web service client for Python
licensed under LGPL (see the `LICENSE.txt` file included in the
distribution).
Although the original `suds` package stopped releasing versions after `0.4`,
many (but not all) other open source projects moved to a maintained fork known
as "suds-jurko". This is a community fork of that fork that is releasing
packages under the main `suds` package name (and `suds-community` for
consistency until version 2.x of this package).
**Forked project information**
- Project site - <https://github.com/suds-community/suds>
- Official releases can be downloaded from:
- Github - <https://github.com/suds-community/suds>
- PyPI - <http://pypi.python.org/pypi/suds> and <http://pypi.python.org/pypi/suds-community>
**Original suds Python library development project information**
For development notes see the `HACKING.rst` document included in the
distribution.
# Installation
Standard Python installation.
Here are the basic instructions for 3 different installation methods:
1. Using `pip`
- Have the `pip` package installed.
- Run `pip install suds`.
2. Using `easy-install`
- Have the `setuptools` package installed.
- Run `easy_install suds`.
3. From sources
- Unpack the source package somewhere.
- Run `python setup.py install` from the source distribution\'s
top level folder.
## Installation troubleshooting
- Released prior to `0.7` have many known installation issues
requiring the target Python environment to be manually prepared when
using some ancient Python versions, e.g. 2.4, 2.5 or 3.1.
- Releases `0.4.1. jurko 5 < x <= 0.6` may not be installed using
`pip` into a Python environment with an already installed
`setuptools` package older than the version expected by our project.
Displayed error message includes instructions on how to manually
upgrade the installed `setuptools` package before rerunning our
installation.
- `pip` internally imports existing `setuptools` packages before
running our setup, thus preventing us from upgrading the
existing `setuptools` installation inplace.
- If automated `setuptools` Python package installation fails (used in
releases `0.4.1 jurko 5` and later), e.g. due to PyPI web site not
being available, user might need to install it manually and then
rerun the installation.
- Releases prior to `0.4.1. jurko 5` will fail if the `distribute`
Python package is not already installed on the system.
- Python 2.4.3 on Windows has problems using automated `setuptools`
Python package downloads via the HTTPS protocol, and therefore does
not work correctly with PyPI which uses HTTPS links to all of its
packages. The same does not occur when using Python version 2.4.4.
# Features
Basic features:
- No class generation
- Provides an object-like API.
- Reads wsdl at runtime for encoding/decoding
- Provides for the following SOAP (style) binding/encoding:
- Document/Literal
- RPC/Literal
- RPC/Encoded (section 5)
The goal of suds is to present an RPC-like interface into
soap-based web services. This means that in most cases, users do not
need to be concerned with the complexities of the WSDL and referenced
schemas. Regardless of which soap message style is specified, the
signature of the service methods remain the same. Uses that do examine
the WSDL will notice that even with the document soap message
style, the signature of each method resembles an RPC. The method
signature contains the contents of the document defined for the
message instead of the document itself.
The primary interface into the library is the
`Client` object. It provides methods for configuring the library and (2)
sub-namespaces defined below. When the
`Client` is created, it processes the wsdl and referenced schema(s).
From this information, it derives a representation of this information
which is used to provide the user with a service description and
for message/reply processing.
## Python Support
See `.github/workflows/test_and_release.yml` for supported Python versions. The goal is to support
[currently maintained versions of Python](https://devguide.python.org/#status-of-python-branches).
## Logging
The suds package use the Python standard lib logging package:
all messages are at level DEBUG or ERROR.
To register a console handler you can use basicConfig:
```py
import logging
logging.basicConfig(level=logging.INFO)
```
Once the console handler is configured, the user can enable module
specific debugging doing the following: logging.getLogger(\<desired
package\>).setLevel(logging.\<desired-level\>) A common example (show
sent/received soap messages):
```py
logging.getLogger('suds.client').setLevel(logging.DEBUG)
```
Suggested modules for debugging:
* suds.client:: Set the logging level to DEBUG on this module to see soap messages (in & out) and http headers.
* suds.transport:: Set the logging level to DEBUG on this module to see more details about soap messages (in& out) and http headers.
* suds.xsd.schema:: Set the logging level to DEBUG on this module to see digestion of the schema(s).
* suds.wsdl:: Set the logging level to `DEBUG` on this module to see digestion WSDL.
## Basic Usage
Version: API\^3\^
The `suds`
`Client` class provides a consolidated API for consuming web services.
The object contains (2) sub-namespaces:
__service__:: The
`service` namespace provides a proxy for the consumed service. This
object is used to invoke operations (methods) provided by the service
endpoint.
__factory__:: The
`factory` namespace provides a factory that may be used to create
instances of objects and types defined in the WSDL.
You will need to know the url for WSDL for each service used. Simply
create a client for that service as follows:
```py
from suds.client import Client
url = 'http://localhost:7080/webservices/WebServiceTestBean?wsdl'
client = Client(url)
```
You can inspect service object with: `__str()__` as follows to get a
list of methods provide by the service:
```py
print client
Suds - version: 0.3.3 build: (beta) R397-20081121
Service (WebServiceTestBeanService) tns="http://test.server.enterprise.rhq.org/"
Prefixes (1):
ns0 = "http://test.server.enterprise.rhq.org/"
Ports (1):
(Soap)
Methods:
addPerson(Person person, )
echo(xs:string arg0, )
getList(xs:string str, xs:int length, )
getPercentBodyFat(xs:string name, xs:int height, xs:int weight)
getPersonByName(Name name, )
hello()
testExceptions()
testListArg(xs:string[] list, )
testVoid()
updatePerson(AnotherPerson person, name name, )
Types (23):
Person
Name
Phone
AnotherPerson
```
note: See example of service with multiple ports below.
The sample output lists that the service named
`WebServiceTestBeanService` has methods such as
getPercentBodyFat() and addPerson().
#### Simple Arguments
Let\'s start with the simple example. The getPercentBodyFat() method has
the signature of getPercentBodyFat(`xs:string` name,
`xs:int` height, `xs:int` weight). In this case, the
parameters are `simple` types. That is, they not objects. This
method would be invoked as follows:
```py
result = client.service.getPercentBodyFat('jeff', 68, 170)
print result
You have 21% body fat.
```
```py
result = client.service.getPercentBodyFat(name='jeff', height=68, weight=170)
print result
You have 21% body fat.
```
```py
d = dict(name='jeff', height=68, weight=170)
result = client.service.getPercentBodyFat(**d)
print result
You have 21% body fat.
```
#### Complex Arguments
The addPerson() method takes a `person` argument of type:
`Person` and has a signature of: addPerson(`Person` person,
) where parameter type is printed followed by it\'s name. There is a
type (or class) named \'person\' which is coincidentally the same name
as the argument. Or in the case of getPercentBodyFat() the parameters
are __string__ of type xs:string and __integer__ of type xs:int.
So, to create a `Person` object to pass as an argument we need to
get a person argument using the `factory` sub-namespace as
follows:
```py
person = client.factory.create('Person')
print person
(Person)=
{
phone = []
age = NONE
name(Name) =
{
last = NONE
first = NONE
}
}
```
As you can see, the object is created as defined by the WSDL. The list
of phone number is empty so we\'ll have to create a `Phone`
object:
```py
phone = client.factory.create('Phone')
phone.npa = 202
phone.nxx = 555
phone.number = 1212
```
\... and the name (Name object) and age need to be set and we need to
create a name object first:
```py
name = client.factory.create('Name')
name.first = 'Elmer'
name.last = 'Fudd'
```
Now, let\'s set the properties of our `Person` object
```py
person.name = name
person.age = 35
person.phone = [phone]
```
or
```py
person.phone.append(phone)
\... and invoke our method named addPerson() as follows:
```py
try:
person_added = client.service.addPerson(person)
except WebFault as e:
print e
```
It\'s that easy.
The ability to use python `dict` to represent complex objects was
re-introduced in 0.3.8. However, this is not the preferred
method because it may lead to passing incomplete objects. Also, this
approach has a significant limitation. Users may __not__ use python
`dict` for complex objects when they are subclasses (or
extensions) of types defined in the wsdl/schema. In other words, if the
schema defines a type to be an `Animal` and you wish to pass a
`Dog` (assumes Dog `isa` Animal), you may __not__ use a
`dict` to represent the dog. In this case, suds needs to set the
xsi:type=\"Dog\" but cannot because the python `dict` does not
provide enough information to indicate that it is a `Dog` not an
`Animal`. Most likely, the server will reject the request and
indicate that it cannot instantiate a abstract `Animal`.
#### Complex Arguments Using Python (dict)
Note: version 0.3.8+
Just like the factory example, let\'s assume the addPerson() method
takes a `person` argument of type: `Person`. So, to create a
`Person` object to pass as an argument we need to get a person
object and we can do so by creating a simple python `dict`.
```py
person = {}
```
According to the WSDL we know that the Person contains a list of Phone
objects so we\'ll need `dict`s for them as well.
```py
phone = {
'npa':202,
'nxx':555,
'number':1212,
}
```
\... and the name (Name object) and age need to be set and we need to
create a name object first:
```py
name = {
'first':'Elmer',
'last':'Fudd'
}
```
Now, let\'s set the properties of our `Person` object
```py
person['name'] = name
person['age'] = 35
person['phone'] = [phone,]
```
\... and invoke our method named addPerson() as follows:
```py
try:
person_added = client.service.addPerson(person)
except WebFault as e:
print e
```
### Faults
The Client can be configured to throw web faults as `WebFault` or to
return a tuple (\<status\>, \<returned-value\>) instead as follows:
```py
client = client(url, faults=False)
result = client.service.addPerson(person)
print result
( 200, person ...)
```
### Options
The `suds`
`client` has many that may be used to control the behavior of the
library. Some are
`general options` and others are
`transport options`. Although, the options objects are exposed, the
preferred and supported way to set/unset options is through:
- The `Client` constructor
- The `Client`.set\_options()
- The `Transport` constructor(s).
General options are as follows:
* faults:: Controls web fault behavior.
* service:: Controls the default service name for multi-service wsdls.
* port:: Controls the default service port for multi-port services.
* location:: This overrides the service port address URL defined in the WSDL.
* transport:: Controls the plugin web `transport`.
* cache:: Provides caching of documents and objects related to loading the WSDL. Soap envelopes are never cached.
* cachingpolicy:: The caching policy, determines how data is cached. The default is 0. version 0.4+
- 0 = XML documents such as WSDL & XSD.
- 1 = WSDL object graph.
* soapheaders:: Provides for soap headers.
* wsse:: Provides for WS-Security object. \
* __inject`:: Controls message/reply message injection.
* doctor:: The schema `doctor` specifies an object used to fix broken schema(s).
* xstq:: The XML schema type qualified flag indicates that `xsi:type` attribute __values__ should be qualified by namespace.
* prefixes:: Elements of the soap message should be qualified (when needed) using XML prefixes as opposed to xmlns=\"\" syntax.
* retxml:: Flag that causes the I{raw} soap envelope to be returned instead of the python object graph.
* autoblend:: Flag that ensures that the schema(s) defined
within the WSDL import each other.
* nosend:: Flag that causes suds to generate the soap envelope but not send it. Instead, a `RequestContext` is returned Default: False.
* sortnamespaces:: Flag that causes suds to sort namespaces alphabetically on storing them. Default: True.
* allowUnknownMessageParts:: Raise exceptions when unknown message parts are detected when receiving a web service reply, compared to the operation's WSDL schema definition. Default: False.
**Transport options passed to the `Client` contructor are only used if the default transport is used**, they are as follows:
* proxy:: Controls http proxy settings.
* timeout:: The URL connection timeout (seconds) default=90. A timeout can also be set per method invocation.
* headers:: Provides for `extra` http headers.
* username:: username for HTTP authentication.
* password:: password for HTTP authentication.
### Custom behavior through swapping components
Various ways that suds behaves can be customized by swapping out custom
#### Initializing optional arrays with lists
In some unreleased versions of suds-jurko, all children elements were populated with empty lists. This was fixed in suds-community as a regression. This can be desired behavior as it simplifies constructing complex requests. To obtain the prior behavior, use a custom `Builder` class, for example:
```py
from suds.client import Client, Builder
class AlwaysInitializeBuilder(Builder):
def skip_value(self, type):
return False # always set value
client = Client(url)
client.factory.builder = AlwaysInitializeBuilder(client.factory.builder.resolver)
```
## Enumerations
Enumerations are handled as follows:
Let\'s say the wsdl defines the following enumeration:
```xml
<xs:simpleType name="resourceCategory">
<xs:restriction base="xs:string">
<xs:enumeration value="PLATFORM"/>
<xs:enumeration value="SERVER"/>
<xs:enumeration value="SERVICE"/>
</xs:restriction>
</xs:simpleType>
```
The client can instantiate the enumeration so it can be used. Misspelled
references to elements of the `enum` will raise a `AttrError`
exception as:
```py
resourceCategory = client.factory.create('resourceCategory')
client.service.getResourceByCategory(resourceCategory.PLATFORM)
```
## Factory
The
`factory` is used to create complex objects defined the the wsdl/schema.
This is __not__ necessary for parameters or types that are specified
as `simple` types such as xs:string, xs:int, etc \...
The create() method should always be used becuase it returns objects
that already have the proper structure and schema-type information.
Since xsd supports nested type definition, so does create() using the
(.) dot notation. For example suppose the (Name) type was not defined as
a top level \"named\" type but rather defined within the (Person) type.
In this case creating a (Name) object would have to be quanified by
it\'s parent\'s name using the dot notation as follows:
```py
name = client.factory.create('Person.Name')
```
If the type is in the same namespace as the wsdl (targetNamespace) then
it may be referenced without any namespace qualification. If not, the
type must be qualifed by either a namespace prefix such as:
```py
name = client.factory.create('ns0:Person')
```
Or, the name can be fully qualified by the namespace itself using the
full qualification syntax as (as of 0.2.6):
```py
name = client.factory.create('{http://test.server.enterprise.rhq.org/}person')
```
Qualified names can only be used for the first part of the
name, when using (.) dot notation to specify a path.
## Services With Multiple Ports
Some services are defined with multiple ports as:
```xml
<wsdl:service name="BLZService">
<wsdl:port name="soap" binding="tns:BLZServiceSOAP11Binding">
<soap:address location="http://www.thomas-bayer.com:80/axis2/services/BLZService"/>
</wsdl:port>
<wsdl:port name="soap12" binding="tns:BLZServiceSOAP12Binding">
<soap12:address location="http://www.thomas-bayer.com:80/axis2/services/BLZService"/>
</wsdl:service>
```
And are reported by suds as:
```py
url = 'http://www.thomas-bayer.com/axis2/services/BLZService?wsdl'
client = Client(url)
print client
Suds - version: 0.3.3 build: (beta) R397-20081121
Service (BLZService) tns="http://thomas-bayer.com/blz/"
Prefixes (1)
ns0 = "http://thomas-bayer.com/blz/"
Ports (2):
(soap)
Methods (1):
getBank(xs:string blz, )
(soap12)
Methods (1):
getBank(xs:string blz, )
Types (5):
getBankType
getBankResponseType
getBankType
getBankResponseType
detailsType
```
This example only has (1) method defined for each port but it could very
likely have may methods defined. Suds does not require the method
invocation to be qualifed (as shown above) by the port as:
```py
client.service.<port>.getBank()
```
unless the user wants to specify a particular port. In most cases, the
server will work properly with any of the soap ports. However, if you
want to invoke the getBank() method on this service the user may qualify
the method name with the port.
There are (2) ways to do this:
- Select a default port using the `port`
` option` before invoking the method as:
<!-- -->
```py
client.set_options(port='soap')
client.service.getBank()
```
- fully qualify the method as:
<!-- -->
```py
client.service.soap.getBank()
```
**After r551 version 0.3.7, this changes some to support
multiple-services within (1) WSDL as follows:**
This example only has (1) method defined for each port but it could very
likely have may methods defined. Suds does not require the method
invocation to be qualifed (as shown above) by the port as:
```py
client.service[port].getBank()
```
unless the user wants to specify a particular port. In most cases, the
server will work properly with any of the soap ports. However, if you
want to invoke the getBank() method on this service the user may qualify
the method name with the port. The `port` may be subscripted
either by name (string) or index(int).
There are many ways to do this:
- Select a default port using the `port`
` option` before invoking the method as:
<!-- -->
```py
client.set_options(port='soap')
client.service.getBank()
```
- fully qualify the method using the port `name` as:
<!-- -->
```py
client.service['soap'].getBank()
```
- fully qualify the method using the port `index` as:
<!-- -->
```py
client.service[0].getBank()
```
## WSDL With Multiple Services & Multiple Ports
version: 0.3.7+
Some WSDLs define multiple services which may (or may not) be defined
with multiple ports as:
```xml
<wsdl:service name="BLZService">
<wsdl:port name="soap" binding="tns:BLZServiceSOAP11Binding">
<soap:address location="http://www.thomas-bayer.com:80/axis2/services/BLZService"/>
</wsdl:port>
<wsdl:port name="soap12" binding="tns:BLZServiceSOAP12Binding">
<soap12:address location="http://www.thomas-bayer.com:80/axis2/services/BLZService"/>
</wsdl:service>
<wsdl:service name="OtherBLZService">
<wsdl:port name="soap" binding="tns:OtherBLZServiceSOAP11Binding">
<soap:address location="http://www.thomas-bayer.com:80/axis2/services/OtherBLZService"/>
</wsdl:port>
<wsdl:port name="soap12" binding="tns:OtherBLZServiceSOAP12Binding">
<soap12:address location="http://www.thomas-bayer.com:80/axis2/services/OtherBLZService"/>
</wsdl:service>
```
And are reported by suds as:
```py
url = 'http://www.thomas-bayer.com/axis2/services/BLZService?wsdl'
client = Client(url)
print client
Suds - version: 0.3.7 build: (beta) R550-20090820
Service (BLZService) tns="http://thomas-bayer.com/blz/"
Prefixes (1)
ns0 = "http://thomas-bayer.com/blz/"
Ports (2):
(soap)
Methods (1):
getBank(xs:string blz, )
(soap12)
Methods (1):
getBank(xs:string blz, )
Types (5):
getBankType
getBankResponseType
getBankType
getBankResponseType
detailsType
Service (OtherBLZService) tns="http://thomas-bayer.com/blz/"
Prefixes (1)
ns0 = "http://thomas-bayer.com/blz/"
Ports (2):
(soap)
Methods (1):
getBank(xs:string blz, )
(soap12)
Methods (1):
getBank(xs:string blz, )
Types (5):
getBankType
getBankResponseType
getBankType
getBankResponseType
detailsType
```
This example only has (1) method defined for each port but it could very
likely have may methods defined. Suds does __not__ require the
method invocation to be qualifed (as shown above) by the service and/or
port as:
```py
client.service[service][port].getBank()
```
unless the user wants to specify a particular service and/or port. In
most cases, the server will work properly with any of the soap ports.
However, if you want to invoke the getBank() method on the
`OtherBLZService` service the user may qualify the method name
with the service and/or port. If not specified, suds will default the
service to the 1st server defined in the WSDL and default to the 1st
port within each service. Also, when a WSDL defines (1) services, the
\[` subscript is applied to the port selection. This may be a little
confusing because the syntax for subscripting can seem inconsistent.
Both the `service` __and__ `port` may be subscripted
either by name (string) or index (int).
There are many ways to do this:
- Select a default service using the `service` option and
default port using `port` option
` option` before invoking the method as:
<!-- -->
```py
client.set_options(service='OtherBLZService', port='soap')
client.service.getBank()
```
- method qualified by `service` and `port` as:
<!-- -->
```py
client.service['OtherBLZService']['soap'].getBank()
```
- method qualified by `service` and `port` using indexes
as:
<!-- -->
```py
client.service[1][0].getBank()
```
- method qualified by `service` (by name) only as:
<!-- -->
```py
client.service['OtherBLZService'].getBank()
```
- method qualified by `service` (by index) only as:
<!-- -->
```py
client.service[1].getBank()
```
Note, that if a WSDL defines more then one service, you __must__
qualify the `service` via
`option` or by using the subscripting syntax in order to specify the
`port` using the subscript syntax.
## SOAP Headers
SOAP headers may be passed during the service invocation by using the
`soapheaders` `option` as follows:
```py
client = client(url)
token = client.factory.create('AuthToken')
token.username = 'Elvis'
token.password = 'TheKing'
client.set_options(soapheaders=token)
result = client.service.addPerson(person)
```
OR
```py
client = client(url)
userid = client.factory.create('Auth.UserID')
userid.set('Elvis')
password = client.factory.create('Auth.Password')
password.set('TheKing')
client.set_options(soapheaders=(userid,password))
result = client.service.addPerson(person)
```
OR
```py
client = client(url)
userid = 'Elmer'
passwd = 'Fudd'
client.set_options(soapheaders=(userid,password))
result = client.service.addPerson(person)
```
The `soapheaders` option may also be assigned a dictionary for
those cases when optional headers are specified and users don\'t want to
pass None place holders. This works much like the method parameters. Eg:
```py
client = client(url)
myheaders = dict(userid='Elmer', passwd='Fudd')
client.set_options(soapheaders=myheaders)
result = client.service.addPerson(person)
```
Passing `soapheaders` by keyword (dict) is available in 0.3.4
(r442) and later.
## Custom SOAP Headers
Custom SOAP headers may be passed during the service invocation by using
the `soapheaders`
`option`. A `custom` soap header is defined as a header that is
required by the service by __not__ defined in the wsdl. Thus, the
`easy` method of passing soap headers already described cannot be
used. This is done by constructing and passing an
`Element` or collection of
`Elements` as follows:
```py
from suds.sax.element import Element
client = client(url)
ssnns = ('ssn', 'http://namespaces/sessionid')
ssn = Element('SessionID', ns=ssnns).setText('123')
client.set_options(soapheaders=ssn)
result = client.service.addPerson(person)
```
Do __not__ try to pass the header as an XML `string` such as:
```py
client = client(url)
ssn = '<ssn:SessionID>123</ssn:SessionID>'
client.set_options(soapheaders=ssn)
result = client.service.addPerson(person)
```
It will not work because: 1. Only
`Elements` are processed as `custom` headers. 1. The XML string
would be escaped as <ssn:SessionID>123</ssn:SessionID>
anyway.
\*Notes: 1. Passing single
`Elements` as soap headers fixed in Ticket \#232 (r533) and will be
released on 0.3.7. 1. Reusing this
`Element` in subsequent calls fixed in Ticket \#233 (r533) and will be
released on 0.3.7.
## WS-SECURITY
As of r452 / 0.3.4 (beta) to provide basic ws-security with
`UsernameToken` with `clear-text` password (no digest).
```py
from suds.wsse import *
security = Security()
token = UsernameToken('myusername', 'mypassword')
security.tokens.append(token)
client.set_options(wsse=security)
```
or, if the `Nonce` and `Create` elements are needed, they
can be generated and set as follows:
```py
from suds.wsse import *
security = Security()
token = UsernameToken('myusername', 'mypassword')
token.setnonce()
token.setcreated()
token.setnonceencoding(True)
token.setpassworddigest('digest')
security.tokens.append(token)
client.set_options(wsse=security)
```
but, if you want to manually set the `Nonce` and/or
`Created`, you may do as follows:
```py
from suds.wsse import *
security = Security()
token = UsernameToken('myusername', 'mypassword')
token.setnonce('MyNonceString...')
token.setcreated(datetime.now())
security.tokens.append(token)
client.set_options(wsse=security)
```
## Multi-document (Document/Literal)
In most cases, services defined using the document/literal SOAP binding
style will define a single document as the message payload. The
\<message/\> will only have (1) \<part/\> which references an
\<element/\> in the schema. In this case, suds presents a RPC view of
that method by displaying the method signature as the contents (nodes)
of the document. Eg:
```xml
<schema>
...
<xs:element name="Foo" type = "tns:Foo"/>
<xs:complexType name="Foo">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
</xs:sequence>
</xs:complexType>
...
</schema>
<definitions>
...
<message name="FooMessage">
<part name="parameters" element="Foo">
</message>
...
</definitions>
```
Suds will report the method `foo` signature as:
foo(xs:string name, xs:int age,)
This provides an RPC feel to the document/literal soap binding style.
Now, if the wsdl defines:
```xml
<schema>
...
<xs:element name="Foo" type = "tns:Foo"/>
<xs:element name="Bar" type = "xs:string"/>
<xs:complexType name="Foo">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
</xs:sequence>
</xs:complexType>
...
</schema>
<definitions>
...
<message name="FooMessage">
<part name="foo" element="Foo">
<part name="bar" element="Bar">
</message>
...
</definitions>
```
Suds will be forced to report the method `foo` signature as:
foo(Foo foo, xs:int bar)
The message has (2) parts which defines that the message payload
contains (2) documents. In this case, suds must present a /Document/
view of the method.
## HTTP Authentication
#### Basic
As of version 0.3.3 and newer, `basic` HTTP authentication
as defined by [RFC-2617](http://www.ietf.org/rfc/rfc2617.txt) can be
done as follows:
```py
client = Client(url, username='elmer', password='fudd')
```
Authentication is provided by the (default)
`HttpAuthenticated` `Transport` class defined in the
`transport.https` module that follows the challenge (http 401) /
response model defined in the RFC.
As of r537, `0.3.7` beta, a new `Transport` was added in the
`transport.http` module that provides http authentication for servers
that don\'t follow the challenge/response model. Rather, it sets the
`Authentication:` http header on __all__ http requests. This
transport can be used as follows:
```py
from suds.transport.http import HttpAuthenticated
t = HttpAuthenticated(username='elmer', password='fudd')
client = Client(url, transport=t)
```
Or
```py
from suds.transport.http import HttpAuthenticated
t = HttpAuthenticated()
client = Client(url, transport=t, username='elmer', password='fudd')
```
For version: 0.3.3 and older ONLY:
Revision 63+ (and release 0.1.8+) includes the migration from httplib to
urllib2 in the suds default
`transport` which enables users to leverage all of the authentication
features provided by urllib2. For example basic HTTP authentication
could be implemented as follows:
```py
myurl = 'http://localhost:7080/webservices/WebServiceTestBean?wsdl'
client = Client(myurl)
import urllib2
baseurl = 'http://localhost:7080/'
username = 'myuser'
password = 'mypassword'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, baseurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
client.options.transport.urlopener = urllib2.build_opener(authhandler)
```
The suds default
`HTTP transport` uses urllib2.urlopen(), basic http authentication is
handled automatically if you create the transport\'s urlopener correctly
and set the urlopener.
#### Windows (NTLM)
As of 0.3.8, suds includes a
`NTLM transport` based on urllib2. This implementation requires
`users to install the [python-ntlm](http://code.google.com/p/python-ntlm/). It is __not__ packaged with suds.
To use this, simply do something like:
```py
from suds.transport.https import WindowsHttpAuthenticated
ntlm = WindowsHttpAuthenticated(username='xx', password='xx')
client = Client(url, transport=ntlm)
```
## Proxies
The suds default
`transport` handles proxies using urllib2.Request.set\_proxy(). The
proxy options can be passed set using Client.set\_options. The proxy
options must contain a dictionary where keys=protocols and values are
the hostname (or IP) and port of the proxy.
```py
...
d = dict(http='host:80', https='host:443', ...)
client.set_options(proxy=d)
...
```
## Message Injection (Diagnostics/Testing)
The service API provides for message/reply injection.
To inject either a soap message to be sent or to inject a reply or fault
to be processed as if returned by the soap server, simply specify the
`__inject` keyword argument with a value of a dictionary
containing either:
- msg = \<message string\>
- reply = \<reply string\>
- fault = \<fault string\>
when invoking the service. Eg:
Sending a raw soap message:
```py
message = \
"""<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
print client.service.test(__inject={'msg':message})
```
Injecting a response for testing:
```py
reply = \
"""<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
print client.service.test(__inject={'reply':reply})
```
## SSL certificate verification & Custom Certificates
With Python 2.7.9, SSL/TLS verification is turned on by default.
This can be a problem when suds is used against an endpoint which has a self-signed certificate, which is quite common in the corporate intranet world.
One approach to turn off certificate validation in suds is to use a custom transport class. For example in Python 3:
```py
import urllib.request
import ssl
import suds.transport.http
class UnverifiedHttpsTransport(suds.transport.http.HttpTransport):
def __init__(self, *args, **kwargs):
super(UnverifiedHttpsTransport, self).__init__(*args, **kwargs)
def u2handlers(self):
handlers = super(UnverifiedHttpsTransport, self).u2handlers()
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
handlers.append(urllib.request.HTTPSHandler(context=context))
return handlers
client = Client(url, transport=UnverifiedHttpsTransport())
```
In addition, if a custom set of certificates and/or root CA is needed, this can also be done via a custom transport class. For example, in Python 3:
```py
class ClientHttpsTransport(HttpTransport):
def __init__(self, certfile, keyfile, cafile, *args, **kwargs):
super(ClientHttpsTransport, self).__init__(*args, **kwargs)
self.certfile = certfile
self.keyfile = keyfile
self.cafile = cafile
def u2handlers(self):
handlers = super(ClientHttpsTransport, self).u2handlers()
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=self.cafile)
context.load_cert_chain(self.certfile, self.keyfile)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
handlers.append(urllib.request.HTTPSHandler(context=context))
return handlers
custom_https = ClientHttpsTransport('/path/to/certificate_file', '/path/to/key_file', '/path/to/ca_file')
client = Client(url, transport=custom_https),
```
## Timeouts
Per request timeouts can be set by using a `__timeout` keyword argument in
each call. This supersedes the global client default. For example, the
following call will have a timeout of 10 seconds:
```python
client = Client(url, timeout=30)
client.service.test(__timeout=10)
```
## Performance
As of 0.3.5 r473, suds provides some URL caching. By default, http
get(s) such as getting the WSDL and importing XSDs are cached. The
caching applies to URL such as those used to get the referenced WSDLs
and XSD schemas but does __not__ apply to service method invocation
as this would not make sense.
In 0.3.9, `FileCache` was replaced with `ObjectCache`.
The default cache is a
`ObjectCache` with an expiration of (1) day.
This duration may be adjusted as follows:
```py
import datetime
...
cache = client.options.cache
cache.duration = datetime.timedelta(days=10)
```
The duration must be a `datetime.timedelta`.
The default location (directory) is /tmp/suds so
Windows users will need to set the location to something
that makes sense on windows.
The cache is an
`option` and can be set with any kind of
`Cache` object or may be disabled by setting the option to None.
So, uses may plug-in any kind of cache they want.
```py
from suds.cache import Cache
class MyCache(Cache)
...
client.set_options(cache=MyCache())
```
To disable caching:
```py
client.set_options(cache=None)
```
## Fixing Broken Schema(s)
There are many cases where the schema(s) defined both within the WSDL or
imported are broken. The most common problem is failure to import the
follow proper import rules. That is, references are made in one schema
to named objects defined in another schema without importing it. The
`doctor` module defines a set of classes for mending broken
schema(s).
### Doctors
The
`Doctor` class provides the interface for classes that provide this
service. Once defined, the doctor can be specified using the
schema doctor as an
`option` when creating the Client. Or, you can use one of the stock
doctors
- ` ImportDoctor` - Used to fix import problems. For example:
<!-- -->
```py
imp = Import('http://schemas.xmlsoap.org/soap/encoding/')
imp.filter.add('http://some/namespace/A')
imp.filter.add('http://some/namespace/B')
doctor = ImportDoctor(imp)
client = Client(url, doctor=doctor)
```
In this example, we\'ve specified that the doctor should examine
schema(s) with a targetNamespace of
`http://some/namespace/A or http://some/namespace/B` and ensure that the
schema for the `http://schemas.xmlsoap.org/soap/encoding/` is imported.
If those schema(s) do not have an \<xs:import/\> for those namespaces,
it is added.
For cases where the schemaLocation is not bound to the
namespace, the
`Import` can be created specifying the location has follows:
```py
imp = Import('http://www.w3.org/2001/XMLSchema', location='http://www.w3.org/2001/XMLSchema.xsd')
imp.filter.add('http://some/namespace/A')
imp.filter.add('http://some/namespace/B')
doctor = ImportDoctor(imp)
client = Client(url, doctor=doctor)
```
A commonly referenced schema (that is not imported) is the SOAP section
5 encoding schema. This can now be fixed as follows:
```py
imp = Import('http://schemas.xmlsoap.org/soap/encoding/')
imp.filter.add('http://some/namespace/A')
doctor = ImportDoctor(imp)
client = Client(url, doctor=doctor)
```
note: Available in r512+ and 0.3.6 `beta`.
### Binding Schema Locations (URL) to Namespaces
Some WSDL(s) schemas import as: \<import
namespace=\"<http://schemas.xmlsoap.org/soap/encoding/%22/>\> without
schemaLocation=\"\" and expect processor to use the namespace URI as the
schema location for the namespace. The specifications for processing
\<import/\> leave the resolution of the imported namespace to a schema
to the descession of the processor (in this case suds) when
\@schemaLocation is not specified. Suds always looks within the WSDL for
a schema but does not look outside unless:
- A schemaLocation is specified, or
- A static binding is specified using the following syntax:
<!-- -->
```py
from suds.xsd.sxbasic import Import
ns = 'http://schemas.xmlsoap.org/soap/encoding/'
location = 'http://schemas.xmlsoap.org/soap/encoding/'
Import.bind(ns, location)
```
Or, the shorthand (when location is the same as the namespace URI)
```py
Import.bind(ns)
```
note: `http://schemas.xmlsoap.org/soap/encoding/'`
automatically `bound` in 0.3.4 as of (r420).
## Plugins
New in 0.4 is a plugin facility. It is intended to be a general, more
extensible, mechanism for users to inspect/modify suds while it is
running. Today, there are two `one-off` ways to do this:
1\. bindings.Binding.replyfilter - The reply text can be inspected &
modified. 2. xsd.Doctor - The doctor `option` used to mend broken
schemas.
The `plugin` module provides a number of classes but users really only need
to be concerned with a few:
- The `Plugin` class which defines the interface for user plugins
- The `Context` classes which are passed to the plugin.
The plugins are divided into (4) classes based on the `tasks` of
the soap client:
`Initialization` :: The client initialization task which is when
the client has digested the WSDL and associated XSD. `Document
Loading` :: The document loading task. This is when the client is
loading WSDL & XSD documents. `Messaging` :: The messaging task is
when the client is doing soap messaging as part of method (operation)
invocation.
#### InitPlugin
The `InitPlugin` currently has (1) hook:
`initialized()` :: Called after the client is initialized. The
context contains the `WSDL` object.
#### DocumentPlugin
The `DocumentPlugin` currently has (2) hooks::
`loaded()` :: Called before parsing a `WSDL` or `XSD`
document. The context contains the url & document text.
`parsed()` :: Called after parsing a `WSDL` or `XSD`
document. The context contains the url & document `root`.
#### MessagePlugin
The `MessagePlugin` currently has (5) hooks ::
* `marshalled()` :: Provides the plugin with the opportunity to inspect/modify the envelope Document __before__ it is sent.
* `sending()` :: Provides the plugin with the opportunity to inspect/modify the message text __before__ it is sent.
* `received()` :: Provides the plugin with the opportunity to inspect/modify the received XML text __before__ it is SAX parsed.
* `parsed()` :: Provides the plugin with the opportunity to inspect/modify the sax parsed DOM tree for the reply __before__ it is unmarshalled.
* `unmarshalled()` :: Provides the plugin with the opportunity to inspect/modify the unmarshalled reply __before__ it is returned to the caller.
General usage:
```py
from suds.plugin import *
class MyPlugin(DocumentPlugin):
...
plugin = MyPlugin()
client = Client(url, plugins=[plugin])
```
Plugins need to override __only__ those methods (hooks) of interest
- not all of them. Exceptions are caught and logged.
Here is an example. Say I want to add some attributes to the document
root element in the soap envelope. Currently suds does not provide a way
to do this using the main API. Using a plugin much like the schema
doctor, we can do this.
Say our envelope is being generated by suds as:
```xml
<soapenv:Envelope>
<soapenv:Body>
<ns0:foo>
<name>Elmer Fudd</name>
<age>55</age>
</ns0:foo>
</soapenv:Body>
</soapenv:Envelope>
```
But what you need is:
```xml
<soapenv:Envelope>
<soapenv:Body>
<ns0:foo id="1234" version="2.0">
<name>Elmer Fudd</name>
<age>55</age>
</ns0:foo>
</soapenv:Body>
</soapenv:Envelope>
```
```py
from suds.plugin import MessagePlugin
class MyPlugin(MessagePlugin):
def marshalled(self, context):
body = context.envelope.getChild('Body')
foo = body[0]
foo.set('id', '12345')
foo.set('version', '2.0')
client = Client(url, plugins=[MyPlugin()])
```
In the future, the `Binding.replyfilter` and `doctor`
__option__ will likely be deprecated. The
`ImportDoctor` has been extended to implement the
`Plugin`.onLoad() API.
In doing this, we can treat the `ImportDoctor` as a plugin:
```py
imp = Import('http://www.w3.org/2001/XMLSchema')
imp.filter.add('http://webservices.serviceU.com/')
d = ImportDoctor(imp)
client = Client(url, plugins=[d])
```
We can also replace our Binding.replyfilter() with a plugin as follows:
```py
def myfilter(reply):
return reply[1:]
Binding.replyfilter = myfilter
# replace with:
class Filter(MessagePlugin):
def received(self, context):
reply = context.reply
context.reply = reply[1:]
client = Client(url, plugins=[Filter()])
```
## Technical (FYI) Notes
- XML namespaces are represented as a tuple (prefix, URI). The default
namespace is (None,None).
- The suds.sax module was written becuase elementtree and other python
XML packages either: have a DOM API which is very unfriendly or: (in
the case of elementtree) do not deal with namespaces and especially
prefixes sufficiently.
- A qualified reference is a type that is referenced in the WSDL such
as \<tag type=\"tns:Person/\> where the qualified reference is a
tuple (\'Person\', (\'tns\',\'<http://myservce/namespace>\')) where
the namespace is the 2nd part of the tuple. When a prefix is not
supplied as in \<tag type=\"Person/\>, the namespace is the
targetNamespace for the defining fragment. This ensures that all
lookups and comparisons are fully qualified.
|