Module: SyncwiseApi::ServiceUtils::HTTP

Defined in:
lib/syncwise_api/service_utils/http.rb

Constant Summary collapse

JSON_MIME =
'application/json'

Class Method Summary collapse

Class Method Details

.post(request_url_string, body, &callback) ⇒ Object

callback will receive the response body



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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