Method: Auth0::Mixins::HTTPProxy#request

Defined in:
lib/auth0/mixins/httpproxy.rb

#request(method, uri, body = {}, extra_headers = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/auth0/mixins/httpproxy.rb', line 74

def request(method, uri, body = {}, extra_headers = {})
  result = if method == :get
    @headers ||= {}
    get_headers = @headers.merge({params: body}).merge(extra_headers)
    call(:get, encode_uri(uri), timeout, get_headers)
  elsif method == :delete
    @headers ||= {}
    delete_headers = @headers.merge({ params: body })
    call(:delete, encode_uri(uri), timeout, delete_headers)
  elsif method == :delete_with_body
    call(:delete, encode_uri(uri), timeout, headers, body.to_json)
  elsif method == :post_file
    body.merge!(multipart: true)
    # Ignore the default Content-Type headers and let the HTTP client define them
    post_file_headers = headers.except('Content-Type') if headers != nil
    # Actual call with the altered headers
    call(:post, encode_uri(uri), timeout, post_file_headers, body)
  elsif method == :post_form
    form_post_headers = headers.except('Content-Type') if headers != nil
    call(:post, encode_uri(uri), timeout, form_post_headers, body.compact)
  else
    call(method, encode_uri(uri), timeout, headers, body.to_json)
  end

  case result.code
  when 200...226 then safe_parse_json(result.body)
  when 400       then raise Auth0::BadRequest.new(result.body, code: result.code, headers: result.headers)
  when 401       then raise Auth0::Unauthorized.new(result.body, code: result.code, headers: result.headers)
  when 403       then raise Auth0::AccessDenied.new(result.body, code: result.code, headers: result.headers)
  when 404       then raise Auth0::NotFound.new(result.body, code: result.code, headers: result.headers)
  when 429       then raise Auth0::RateLimitEncountered.new(result.body, code: result.code, headers: result.headers)
  when 500       then raise Auth0::ServerError.new(result.body, code: result.code, headers: result.headers)
  else           raise Auth0::Unsupported.new(result.body, code: result.code, headers: result.headers)
  end
end