Skip to content

Commit

Permalink
Add timeout configuration option, close #74
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Guo committed Dec 16, 2015
1 parent 8824fc5 commit c51f675
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.7.0 (unreleased)

* Add timeout configuration option, close #74

## v0.6.6 (released at 12/15/2015)

* Add jsapi_ticket support for Enterprise Account
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ production:
agentid: <%= ENV['WECHAT_AGENTID'] %>
access_token: <%= ENV['WECHAT_ACCESS_TOKEN'] %>
token: <%= ENV['WECHAT_TOKEN'] %>
skip_verify_ssl: false
timeout: 30,
skip_verify_ssl: true
encoding_aes_key: <%= ENV['WECHAT_ENCODING_AES_KEY'] %>
jsapi_ticket: <%= ENV['WECHAT_JSAPI_TICKET'] %>
Expand All @@ -133,6 +134,10 @@ test:

注意在Rails项目根目录下运行`wechat`命令行工具会优先使用`config/wechat.yml`中的`default`配置,如果失败则使用`~\.wechat.yml`中的配置,以便于在生产环境下管理多个微信账号应用。

##### 配置微信服务器超时

微信服务器有时请求会花很长时间,如果不配置,默认为20秒,可视情况配置。

##### 配置跳过SSL认证

Wechat服务器有报道曾出现[RestClient::SSLCertificateNotVerified](http://qydev.weixin.qq.com/qa/index.php?qa=11037)错误,此时可以选择关闭SSL验证。`skip_verify_ssl: true`
Expand Down
5 changes: 3 additions & 2 deletions lib/action_controller/wechat_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def wechat_responder(opts = {})
self.corpid = opts[:corpid] || Wechat.config.corpid
self.agentid = opts[:agentid] || Wechat.config.agentid
self.encrypt_mode = opts[:encrypt_mode] || Wechat.config.encrypt_mode || corpid.present?
self.timeout = opts[:timeout] || 20
self.skip_verify_ssl = opts[:skip_verify_ssl]
self.token = opts[:token] || Wechat.config.token
self.encoding_aes_key = opts[:encoding_aes_key] || Wechat.config.encoding_aes_key
Expand All @@ -14,9 +15,9 @@ def wechat_responder(opts = {})
self.wechat = Wechat.api
else
if corpid.present?
self.wechat = Wechat::CorpApi.new(corpid, opts[:corpsecret], opts[:access_token], agentid, skip_verify_ssl, opts[:jsapi_ticket])
self.wechat = Wechat::CorpApi.new(corpid, opts[:corpsecret], opts[:access_token], agentid, timeout, skip_verify_ssl, opts[:jsapi_ticket])
else
self.wechat = Wechat::Api.new(opts[:appid], opts[:secret], opts[:access_token], skip_verify_ssl, opts[:jsapi_ticket])
self.wechat = Wechat::Api.new(opts[:appid], opts[:secret], opts[:access_token], timeout, skip_verify_ssl, opts[:jsapi_ticket])
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/generators/wechat/templates/config/wechat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ production:
# appid: <%= ENV['WECHAT_APPID'] %>
# secret: <%= ENV['WECHAT_APP_SECRET'] %>
token: <%%= ENV['WECHAT_TOKEN'] %>
timeout: 30,
skip_verify_ssl: true
access_token: <%%= ENV['WECHAT_ACCESS_TOKEN'] %>
encrypt_mode: false # if true must fill encoding_aes_key
encoding_aes_key: <%%= ENV['WECHAT_ENCODING_AES_KEY'] %>
Expand Down
4 changes: 2 additions & 2 deletions lib/wechat/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Api < ApiBase
API_BASE = 'https://api.weixin.qq.com/cgi-bin/'
OAUTH2_BASE = 'https://api.weixin.qq.com/sns/oauth2/'

def initialize(appid, secret, token_file, skip_verify_ssl, jsapi_ticket_file)
@client = Client.new(API_BASE, skip_verify_ssl)
def initialize(appid, secret, token_file, timeout, skip_verify_ssl, jsapi_ticket_file)
@client = Client.new(API_BASE, timeout, skip_verify_ssl)
@access_token = AccessToken.new(@client, appid, secret, token_file)
@jsapi_ticket = JsapiTicket.new(@client, @access_token, jsapi_ticket_file)
end
Expand Down
6 changes: 4 additions & 2 deletions lib/wechat/api_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def self.with(options)
js_token_file = options[:js_token_file] || c.jsapi_ticket || '/var/tmp/wechat_jsapi_ticket'

if c.appid && c.secret && token_file.present?
Wechat::Api.new(c.appid, c.secret, token_file, c.skip_verify_ssl, js_token_file)
Wechat::Api.new(c.appid, c.secret, token_file, c.timeout, c.skip_verify_ssl, js_token_file)
elsif c.corpid && c.corpsecret && token_file.present?
Wechat::CorpApi.new(c.corpid, c.corpsecret, token_file, c.agentid, c.skip_verify_ssl, js_token_file)
Wechat::CorpApi.new(c.corpid, c.corpsecret, token_file, c.agentid, c.timeout, c.skip_verify_ssl, js_token_file)
else
puts <<-HELP
Need create ~/.wechat.yml with wechat appid and secret
Expand All @@ -35,6 +35,7 @@ def self.loading_config!
config[:access_token] ||= Rails.root.join('tmp/access_token').to_s
config[:jsapi_ticket] ||= Rails.root.join('tmp/jsapi_ticket').to_s
end
config[:timeout] ||= 20
config.symbolize_keys!
@config = OpenStruct.new(config)
end
Expand Down Expand Up @@ -68,6 +69,7 @@ def self.config_from_environment
token: ENV['WECHAT_TOKEN'],
access_token: ENV['WECHAT_ACCESS_TOKEN'],
encrypt_mode: ENV['WECHAT_ENCRYPT_MODE'],
timeout: ENV['WECHAT_TIMEOUT'],
skip_verify_ssl: ENV['WECHAT_SKIP_VERIFY_SSL'],
encoding_aes_key: ENV['WECHAT_ENCODING_AES_KEY'],
jsapi_ticket: ENV['WECHAT_JSAPI_TICKET'] }
Expand Down
9 changes: 5 additions & 4 deletions lib/wechat/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

