Class: Pindo::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/client/httpclient.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#baseurlObject

Returns the value of attribute baseurl.



82
83
84
# File 'lib/pindo/client/httpclient.rb', line 82

def baseurl
  @baseurl
end

#request_configObject

Returns the value of attribute request_config.



83
84
85
# File 'lib/pindo/client/httpclient.rb', line 83

def request_config
  @request_config
end

#tokenObject

Returns the value of attribute token.



81
82
83
# File 'lib/pindo/client/httpclient.rb', line 81

def token
  @token
end

Class Method Details

.create_instanceObject



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pindo/client/httpclient.rb', line 112

def self.create_instance()

    con = Faraday.new do |config|
      # 使用自定义重试中间件
      config.use SimpleRetryMiddleware, max: 3, interval: 0.5, backoff_factor: 2
      # 使用 Typhoeus 适配器
      config.adapter :typhoeus
    end

    con
end

.create_instance_with_proxyObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pindo/client/httpclient.rb', line 85

def self.create_instance_with_proxy()

    # puts body_params.to_json
    proxy_options = {
      uri: ENV['http_proxy'] || ENV['https_proxy'],
      user: ENV['HTTP_PROXY_USER'],
      password: ENV['HTTP_PROXY_PASSWORD']
    }

    retry_options = {
      max: 3,
      interval: 0.5,
      backoff_factor: 2,
      interval_randomness: 0.5
    }

    con = Faraday.new do |config|
      # 使用自定义重试中间件
      config.use SimpleRetryMiddleware, retry_options
      # 使用 Typhoeus 适配器(支持代理和连接池)
      config.adapter :typhoeus
      config.proxy = proxy_options.compact if proxy_options[:uri]
    end

    con
end