洛阳铲的日志

2010年10月24日

deeply play soap in ruby with SAVON or HandSoap

Filed under: ruby,web — 标签:, , , — 洛阳铲 @ 21:16

虽然soap4r是ruby的默认实现,但是恰如http://www.eggheadcafe.com/software/aspnet/36181571/soap-with-ruby-19-.aspx  批评的那样。文档奇缺,更新缓慢,最好的参考居然是code sample, 不过好消息是还有另外的选择:  SAVONHandSoap
而且在这儿也有SAVON和HandSoap的一个使用介绍:  http://blog.nofail.de/2010/01/savon-handsoap-shootout/

play soap with soap4r

Filed under: ruby,web — 标签:, , , , — 洛阳铲 @ 20:16

对于ruby而的soap言有两个库非常重要:soap4r这个库是NAKAMURA维护的获得ruby官方
支持的soap库,另外一个是基于soap4r的wss4r的库。遗憾的是这两个库,文档奇缺好消息是还有其他选择
最多的资料只能来自于google了,而且在soap4r的邮件组里面,NAKAMURA也常常会亲自解答一些
问题。搜索问题的时候加上NAKAMURA作为关键词之一,会有意想不到的惊喜。

soap4r无需安装,wss4r的安装略过,基础知识:

  1. wsdl学习笔记
  2. soap学习笔记

在soap4r的源代码的sample中有一个自定义header头的例子: sample/soapheader/authheader 和
sample/soapheader/soapext_basicauth两个例子。以authheader 来开始。server.rb略过。

client.rb

require ‘soap/rpc/driver’
require ‘soap/header/simplehandler’

server = ARGV.shift || ‘http://localhost:7000/’

class ClientAuthHeaderHandler < SOAP::Header::SimpleHandler
MyHeaderName = XSD::QName.new("http://schemas.xmlsoap.org/ws/2002/07/secext", "wsse")

def initialize(userid, passwd)
super(MyHeaderName)
@sessionid = nil
@userid = userid
@passwd = passwd
@mustunderstand = true
end

def on_simple_outbound
if @sessionid
{ "sessionid" => @sessionid }
else
{ "userid" => @userid, "passwd" => @passwd }
end
end

def on_simple_inbound(my_header, mustunderstand)
@sessionid = my_header["sessionid"]
end
end

ns = ‘http://tempuri.org/authHeaderPort’
serv = SOAP::RPC::Driver.new(server, ns)
serv.add_method(‘deposit’, ‘amt’)
serv.add_method(‘withdrawal’, ‘amt’)

serv.headerhandler << ClientAuthHeaderHandler.new(‘NaHi’, ‘passwd’)

serv.wiredump_dev = STDOUT

p serv.deposit(150)
p serv.withdrawal(120)

最后该程序发送的请求为:
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<n1:wsse xmlns:n1="http://schemas.xmlsoap.org/ws/2002/07/secext"
env:mustUnderstand="1">
<n1:passwd>passwd</n1:passwd>
<n1:userid>NaHi</n1:userid>
</n1:wsse>
</env:Header>
<env:Body>
<n2:deposit xmlns:n2="http://tempuri.org/authHeaderPort"
env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<amt xsi:type="xsd:int">150</amt>
</n2:deposit>
</env:Body>
</env:Envelope>

http://developer.searchmarketing.yahoo.com/docs/V7/gsg/requests.php

  1. < soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/"
  2. xmlns:wsse = "http://schemas.xmlsoap.org/ws/2002/07/secext"
  3. xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
  4. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns = "http://marketing.ews.yahooapis.com/V7" >
  6. < soapenv:Header >
  7. < wsse:Security >
  8. < wsse:UsernameToken >
  9. < wsse:Username > user </ wsse:Username >
  10. < wsse:Password > password </ wsse:Password >
  11. </ wsse:UsernameToken >
  12. </ wsse:Security >
  13. </ soapenv:Header >
  14. < soapenv:Body >
  15. </ soapenv:Body >
  16. </ soapenv:Envelope >

The preferred method is through the WS-Security extension, where both the username
and password elements are wrapped in the Security and UsernameToken elements,
as shown below. To use WS-Security you must also include the secext schema in the SOAP envelope,
here defined as wsse.

  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/"
  3. xmlns:wsse = "http://schemas.xmlsoap.org/ws/2002/07/secext"
  4. xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
  5. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns = "http://marketing.ews.yahooapis.com/V7" >
  7. < soapenv:Header >
  8. < wsse:Security >
  9. < wsse:UsernameToken >
  10. < wsse:Username > user </ wsse:Username >
  11. < wsse:Password > password </ wsse:Password >
  12. </ wsse:UsernameToken >
  13. </ wsse:Security >
  14. </ soapenv:Header >
  15. < soapenv:Body >
  16. </ soapenv:Body >
  17. </ soapenv:Envelope >


