Method: Seatsio::HttpClient#execute

Defined in:
lib/seatsio/httpClient.rb

#execute(*args) ⇒ Object



16
17
18
19
20
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
54
55
56
57
58
59
60
61
62
# File 'lib/seatsio/httpClient.rb', line 16

def execute(*args)
  begin
    headers = { :Authorization => "Basic #{@secret_key}" }
    headers[:'X-Client-Lib'] = "ruby"
    unless @workspace_key.nil?
      headers[:'X-Workspace-Key'] = @workspace_key
    end

    url = "#{@base_url}/#{args[1]}"

    if args[2].include? :params
      params = args[2][:params]
      if params.is_a? Hash
        headers[:params] = params
      else
        url += "?" + params
      end
    end

    request_options = { method: args[0], url: url, headers: headers }

    if args[0] == :post || args[0] == :delete
      args[2].delete :params
      request_options[:payload] = args[2].to_json
    end

    response = execute_with_retries(request_options)

    # If RAW
    if args[3]
      return response
    end
    JSON.parse(response) unless response.empty?
  rescue RestClient::NotFound => e
    raise Exception::NotFoundException.new(e.response)
  rescue RestClient::ExceptionWithResponse => e
    if e.response.code == 429
      raise Exception::RateLimitExceededException.new(e.response)
    else
      raise Exception::SeatsioException.new(e.response)
    end
  rescue RestClient::Exceptions::Timeout
    raise Exception::SeatsioException.new("Timeout ERROR")
  rescue SocketError
    raise Exception::SeatsioException.new("Failed to connect to backend")
  end
end