A fork of https://github.com/Synerty/SOAPpy-py3 This is a working tree till fixes get imported upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

221 lines
8.0 KiB

  1. Simple Types HOWTO
  2. ==================
  3. The easiest way to understand use of data types is look at and run the examples
  4. already written (in tests/, validate/ and bid/) , and to write your own
  5. clients, looking at the xml as it is sent (by setting SOAP.Config.debug=1).
  6. As far as the built-in types are concerned, SOAP.py will preserve type
  7. as expected. That is: python integer will be of type integer, and
  8. equivalently for string and float. To access more than just these types,
  9. there are classes in SOAP.py. These allow invoking a certain type by making
  10. an instance of the corresponding class.
  11. The SOAPBuilder in SOAP.py will automatically convert python lists to Arrays
  12. and python dictionaries to Structs- these are two of the most frequently used
  13. data types.
  14. CLIENT EXAMPLES
  15. ---------------
  16. ## CODE
  17. import SOAP
  18. server = SOAP.SOAPProxy("http://localhost:8080/")
  19. print server.echo("Hello world")
  20. ## /CODE
  21. This example (taken from quickstart.txt) sends an ordered parameter of type
  22. string.
  23. ## CODE
  24. import SOAP
  25. import time
  26. #SOAP.Config.debug = 1
  27. test = time.gmtime (time.time ())
  28. server = SOAP.SOAPProxy("http://localhost:8080/")
  29. print server.echoDate (inputDate = SOAP.DateTime(test))
  30. ## /CODE
  31. This test calls echoDate with the named parameter inputDate, which is a
  32. TimeInstant. It prints the the result.
  33. **Note: The reason that it is a TimeInstant and not a DateTime
  34. is that SOAP.py uses the 1999 schema intead of the 2001 schema. To make it
  35. a DateTime, one would just use SOAP.dateTimeType() in place of SOAP.DateTime().
  36. **
  37. ## CODE
  38. import SOAP
  39. server = SOAP.SOAPProxy("http://localhost:8080/")
  40. test = [0, 1, -1, 3853]
  41. print server.echoIntegerArray (inputIntegerArray = test)
  42. ## /CODE
  43. This calls echoIntegerArray with the named parameter inputIntegerArray, which
  44. is a four-member array of type int. It prints the result.
  45. ## CODE
  46. import SOAP
  47. test = {'varFloat': 2.256, 'varInt': 474, 'varString': 'Utah'}
  48. server = SOAP.SOAPProxy("http://localhost:8080/")
  49. print server.echoStruct (inputStruct = test)
  50. ## /CODE
  51. This code calls the method echoStruct with the named parameter inputStruct,
  52. which is of type Struct. It then prints the result.
  53. ## CODE
  54. import SOAP
  55. item1 = SOAP.Struct( data = {"name":"widget","quantity":200,"price":SOAP.decimalType(45.99), "_typename":"LineItem"})
  56. items = SOAP.Array ( data = [item1] )
  57. items._ns = "http://www.soapinterop.org/Bid"
  58. server = SOAP.SOAPProxy("http://localhost:8080")
  59. server = server._sa ("http://www.soapinterop.org/Buy")
  60. server = server._ns ("http://www.soapinterop.org/Bid")
  61. po = SOAP.Struct( data = {"poID":"Order 1234", "createDate": SOAP.dateTimeType(), "items": items} )
  62. print server.Buy(PurchaseOrder = po)
  63. ## /CODE
  64. A few new things here.
  65. -First, we are creating an Array, 'items', with components of (made up) type
  66. 'LineItem'. (Notice the use of "_typename" to specify type).
  67. -This code associates a namespace with the Array, rather than use the default.
  68. -SOAP.dateTimeType() is called directly to get a dateTime instead of SOAP.py's
  69. default, 'timeInstant'.
  70. -Note that when creating a Struct or Array, the data must be passed in as a
  71. named 'data' param (as the first param, by order, is 'name').
  72. -The proxy is instantiated and then the values for its namespace (_ns) and
  73. soapaction (_sa) are assigned.
  74. -This call will work for a server expecting a parameter with the same
  75. components as those in the variable 'po' above. It will work whether the
  76. server has a named param 'PurchaseOrder' or has an unnamed param, but will
  77. not work if the server expects a named param with a name of anything but
  78. 'PurchaseOrder'.
  79. SERVER EXAMPLES
  80. ---------------
  81. ## CODE
  82. import SOAP
  83. def echo(s):
  84. return s + s # repeats a string twice
  85. server = SOAP.SOAPServer(("localhost", 8080))
  86. server.registerFunction(echo)
  87. server.serve_forever()
  88. ## /CODE
  89. This server example, from quickstart.txt, echoes (as type string) the
  90. string that is passed in, s.
  91. ## CODE
  92. import SOAP
  93. def echoDate (inputDate):
  94. return SOAP.DateTime(inputDate)
  95. server = SOAP.SOAPServer(("localhost", 8080))
  96. server.registerKWFunction(echoDate )
  97. server.serve_forever()
  98. ## /CODE
  99. This code accepts an inputDate and returns the same date, ensuring that it
  100. is of type TimeInstant by returning an instance of DateTime instead of
  101. simply returning the value.
  102. ## CODE
  103. import SOAP
  104. def echoIntegerArray (inputIntegerArray):
  105. if type(inputIntegerArray) != type([]) or len(inputIntegerArray) != 4:
  106. for elem in inputIntegerArray:
  107. if type(elem) != type(1):
  108. raise TypeError, "expected 4-member Array of ints"
  109. return inputIntegerArray
  110. server = SOAP.SOAPServer(("localhost", 8080))
  111. server.registerKWFunction(echoIntegerArray )
  112. server.serve_forever()
  113. ## /CODE
  114. This server supports the method echoIntegerArray, requiring the named parameter
  115. inputIntegerArray, which must be a four-member array of type int.
  116. ## CODE
  117. import SOAP
  118. def echoStruct (inputStruct):
  119. myfloat = inputStruct["varFloat"]
  120. mystr = inputStruct["varString"]
  121. myint = inputStruct["varInt"]
  122. return inputStruct
  123. server = SOAP.SOAPServer(("localhost", 8080))
  124. server.registerKWFunction(echoStruct )
  125. server.serve_forever()
  126. ## /CODE
  127. This code creates a server with a method echoStruct, which requires that the
  128. incoming Struct have elements named varFloat, varString, and varInt. That is,
  129. the server will fault if the incoming Struct does not have any of those
  130. elements. **Note, this server code does NOT require that these be the only
  131. elements in the struct- just that they be present**. This method simply
  132. returns the Struct passed in.
  133. ## CODE
  134. import sys
  135. import SOAP
  136. serverstring = "SOAP.py (actzero.com) running "+sys.platform
  137. def Buy(**kw):
  138. try:
  139. PurchaseOrder = kw["PurchaseOrder"]
  140. except:
  141. PurchaseOrder = kw["PO"]
  142. POkeys = PurchaseOrder['_keyord']
  143. POkeys.sort()
  144. POkeys_expected = ["items","poID","createDate"]
  145. POkeys_expected.sort()
  146. if POkeys != POkeys_expected:
  147. raise ValueError, "struct 'PurchaseOrder' needs %s, %s, and %s" % tuple(POkeys_expected)
  148. items = PurchaseOrder["items"].__dict__
  149. data = items["data"]
  150. retstring = ""
  151. for item in data:
  152. itemdict = item["_asdict"]
  153. q = itemdict["quantity"]
  154. p = itemdict["price"]
  155. name = itemdict["name"]
  156. if retstring != "":
  157. retstring += ", "
  158. else:
  159. retstring = "bought "
  160. retstring += "%d %s(s) for %.2f" % (q,name,p)
  161. retstring += " from "+serverstring
  162. return retstring
  163. server = SOAP.SOAPServer(("localhost", 8080))
  164. namespace = "http://www.soapinterop.org/Bid"
  165. server.registerKWFunction(Buy, namespace )
  166. server.serve_forever()
  167. ## /CODE
  168. This example creates a server to implement 'Buy', which takes a parameter
  169. named either PurchaseOrder or PO. (Notice the use of **kw as the input
  170. parameter to the method for this functionality).
  171. The server gets the names of the Struct's members by using the '_keyord'
  172. key of the Struct-as-dictionary. It checks these names against what it
  173. expects from the client, and raises a fault if the two are not the same.
  174. By using the __dict__ attribute, the server gets the 'items' (an elemnent of
  175. the PurchaseOrder Struct) as a dictionary. Then it checks that 'items' is
  176. formatted as expected. Finally, it returns a confirmation of what was bought.
  177. $Id: simpleTypes.txt,v 1.2 2005/02/21 20:09:39 warnes Exp $