If your SOAP toolkit does not support WS-Security, use the username and password elements in the header:
  1. <? xml version = "1.0" encoding = "UTF-8" ?>
  2. < soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/"
  3. xmlns:wsse = "http://schemas.xmlsoap.org/ws/2002/07/secext"
  4. xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
  5. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns = "http://marketing.ews.yahooapis.com/V7" >
  7. < soapenv:Header >
  8. < username > user </ username >
  9. < password > password </ password >
  10. </ soapenv:Header >
  11. < soapenv:Body >
  12. </ soapenv:Body >
  13. </ soapenv:Envelope >

soap的tips:

  1. 如何删掉: env:mustUnderstand ?,两个两个方法,二选一

    1. 创建一个HTTP filter,用正则表达式来修改HTTP body: body.sub(/\s+[^:]+:mustUnderstand\S*/, ”)

    2. 如果使用svn的代码,可以在header_handle#on_outbound中使用self.mustunderstand = nil来关闭mustUnderstand属性

  2. 如何删掉: ns# namespace?

    1. http://www.google.com/url?sa=D&q=http://www.pluitsolutions.com/2007/08/10/remove-n1-namespace-for-soap4r/&usg=AFQjCNEYmZR3Xen_12paXCe4P7IscNa3Ag

    2. https://groups.google.com/group/soap4r/browse_thread/thread/aad6c8af7c0dc473/b421cd11225b1e6e?lnk=gst&q=header#b421cd11225b1e6e

  3. 如何自定义ns? 参见sample/howto/custom_ns。 请注意,系统自带的soap4r版本很低(1.5.5),建议升级至最新版。

    当前最新的是1.6.1. svn co http://dev.ctor.org/svn/soap4r/trunk soap4r-1.6.1

后记deeply play soap in ruby with SAVON or HandSoap

2010年10月13日

yield之py和rb

Filed under: Python,ruby — 标签:, , — HackGou @ 17:56

py和rb里面都有yield, 但是他们的用法却天壤之别:

py中yield用于生成器,引用[1]的话说:

yield 简单说来就是一个生成器,生成器是这样一个函数,它记住上一次返回时在函数体中的位置。对生成器函数的第二次(或第 n 次)调用跳转至该函数中间,而上次调用的所有局部变量都保持不变。

生成器 是 一个函数
函数的所有参数都会保留

第二次调用 此函数 时
使用的参数是前一次保留下的.

生成器还"记住"了它在流控制构造
生成器不仅"记住"了它数据状态。 生成器还"记住"了它在流控制构造(在命令式编程中,这种构造不只是数据值)中的位置。由于连续性使您在执行框架间任意跳转,而不总是返回到直接调用者的上下文(如同生成器那样),因此它仍是比较一般的。

这是个列子:

>>> def tt(x):
...     print "starting"
...     yield x
...     print "continue"
...     yield x+10
...     print "3th step"
...     yield x*5
... 
>>> i=tt(1)
>>> i.next()
starting
1
>>> i.next()
continue
11
>>> i.next()
3th step
5
>>> i.next()
Traceback (most recent call last):
  File "
", line 1, in 

StopIteration
>>> for k in tt(1): ... print k ... starting 1 continue 11 3th step 5
>>> type(i) <type 'generator'> >>> type(type(i)) <type 'type'> >>> type(tt) <type 'function'> >>>

而rb中,yield却是对传入块的调用,比如:

irb(main):001:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):002:0> def foo
irb(main):003:1> puts "starting"
irb(main):004:1> yield a
irb(main):005:1> puts "finished"
irb(main):006:1> end
irb(main):007:0> a.each do | a1 |
irb(main):008:1* foo(a1) { | x | puts x*x }
irb(main):009:1> end
starting
1
finished
starting
4
finished
starting
9
finished
=> [1, 2, 3]
irb(main):010:0>

可以看出,其实yied a1就是对{|x| puts x*x}的调用,

[1]: 《Python yield 用法》: http://www.pythonclub.org/python-basic/yield

Powered by WordPress