7
8
9
10
11
12
13
14
15
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
63
64
65
66
67
68
69
70
71
72
73 
     | 
    
      # File 'lib/chargebee/rest.rb', line 7
def self.request(method, url, env, params=nil, ={})
  raise Error.new('No environment configured.') unless env
  api_key = env.api_key
  
  if(ChargeBee.verify_ca_certs?)
    ssl_opts = {
      :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
      :ssl_ca_file => ChargeBee.ca_cert_path
    }
  else
    ssl_opts = {
      :verify_ssl => false
    }
  end
  case method.to_s.downcase.to_sym
  when :get, :head, :delete
     = { :params => params }.merge()
    payload = nil
  else
    payload = params
  end
    
  user_agent = ChargeBee.user_agent 
   = { 
    "User-Agent" => user_agent,
    :accept => :json,
    "Lang-Version" => RUBY_VERSION,
    "OS-Version" => RUBY_PLATFORM 
    }.merge()      
  opts = {
    :method => method,
    :url => env.api_url(url),
    :user => api_key,
    :headers => ,
    :payload => payload,
    :open_timeout => env.connect_timeout,
    :timeout => env.read_timeout
    }.merge(ssl_opts)
  begin
    response = RestClient::Request.execute(opts)
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
        raise handle_for_error(e, rcode, rbody)
    else
        raise IOError.new("IO Exception when trying to connect to chargebee with url #{opts[:url]} . Reason #{e}",e)
    end
  rescue Exception => e
        raise IOError.new("IO Exception when trying to connect to chargebee with url #{opts[:url]} . Reason #{e}",e)        
  end
   = response.
  rbody = response.body
  rcode = response.code
  begin
    resp = JSON.parse(rbody)
  rescue Exception => e
    if rbody.include? "503"
        raise Error.new("Sorry, the server is currently unable to handle the request due to a temporary overload or scheduled maintenance. Please retry after sometime. \n type: internal_temporary_error, \n http_status_code: 503, \n error_code: internal_temporary_error,\n content: #{rbody.inspect}",e)
    elsif rbody.include? "504"
        raise Error.new("The server did not receive a timely response from an upstream server, request aborted. If this problem persists, contact us at support@chargebee.com. \n type: gateway_timeout, \n http_status_code: 504, \n error_code: gateway_timeout,\n content:  #{rbody.inspect}",e)
    else
        raise Error.new("Sorry, something went wrong when trying to process the request. If this problem persists, contact us at support@chargebee.com. \n type: internal_error, \n http_status_code: 500, \n error_code: internal_error,\n content:  #{rbody.inspect}",e)
    end
  end
  resp = Util.symbolize_keys(resp)
  return resp, 
end
     |