paulb@305 | 1 | #!/usr/bin/env python |
paulb@305 | 2 | |
paulb@305 | 3 | import libxml2dom.xmlrpc |
paulb@305 | 4 | |
paulb@308 | 5 | # Some examples from the specification. |
paulb@308 | 6 | |
paulb@305 | 7 | request = """<?xml version="1.0"?> |
paulb@305 | 8 | <methodCall> |
paulb@305 | 9 | <methodName>examples.getStateName</methodName> |
paulb@305 | 10 | <params> |
paulb@305 | 11 | <param> |
paulb@305 | 12 | <value><i4>41</i4></value> |
paulb@305 | 13 | </param> |
paulb@305 | 14 | </params> |
paulb@305 | 15 | </methodCall>""" |
paulb@305 | 16 | |
paulb@305 | 17 | req = libxml2dom.xmlrpc.parseString(request) |
paulb@311 | 18 | assert req.method.methodName == "examples.getStateName" |
paulb@311 | 19 | assert req.method.parameterValues == [("i4", "41")] |
paulb@308 | 20 | assert req.fault is None |
paulb@311 | 21 | print "Method name:", req.method.methodName |
paulb@311 | 22 | print "Parameter values:", req.method.parameterValues |
paulb@308 | 23 | print "Fault:", req.fault |
paulb@305 | 24 | |
paulb@305 | 25 | response = """<?xml version="1.0"?> |
paulb@305 | 26 | <methodResponse> |
paulb@305 | 27 | <params> |
paulb@305 | 28 | <param> |
paulb@305 | 29 | <value><string>South Dakota</string></value> |
paulb@305 | 30 | </param> |
paulb@305 | 31 | </params> |
paulb@305 | 32 | </methodResponse>""" |
paulb@305 | 33 | |
paulb@305 | 34 | resp = libxml2dom.xmlrpc.parseString(response) |
paulb@311 | 35 | assert resp.method.methodName is None |
paulb@311 | 36 | assert resp.method.parameterValues == [("string", "South Dakota")] |
paulb@308 | 37 | assert resp.fault is None |
paulb@311 | 38 | print "Method name:", resp.method.methodName |
paulb@311 | 39 | print "Parameter values:", resp.method.parameterValues |
paulb@308 | 40 | print "Fault:", resp.fault |
paulb@305 | 41 | |
paulb@305 | 42 | failed = """<?xml version="1.0"?> |
paulb@305 | 43 | <methodResponse> |
paulb@305 | 44 | <fault> |
paulb@305 | 45 | <value> |
paulb@305 | 46 | <struct> |
paulb@305 | 47 | <member> |
paulb@305 | 48 | <name>faultCode</name> |
paulb@305 | 49 | <value><int>4</int></value> |
paulb@305 | 50 | </member> |
paulb@305 | 51 | <member> |
paulb@305 | 52 | <name>faultString</name> |
paulb@305 | 53 | <value><string>Too many parameters.</string></value> |
paulb@305 | 54 | </member> |
paulb@305 | 55 | </struct> |
paulb@305 | 56 | </value> |
paulb@305 | 57 | </fault> |
paulb@305 | 58 | </methodResponse>""" |
paulb@305 | 59 | |
paulb@305 | 60 | f = libxml2dom.xmlrpc.parseString(failed) |
paulb@311 | 61 | assert f.method.methodName is None |
paulb@311 | 62 | assert f.method.parameterValues == [] |
paulb@308 | 63 | assert f.fault.code == "4" |
paulb@308 | 64 | assert f.fault.reason == "Too many parameters." |
paulb@311 | 65 | print "Method name:", f.method.methodName |
paulb@311 | 66 | print "Parameter values:", f.method.parameterValues |
paulb@308 | 67 | print "Fault code:", f.fault.code |
paulb@308 | 68 | |
paulb@308 | 69 | # Python Package Index examples. |
paulb@308 | 70 | |
paulb@308 | 71 | search = """<?xml version="1.0"?> |
paulb@308 | 72 | <methodCall> |
paulb@308 | 73 | <methodName>search</methodName> |
paulb@308 | 74 | <params> |
paulb@308 | 75 | <param> |
paulb@308 | 76 | <value> |
paulb@308 | 77 | <struct> |
paulb@308 | 78 | <member> |
paulb@308 | 79 | <name>name</name> |
paulb@308 | 80 | <value><string>libxml2dom</string></value> |
paulb@308 | 81 | </member> |
paulb@308 | 82 | <member> |
paulb@308 | 83 | <name>description</name> |
paulb@308 | 84 | <value>XML</value> |
paulb@308 | 85 | </member> |
paulb@308 | 86 | </struct> |
paulb@308 | 87 | </value> |
paulb@308 | 88 | </param> |
paulb@308 | 89 | <param> |
paulb@308 | 90 | <value> |
paulb@308 | 91 | <string>and</string> |
paulb@308 | 92 | </value> |
paulb@308 | 93 | </param> |
paulb@308 | 94 | </params> |
paulb@308 | 95 | </methodCall>""" |
paulb@308 | 96 | |
paulb@308 | 97 | s = libxml2dom.xmlrpc.parseString(search) |
paulb@311 | 98 | assert s.method.methodName == "search" |
paulb@311 | 99 | assert s.method.parameterValues == [("struct", [("name", ("string", "libxml2dom")), ("description", ("string", "XML"))]), ("string", "and")] |
paulb@308 | 100 | assert s.fault is None |
paulb@311 | 101 | print "Method name:", s.method.methodName |
paulb@311 | 102 | print "Parameter values:", s.method.parameterValues |
paulb@308 | 103 | print "Fault:", s.fault |
paulb@305 | 104 | |
paulb@305 | 105 | # vim: tabstop=4 expandtab shiftwidth=4 |