Class: Wechat::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, network_setting) ⇒ HttpClient

Returns a new instance of HttpClient.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wechat/http_client.rb', line 9

def initialize(base, network_setting)
  @base = base
  @httprb = if HTTP::VERSION.to_i >= 4
              HTTP.timeout(write: network_setting.timeout, connect: network_setting.timeout, read: network_setting.timeout)
            else
              HTTP.timeout(:global, write: network_setting.timeout, connect: network_setting.timeout, read: network_setting.timeout)
            end

  unless network_setting.proxy_url.nil?
    @httprb = @httprb.via(network_setting.proxy_url, network_setting.proxy_port.to_i, network_setting.proxy_username, network_setting.proxy_password)
  end

  @ssl_context = OpenSSL::SSL::SSLContext.new
  @ssl_context.ssl_version = 'TLSv1_2'
  @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE if network_setting.skip_verify_ssl
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/wechat/http_client.rb', line 7

def base
  @base
end

#httprbObject (readonly)

Returns the value of attribute httprb.



7
8
9
# File 'lib/wechat/http_client.rb', line 7

def httprb
  @httprb
end

#ssl_contextObject (readonly)

Returns the value of attribute ssl_context.



7
8
9
# File 'lib/wechat/http_client.rb', line 7

def ssl_context
  @ssl_context
end

Instance Method Details

#get(path, get_header = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/wechat/http_client.rb', line 26

def get(path, get_header = {})
  request(path, get_header) do |url, header|
    params = header.delete(:params)
    httprb.headers(header).get(url, params: params, ssl_context: ssl_context)
  end
end

#post(path, payload, post_header = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/wechat/http_client.rb', line 33

def post(path, payload, post_header = {})
  request(path, post_header) do |url, header|
    params = header.delete(:params)
    httprb.headers(header).post(url, params: params, body: payload, ssl_context: ssl_context)
  end
end

#post_file(path, file, post_header = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wechat/http_client.rb', line 40

def post_file(path, file, post_header = {})
  request(path, post_header) do |url, header|
    params = header.delete(:params)
    form_file = file.is_a?(HTTP::FormData::File) ? file : HTTP::FormData::File.new(file)
    httprb.headers(header)
          .post(url, params: params,
                     form: { media: form_file,
                             hack: 'X' }, # Existing here for http-form_data 1.0.1 handle single param improperly
                     ssl_context: ssl_context)
  end
end