phalapi_client.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. require 'open-uri'
  2. require 'net/http'
  3. require 'json'
  4. # PhalApi 客户端SDK包(Ruby版)
  5. #
  6. # - 以接口查询语言(ASQL)的方式来实现接口请求
  7. # - 出于简明客户端,将全部的类都归于同一个文件,避免过多的加载
  8. #
  9. # <br>使用示例:<br>
  10. # ```
  11. # a_response = PhalApi::Client.create \
  12. # .withHost('http://demo.phalapi.net') \
  13. # .withService('Default.Index') \
  14. # .withParams('username', 'dogstar') \
  15. # .withTimeout(3000) \
  16. # .request
  17. #
  18. # puts a_response.ret, a_response.data, a_response.msg
  19. #
  20. # ```
  21. #
  22. # @package PhalApi\SDK
  23. # @license http://www.phalapi.net/license GPL 协议
  24. # @link http://www.phalapi.net/
  25. # @author dogstar <chanzonghuang@gmail.com> 2015-10-25
  26. module PhalApi
  27. # PhalApi::Client 客户端入口类
  28. # 完成总的调用
  29. class Client
  30. def self.create
  31. self.new
  32. end
  33. def method_missing(name, *args, &block)
  34. raise "undefined method `#{name}' for PhalApi::Client" if "with" != name[0,4].downcase
  35. param_name = name[4, name.length].downcase
  36. case param_name
  37. when 'host'
  38. @host = args[0]
  39. when 'filter'
  40. @filter = args[0]
  41. when 'parser'
  42. @parser = args[0]
  43. when 'service'
  44. @service = args[0]
  45. when 'timeout'
  46. @timeoutMs = args[0].to_i
  47. else 'params'
  48. raise "you forget a value for param: #{args[0]} ?" if args[1] == nil #warm ?
  49. @params[args[0]] = args[1]
  50. end
  51. self
  52. end
  53. def initialize
  54. @host = ''
  55. reset
  56. @parser = PhalApi::ClientParserJson.new
  57. end
  58. def reset
  59. @service, @timeoutMs = '', 3000
  60. @params = Hash.new
  61. self
  62. end
  63. # 发起接口请求
  64. def request
  65. url = @host
  66. url += "?service=" + @service if @service != nil and @service != ''
  67. @filter.send :filter, @service, @params if @filter != nil
  68. begin
  69. rs = do_request url, @params, @timeoutMs
  70. return @parser.parse rs
  71. rescue Exception => e
  72. return PhalApi::ClientResponse.new(408, [], e.message)
  73. end
  74. end
  75. def do_request(url, params, timeoutMs)
  76. uri = URI.parse(url)
  77. res = Net::HTTP.post_form(uri, params)
  78. # TODO: timeoutMs ?
  79. case res
  80. when Net::HTTPSuccess
  81. return res.body
  82. else
  83. return nil
  84. end
  85. end
  86. end
  87. # 接口返回结果
  88. #
  89. # - 与接口返回的格式对应,即有:ret/data/msg
  90. class ClientResponse
  91. def initialize(ret, data = nil, msg = nil)
  92. @ret, @data, @msg = ret, data, msg
  93. end
  94. def ret
  95. @ret
  96. end
  97. def data
  98. @data
  99. end
  100. def msg
  101. @msg
  102. end
  103. end
  104. # 接口结果解析器
  105. #
  106. # - 可用于不同接口返回格式的处理
  107. class ClientParser
  108. def parse(rs)
  109. raise 'hey guys, you should rewrite PhalApi::ClientPaser.parse'
  110. end
  111. end
  112. # JSON解析
  113. class ClientParserJson < PhalApi::ClientParser
  114. def parse(rs)
  115. #puts "what we got: #{rs}"
  116. return PhalApi::ClientResponse.new(408, [], 'Request Timeout') if rs == nil
  117. begin
  118. a_json = JSON.parse(rs)
  119. return PhalApi::ClientResponse.new(a_json['ret'], a_json['data'], a_json['msg'])
  120. rescue JSON::ParserError => e
  121. return PhalApi::ClientResponse.new(500, [], 'Internal Server Error')
  122. end
  123. end
  124. end
  125. # 接口过滤器
  126. class ClientFilter
  127. def filter(service, *params)
  128. #nothing here ...
  129. end
  130. end
  131. end