module SyncwiseApi::ServiceUtils::HTTP

Constants

JSON_MIME

Public Class Methods

post(request_url_string, body, &callback) click to toggle source

callback will receive the response body

# File lib/syncwise_api/service_utils/http.rb, line 10
def post(request_url_string, body, &callback)
  uri = URI(request_url_string)
  req = Net::HTTP::Post.new(uri)
  req.body = body
  req.content_type = JSON_MIME
  req['Accept'] = JSON_MIME
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    begin
      resp = http.request(req)
      # throws HTTP error if response.class is not Net::HTTPSuccess
      resp.value
      callback.call(resp)
    rescue SyncwiseApi::Errors::SyncwiseError => e
      SyncwiseApi::LOGGER.error e
    rescue => e
      fail SyncwiseApi::Errors::HttpError.new(e, request_url_string)
    end
  end
end

Private Instance Methods

post(request_url_string, body, &callback) click to toggle source

callback will receive the response body

# File lib/syncwise_api/service_utils/http.rb, line 10
def post(request_url_string, body, &callback)
  uri = URI(request_url_string)
  req = Net::HTTP::Post.new(uri)
  req.body = body
  req.content_type = JSON_MIME
  req['Accept'] = JSON_MIME
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    begin
      resp = http.request(req)
      # throws HTTP error if response.class is not Net::HTTPSuccess
      resp.value
      callback.call(resp)
    rescue SyncwiseApi::Errors::SyncwiseError => e
      SyncwiseApi::LOGGER.error e
    rescue => e
      fail SyncwiseApi::Errors::HttpError.new(e, request_url_string)
    end
  end
end