Class: K3cloud::Http
- Inherits:
-
Object
- Object
- K3cloud::Http
- Defined in:
- lib/k3cloud/http.rb
Overview
HTTP Client
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
-
#header ⇒ Object
Returns the value of attribute header.
-
#request_timeout ⇒ Object
Returns the value of attribute request_timeout.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url, header, body, connect_timeout, request_timeout) ⇒ Http
constructor
A new instance of Http.
- #post ⇒ Object
Constructor Details
#initialize(url, header, body, connect_timeout, request_timeout) ⇒ Http
Returns a new instance of Http.
13 14 15 16 17 18 19 |
# File 'lib/k3cloud/http.rb', line 13 def initialize(url, header, body, connect_timeout, request_timeout) @url = url @header = header @body = body @connect_timeout = connect_timeout || 2 @request_timeout = request_timeout || 2 end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
11 12 13 |
# File 'lib/k3cloud/http.rb', line 11 def body @body end |
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
11 12 13 |
# File 'lib/k3cloud/http.rb', line 11 def connect_timeout @connect_timeout end |
#header ⇒ Object
Returns the value of attribute header.
11 12 13 |
# File 'lib/k3cloud/http.rb', line 11 def header @header end |
#request_timeout ⇒ Object
Returns the value of attribute request_timeout.
11 12 13 |
# File 'lib/k3cloud/http.rb', line 11 def request_timeout @request_timeout end |
#url ⇒ Object
Returns the value of attribute url.
11 12 13 |
# File 'lib/k3cloud/http.rb', line 11 def url @url end |
Instance Method Details
#post ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/k3cloud/http.rb', line 21 def post uri = URI(@url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == "https") http.open_timeout = @connect_timeout * 60 http.read_timeout = @request_timeout * 60 request = Net::HTTP::Post.new(uri) request.initialize_http_header(@header) request["Content-Type"] = "application/json" request["User-Agent"] = "Kingdee/Ruby WebApi SDK" request.body = @body.to_json max_retries = 5 # 最大重试次数 retry_count = 0 sleep_interval = 5 # 重试间隔时间,单位秒 begin response = http.request(request) if response.code.to_i != 200 && response.code.to_i != 206 raise K3cloud::ResponseError, "status: #{response.code}, desc: #{response.body}" end response.body rescue Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout => e K3cloud.logger.error("Request timed out: #{e.}") if retry_count < max_retries retry_count += 1 K3cloud.logger.info("Request timed out. Retrying #{retry_count}/#{max_retries} in #{sleep_interval} seconds...") sleep(sleep_interval) retry else raise K3cloud::ResponseError, "Request failed after #{max_retries} retries: #{e.}, backtrace: #{e.backtrace}" end end end |