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.
-
#status_code ⇒ Object
Returns the value of attribute status_code.
-
#url ⇒ Object
Returns the value of attribute url.
-
#verify_ssl ⇒ Object
Returns the value of attribute verify_ssl.
Instance Method Summary collapse
-
#initialize(url:, header:, body:, connect_timeout: Configuration::DEFAULT_CONNECT_TIMEOUT, request_timeout: Configuration::DEFAULT_REQUEST_TIMEOUT, verify_ssl: true) ⇒ Http
constructor
A new instance of Http.
- #post ⇒ Object
Constructor Details
#initialize(url:, header:, body:, connect_timeout: Configuration::DEFAULT_CONNECT_TIMEOUT, request_timeout: Configuration::DEFAULT_REQUEST_TIMEOUT, verify_ssl: true) ⇒ Http
Returns a new instance of Http.
14 15 16 17 18 19 20 21 22 |
# File 'lib/k3cloud/http.rb', line 14 def initialize(url:, header:, body:, connect_timeout: Configuration::DEFAULT_CONNECT_TIMEOUT, request_timeout: Configuration::DEFAULT_REQUEST_TIMEOUT, verify_ssl: true) @url = url @header = header || {} @body = body @connect_timeout = connect_timeout @request_timeout = request_timeout @verify_ssl = verify_ssl end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def body @body end |
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def connect_timeout @connect_timeout end |
#header ⇒ Object
Returns the value of attribute header.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def header @header end |
#request_timeout ⇒ Object
Returns the value of attribute request_timeout.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def request_timeout @request_timeout end |
#status_code ⇒ Object
Returns the value of attribute status_code.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def status_code @status_code end |
#url ⇒ Object
Returns the value of attribute url.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def url @url end |
#verify_ssl ⇒ Object
Returns the value of attribute verify_ssl.
12 13 14 |
# File 'lib/k3cloud/http.rb', line 12 def verify_ssl @verify_ssl end |
Instance Method Details
#post ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/k3cloud/http.rb', line 24 def post uri = URI.parse(@url) http = build_http(uri) request = Net::HTTP::Post.new(uri.request_uri) request.initialize_http_header(@header) request["Content-Type"] = "application/json" request["User-Agent"] = generate_user_agent request.body = @body.is_a?(String) ? @body : @body.to_json response = http.request(request) @status_code = response.code.to_i raise K3cloud::ResponseError, "status: #{response.code}, desc: #{response.body}" if @status_code >= 400 response.body rescue Errno::ETIMEDOUT, Net::OpenTimeout, Net::ReadTimeout => e raise K3cloud::ResponseError, "Request timed out: #{e.}" rescue K3cloud::ResponseError raise rescue Net::HTTPServerError => e raise K3cloud::ResponseError, "Server error: #{e.}" rescue StandardError => e raise K3cloud::ResponseError, "Unexpected error: #{e.}" end |