diff --git a/CHANGELOG.md b/CHANGELOG.md index 26840355..8e386da4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 5738ee3f..72328c15 100644 --- a/README.md +++ b/README.md @@ -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'] %> @@ -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` diff --git a/lib/action_controller/wechat_responder.rb b/lib/action_controller/wechat_responder.rb index dbe03fc3..c1964649 100644 --- a/lib/action_controller/wechat_responder.rb +++ b/lib/action_controller/wechat_responder.rb @@ -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 @@ -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 diff --git a/lib/generators/wechat/templates/config/wechat.yml b/lib/generators/wechat/templates/config/wechat.yml index 465ce2ba..5147b59f 100644 --- a/lib/generators/wechat/templates/config/wechat.yml +++ b/lib/generators/wechat/templates/config/wechat.yml @@ -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'] %> diff --git a/lib/wechat/api.rb b/lib/wechat/api.rb index cde9d4fc..e9879fec 100644 --- a/lib/wechat/api.rb +++ b/lib/wechat/api.rb @@ -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 diff --git a/lib/wechat/api_loader.rb b/lib/wechat/api_loader.rb index 080d61cb..2a9fc2b2 100644 --- a/lib/wechat/api_loader.rb +++ b/lib/wechat/api_loader.rb @@ -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 @@ -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 @@ -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'] } diff --git a/lib/wechat/client.rb b/lib/wechat/client.rb index d8753865..b973b4be 100644 --- a/lib/wechat/client.rb +++ b/lib/wechat/client.rb @@ -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 diff --git a/lib/wechat/corp_api.rb b/lib/wechat/corp_api.rb index b82f34d0..db4cd0d9 100644 --- a/lib/wechat/corp_api.rb +++ b/lib/wechat/corp_api.rb @@ -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) diff --git a/lib/wechat/responder.rb b/lib/wechat/responder.rb index 893999db..f524c213 100644 --- a/lib/wechat/responder.rb +++ b/lib/wechat/responder.rb @@ -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) diff --git a/spec/lib/wechat/api_spec.rb b/spec/lib/wechat/api_spec.rb index ea4acce4..a5d74c14 100644 --- a/spec/lib/wechat/api_spec.rb +++ b/spec/lib/wechat/api_spec.rb @@ -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 diff --git a/spec/lib/wechat/client_spec.rb b/spec/lib/wechat/client_spec.rb index 4250df94..54eb3aaf 100644 --- a/spec/lib/wechat/client_spec.rb +++ b/spec/lib/wechat/client_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/lib/wechat/corp_api_spec.rb b/spec/lib/wechat/corp_api_spec.rb index 77d5deba..1d0bf423 100644 --- a/spec/lib/wechat/corp_api_spec.rb +++ b/spec/lib/wechat/corp_api_spec.rb @@ -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