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
# File 'lib/wechat/http_client.rb', line 9

def initialize(base, network_setting)
  @base = base
  @httpx = HTTPX.with(timeout: { connect_timeout: network_setting.timeout, request_timeout: network_setting.timeout })

  if network_setting.proxy_url.present?
    @httpx = @httpx.with_proxy(uri: network_setting.proxy_url,
                               username: network_setting.proxy_username,
                               password: network_setting.proxy_password)
  end

  return unless network_setting.skip_verify_ssl

  @httpx = @httpx.with(ssl: { verify_mode: OpenSSL::SSL::VERIFY_NONE })
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

#httpxObject (readonly)

Returns the value of attribute httpx.



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

def httpx
  @httpx
end

Instance Method Details

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



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

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

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



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

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

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



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

def post_file(path, file, post_header = {})
  request(path, post_header) do |url, headers|
    params = headers.delete(:params)
    form_file = file.is_a?(HTTP::FormData::File) ? file : HTTP::FormData::File.new(file)
    httpx.with(headers: headers)
         .post(url, params: params,
                    form: { media: form_file })
  end
end