3.8.1
This commit is contained in:
48
sdk/Ruby/PhalApiClient/demo.rb
Normal file
48
sdk/Ruby/PhalApiClient/demo.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
require_relative 'phalapi_client'
|
||||
|
||||
class MyFilter < PhalApi::ClientFilter
|
||||
end
|
||||
|
||||
def show_res(response)
|
||||
puts "response: #{response.ret}, #{response.data}, #{response.msg}"
|
||||
end
|
||||
|
||||
a_client = PhalApi::Client.create.withHost('http://demo.phalapi.net')
|
||||
a_response = a_client.withService('Default.Index').withParams('username', 'dogstar').withTimeout(3000).request()
|
||||
|
||||
puts a_response.ret, a_response.data, a_response.msg
|
||||
|
||||
puts "--------------------"
|
||||
|
||||
a_client = PhalApi::Client.create
|
||||
#a_client = PhalApi::Client.new
|
||||
|
||||
a_response = a_client.withHost('http://demo.phalapi.net').withService('Default.Index').withParams('username', 'dogstar').withParams('v', '1.3.0').request()
|
||||
|
||||
puts "--------------------"
|
||||
|
||||
#puts a_client
|
||||
show_res a_response
|
||||
|
||||
puts "--------------------"
|
||||
|
||||
begin
|
||||
a_response = a_client.reset.withParams('one').request
|
||||
rescue Exception => e
|
||||
puts e.message
|
||||
end
|
||||
|
||||
puts "--------------------"
|
||||
|
||||
a_response = a_client.reset.withFilter(MyFilter.new).withService('Default.Index').withParams('username', 'dogstar').request
|
||||
show_res a_response
|
||||
|
||||
puts "--------------------"
|
||||
|
||||
a_response = a_client.reset.withService('XXXX.noThisMethod').request
|
||||
puts a_response.ret, a_response.data, a_response.msg
|
||||
show_res a_response
|
||||
|
||||
puts 'we done!'
|
||||
puts 'we done!'
|
||||
puts 'we done!'
|
||||
158
sdk/Ruby/PhalApiClient/phalapi_client.rb
Normal file
158
sdk/Ruby/PhalApiClient/phalapi_client.rb
Normal file
@@ -0,0 +1,158 @@
|
||||
require 'open-uri'
|
||||
require 'net/http'
|
||||
require 'json'
|
||||
|
||||
# PhalApi 客户端SDK包(Ruby版)
|
||||
#
|
||||
# - 以接口查询语言(ASQL)的方式来实现接口请求
|
||||
# - 出于简明客户端,将全部的类都归于同一个文件,避免过多的加载
|
||||
#
|
||||
# <br>使用示例:<br>
|
||||
# ```
|
||||
# a_response = PhalApi::Client.create \
|
||||
# .withHost('http://demo.phalapi.net') \
|
||||
# .withService('Default.Index') \
|
||||
# .withParams('username', 'dogstar') \
|
||||
# .withTimeout(3000) \
|
||||
# .request
|
||||
#
|
||||
# puts a_response.ret, a_response.data, a_response.msg
|
||||
#
|
||||
# ```
|
||||
#
|
||||
# @package PhalApi\SDK
|
||||
# @license http://www.phalapi.net/license GPL 协议
|
||||
# @link http://www.phalapi.net/
|
||||
# @author dogstar <chanzonghuang@gmail.com> 2015-10-25
|
||||
|
||||
module PhalApi
|
||||
|
||||
# PhalApi::Client 客户端入口类
|
||||
# 完成总的调用
|
||||
class Client
|
||||
def self.create
|
||||
self.new
|
||||
end
|
||||
|
||||
def method_missing(name, *args, &block)
|
||||
raise "undefined method `#{name}' for PhalApi::Client" if "with" != name[0,4].downcase
|
||||
|
||||
param_name = name[4, name.length].downcase
|
||||
|
||||
case param_name
|
||||
when 'host'
|
||||
@host = args[0]
|
||||
when 'filter'
|
||||
@filter = args[0]
|
||||
when 'parser'
|
||||
@parser = args[0]
|
||||
when 'service'
|
||||
@service = args[0]
|
||||
when 'timeout'
|
||||
@timeoutMs = args[0].to_i
|
||||
else 'params'
|
||||
raise "you forget a value for param: #{args[0]} ?" if args[1] == nil #warm ?
|
||||
@params[args[0]] = args[1]
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
def initialize
|
||||
@host = ''
|
||||
|
||||
reset
|
||||
|
||||
@parser = PhalApi::ClientParserJson.new
|
||||
end
|
||||
|
||||
def reset
|
||||
@service, @timeoutMs = '', 3000
|
||||
@params = Hash.new
|
||||
self
|
||||
end
|
||||
|
||||
# 发起接口请求
|
||||
def request
|
||||
url = @host
|
||||
|
||||
url += "?service=" + @service if @service != nil and @service != ''
|
||||
|
||||
@filter.send :filter, @service, @params if @filter != nil
|
||||
|
||||
begin
|
||||
rs = do_request url, @params, @timeoutMs
|
||||
return @parser.parse rs
|
||||
rescue Exception => e
|
||||
return PhalApi::ClientResponse.new(408, [], e.message)
|
||||
end
|
||||
end
|
||||
|
||||
def do_request(url, params, timeoutMs)
|
||||
uri = URI.parse(url)
|
||||
res = Net::HTTP.post_form(uri, params)
|
||||
# TODO: timeoutMs ?
|
||||
|
||||
case res
|
||||
when Net::HTTPSuccess
|
||||
return res.body
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 接口返回结果
|
||||
#
|
||||
# - 与接口返回的格式对应,即有:ret/data/msg
|
||||
class ClientResponse
|
||||
def initialize(ret, data = nil, msg = nil)
|
||||
@ret, @data, @msg = ret, data, msg
|
||||
end
|
||||
|
||||
def ret
|
||||
@ret
|
||||
end
|
||||
|
||||
def data
|
||||
@data
|
||||
end
|
||||
|
||||
def msg
|
||||
@msg
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# 接口结果解析器
|
||||
#
|
||||
# - 可用于不同接口返回格式的处理
|
||||
class ClientParser
|
||||
def parse(rs)
|
||||
raise 'hey guys, you should rewrite PhalApi::ClientPaser.parse'
|
||||
end
|
||||
end
|
||||
|
||||
# JSON解析
|
||||
class ClientParserJson < PhalApi::ClientParser
|
||||
def parse(rs)
|
||||
#puts "what we got: #{rs}"
|
||||
return PhalApi::ClientResponse.new(408, [], 'Request Timeout') if rs == nil
|
||||
|
||||
begin
|
||||
a_json = JSON.parse(rs)
|
||||
return PhalApi::ClientResponse.new(a_json['ret'], a_json['data'], a_json['msg'])
|
||||
rescue JSON::ParserError => e
|
||||
return PhalApi::ClientResponse.new(500, [], 'Internal Server Error')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 接口过滤器
|
||||
class ClientFilter
|
||||
def filter(service, *params)
|
||||
#nothing here ...
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
103
sdk/Ruby/PhalApiClient/tests/client.rb
Normal file
103
sdk/Ruby/PhalApiClient/tests/client.rb
Normal file
@@ -0,0 +1,103 @@
|
||||
require_relative '../phalapi_client'
|
||||
require 'test/unit'
|
||||
|
||||
# open class
|
||||
class PhalApi::Client
|
||||
attr_accessor :host, :service, :filter, :parser, :timeoutMs, :params
|
||||
end
|
||||
|
||||
class ClientTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@a_client = PhalApi::Client.create
|
||||
end
|
||||
|
||||
def test_create
|
||||
assert_instance_of PhalApi::Client, @a_client
|
||||
end
|
||||
|
||||
def test_with_host
|
||||
@a_client.withHost('http://demo')
|
||||
|
||||
assert_equal 'http://demo', @a_client.host
|
||||
end
|
||||
|
||||
def test_with_filter
|
||||
@a_client.withFilter(PhalApi::ClientFilter.new)
|
||||
|
||||
assert_instance_of PhalApi::ClientFilter, @a_client.filter
|
||||
end
|
||||
|
||||
def test_with_parser
|
||||
@a_client.withParser(PhalApi::ClientParserJson.new)
|
||||
|
||||
assert_instance_of PhalApi::ClientParserJson, @a_client.parser
|
||||
end
|
||||
|
||||
def test_with_service
|
||||
@a_client.withService('Default.Index')
|
||||
|
||||
assert_equal 'Default.Index', @a_client.service
|
||||
end
|
||||
|
||||
def test_with_timeoutMs
|
||||
@a_client.withTimeout(5000)
|
||||
|
||||
assert_equal 5000, @a_client.timeoutMs
|
||||
end
|
||||
|
||||
def test_with_more_params
|
||||
@a_client.withParams('username', 'dogstar')
|
||||
@a_client.withParams('version', '1.3.0')
|
||||
@a_client.withParams('age', 100)
|
||||
|
||||
params = @a_client.params
|
||||
assert_equal 'dogstar', params['username']
|
||||
assert_equal 100, params['age']
|
||||
end
|
||||
|
||||
def test_with_unexpected_params
|
||||
assert_raise (RuntimeError) {
|
||||
@a_client.withParams('mess')
|
||||
}
|
||||
end
|
||||
|
||||
def test_reset
|
||||
@a_client.withHost('http://demo').withService('User.Move').withParams('id', '10');
|
||||
|
||||
@a_client.reset
|
||||
assert_equal '', @a_client.service
|
||||
|
||||
@a_client.withService('Default.Index').withParams('username', 'dogstar')
|
||||
assert_equal 'Default.Index', @a_client.service
|
||||
|
||||
params = @a_client.params
|
||||
assert_equal 'dogstar', params['username']
|
||||
assert_equal nil, params['id']
|
||||
end
|
||||
|
||||
def test_base_request
|
||||
a_response = @a_client.withHost('http://demo.phalapi.net').request
|
||||
|
||||
assert_equal 200, a_response.ret
|
||||
assert_not_nil a_response.data
|
||||
end
|
||||
|
||||
def test_normal_request
|
||||
a_response = @a_client.withHost('http://demo.phalapi.net').withService('Default.Index').withParams('username', 'dogstar').withParams('v', '1.3.0').request
|
||||
|
||||
assert_equal 200, a_response.ret
|
||||
assert_not_nil a_response.data
|
||||
end
|
||||
|
||||
def test_wrong_request
|
||||
a_response = @a_client.withHost('http://demo.phalapi.net').withService('XXXX.noThisMethod').request
|
||||
|
||||
assert_equal 400, a_response.ret
|
||||
end
|
||||
|
||||
def test_timeout_request
|
||||
a_response = @a_client.withHost('http://xxx.phalapi.net').withService('Default.Index').request
|
||||
|
||||
assert_equal 408, a_response.ret
|
||||
end
|
||||
end
|
||||
35
sdk/Ruby/PhalApiClient/tests/client_parser_json.rb
Normal file
35
sdk/Ruby/PhalApiClient/tests/client_parser_json.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
require_relative '../phalapi_client'
|
||||
require 'test/unit'
|
||||
|
||||
class ClientParserJsonTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@parser = PhalApi::ClientParserJson.new
|
||||
end
|
||||
|
||||
def test_nil
|
||||
rs = nil
|
||||
a_response = @parser.parse(rs)
|
||||
|
||||
assert_equal 408, a_response.ret
|
||||
end
|
||||
|
||||
def test_illegal_json
|
||||
rs = 'i am not a json, as you can see'
|
||||
a_response = @parser.parse(rs)
|
||||
|
||||
assert_equal 500, a_response.ret
|
||||
end
|
||||
|
||||
def test_normal_json
|
||||
rs = '{"ret":200,"data":{"title":"Hello World","content":"Welcome to use Web Tools!","version":"1.0.0","time":1415982826},"msg":""}'
|
||||
a_response = @parser.parse(rs)
|
||||
|
||||
assert_equal 200, a_response.ret
|
||||
|
||||
data = a_response.data
|
||||
assert_equal 'Hello World', data['title']
|
||||
assert_equal '1.0.0', data['version']
|
||||
|
||||
assert_equal '', a_response.msg
|
||||
end
|
||||
end
|
||||
14
sdk/Ruby/PhalApiClient/tests/client_response.rb
Normal file
14
sdk/Ruby/PhalApiClient/tests/client_response.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
require_relative '../phalapi_client'
|
||||
require 'test/unit'
|
||||
|
||||
class ClientResponseTest < Test::Unit::TestCase
|
||||
def test_new
|
||||
a_response = PhalApi::ClientResponse.new(400)
|
||||
assert_equal 400, a_response.ret
|
||||
|
||||
a_response = PhalApi::ClientResponse.new(500, [], 'Server Error')
|
||||
assert_equal 500, a_response.ret
|
||||
assert_equal [], a_response.data
|
||||
assert_equal 'Server Error', a_response.msg
|
||||
end
|
||||
end
|
||||
7
sdk/Ruby/PhalApiClient/tests/run_tests
Normal file
7
sdk/Ruby/PhalApiClient/tests/run_tests
Normal file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
ruby ./client_response.rb
|
||||
|
||||
ruby ./client_parser_json.rb
|
||||
|
||||
ruby ./client.rb
|
||||
84
sdk/Ruby/README.md
Normal file
84
sdk/Ruby/README.md
Normal file
@@ -0,0 +1,84 @@
|
||||
|
||||
## 推荐使用Gem
|
||||
推荐使用:[PhalApi SDK for Ruby](https://github.com/phalapi/phalapi-sdk-ruby) 。
|
||||
|
||||
|
||||
## 使用说明
|
||||
将框架目录下的 ./SDK/Ruby/PhalApiClient 目录中的全部代码拷贝到项目里面即可使用。
|
||||
|
||||
## 代码示例
|
||||
如下是使用的代码场景片段。
|
||||
|
||||
首先,我们需要导入SDK包:
|
||||
```ruby
|
||||
#demo.rb
|
||||
|
||||
require_relative './PhalApiClient/phalapi_client'
|
||||
|
||||
a_client = PhalApi::Client.create.withHost('http://demo.phalapi.net')
|
||||
a_response = a_client.withService('Default.Index').withParams('username', 'dogstar').withTimeout(3000).request()
|
||||
|
||||
puts a_response.ret, a_response.data, a_response.msg
|
||||
```
|
||||
|
||||
## 运行效果
|
||||
运行后,可以看到:
|
||||
```
|
||||
200
|
||||
{"title"=>"Hello World!", "content"=>"dogstar您好,欢迎使用PhalApi!", "version"=>"1.2.1", "time"=>1445741092}
|
||||
|
||||
```
|
||||
## 6.5.4 更多调用
|
||||
当需要重复调用时,需要先进行 **重置操作** ,如:
|
||||
```ruby
|
||||
#one more time
|
||||
a_response = a_client.reset \
|
||||
.withService("User.GetBaseInfo") \
|
||||
.withParams("user_id", "1") \
|
||||
.request
|
||||
|
||||
puts a_response.ret, a_response.data, a_response.msg
|
||||
```
|
||||
|
||||
当请求有异常时,返回的 ret!= 200,如:
|
||||
```ruby
|
||||
#illegal request
|
||||
a_response = a_client.reset.withService('XXXX.noThisMethod').request
|
||||
|
||||
puts a_response.ret, a_response.data, a_response.msg
|
||||
```
|
||||
|
||||
以上的输出为:
|
||||
```
|
||||
--------------------
|
||||
400
|
||||
非法请求:接口服务XXXX.noThisMethod不存在
|
||||
|
||||
```
|
||||
## 扩展你的过滤器和结果解析器
|
||||
### (1)扩展过滤器
|
||||
当服务端接口需要接口签名验证,或者接口参数加密传送,或者压缩传送时,可以实现此过滤器,以便和服务端操持一致。
|
||||
|
||||
当需要扩展时,分两步。首先,需要实现过滤器接口:
|
||||
```ruby
|
||||
class MyFilter < PhalApi::ClientFilter
|
||||
def filter(service, *params)
|
||||
#TODO ...
|
||||
end
|
||||
}
|
||||
```
|
||||
然后设置过滤器:
|
||||
```ruby
|
||||
a_response = PhalApi::Client.create.withHost('http://demo.phalapi.net') \
|
||||
.withFilter(MyFilter.new) \
|
||||
# ... \
|
||||
.request
|
||||
```
|
||||
### (2)扩展结果解析器
|
||||
当返回的接口结果不是JSON格式时,可以重新实现此接口。
|
||||
|
||||
当需要扩展时,同样分两步。类似过滤器扩展,这里不再赘述。
|
||||
|
||||
## 一如既往的单元测试
|
||||
最后,附一张单元测试运行的效果图:
|
||||

|
||||
Reference in New Issue
Block a user