module Wechat
class Client
attr_reader :base, :verify_ssl
attr_reader :base, :timeout, :verify_ssl

def initialize(base, skip_verify_ssl)
def initialize(base, timeout, skip_verify_ssl)
@base = base
@timeout = timeout
@verify_ssl = skip_verify_ssl ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
end

def get(path, header = {})
request(path, header) do |url, header|
RestClient::Request.execute(method: :get, url: url, headers: header, verify_ssl: verify_ssl)
RestClient::Request.execute(method: :get, url: url, headers: header, timeout: timeout, verify_ssl: verify_ssl)
end
end

def post(path, payload, header = {})
request(path, header) do |url, header|
RestClient::Request.execute(method: :post, url: url, payload: payload, headers: header, verify_ssl: verify_ssl)
RestClient::Request.execute(method: :post, url: url, payload: payload, headers: header, timeout: timeout, verify_ssl: verify_ssl)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/wechat/corp_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class CorpApi < ApiBase

API_BASE = 'https://qyapi.weixin.qq.com/cgi-bin/'

def initialize(appid, secret, token_file, agentid, skip_verify_ssl, jsapi_ticket_file)
@client = Client.new(API_BASE, skip_verify_ssl)
def initialize(appid, secret, token_file, agentid, timeout, skip_verify_ssl, jsapi_ticket_file)
@client = Client.new(API_BASE, timeout, skip_verify_ssl)
@access_token = CorpAccessToken.new(@client, appid, secret, token_file)
@agentid = agentid
@jsapi_ticket = CorpJsapiTicket.new(@client, @access_token, jsapi_ticket_file)
Expand Down
2 changes: 1 addition & 1 deletion lib/wechat/responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Responder
end

module ClassMethods
attr_accessor :wechat, :token, :corpid, :agentid, :encrypt_mode, :skip_verify_ssl, :encoding_aes_key
attr_accessor :wechat, :token, :corpid, :agentid, :encrypt_mode, :timeout, :skip_verify_ssl, :encoding_aes_key

def on(message_type, with: nil, respond: nil, &block)
fail 'Unknow message type' unless [:text, :image, :voice, :video, :link, :event, :click, :view, :scan, :batch_job, :location, :fallback].include?(message_type)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/wechat/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:jsapi_ticket_file) { Rails.root.join('tmp/jsapi_ticket') }

subject do
Wechat::Api.new('appid', 'secret', token_file, false, jsapi_ticket_file)
Wechat::Api.new('appid', 'secret', token_file, 20, false, jsapi_ticket_file)
end

before :each do
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/wechat/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Wechat::Client do
subject do
Wechat::Client.new('http://host/', false)
Wechat::Client.new('http://host/', 20, false)
end

let(:response_params) do
Expand All @@ -22,7 +22,7 @@
describe '#get' do
specify 'Will use http get method to request data' do
expect(RestClient::Request).to receive(:execute)
.with(method: :get, url: 'http://host/token', headers: { accept: :json }, verify_ssl: OpenSSL::SSL::VERIFY_PEER)
.with(method: :get, url: 'http://host/token', headers: { accept: :json }, timeout: 20, verify_ssl: OpenSSL::SSL::VERIFY_PEER)
.and_return(response_json)
subject.get('token')
end
Expand All @@ -31,7 +31,7 @@
describe '#post' do
specify 'Will use http post method to request data' do
expect(RestClient::Request).to receive(:execute)
.with(method: :post, url: 'http://host/token', payload: 'some_data', headers: { accept: :json }, verify_ssl: OpenSSL::SSL::VERIFY_PEER)
.with(method: :post, url: 'http://host/token', payload: 'some_data', headers: { accept: :json }, timeout: 20, verify_ssl: OpenSSL::SSL::VERIFY_PEER)
.and_return(response_json)
subject.post('token', 'some_data')
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/wechat/corp_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:jsapi_ticket_file) { Rails.root.join('tmp/jsapi_ticket') }

subject do
Wechat::CorpApi.new('corpid', 'corpsecret', token_file, '1', false, jsapi_ticket_file)
Wechat::CorpApi.new('corpid', 'corpsecret', token_file, '1', 20, false, jsapi_ticket_file)
end

before :each do
Expand Down

0 comments on commit c51f675

Please sign in to comment.