Class: WebHDFS::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/webhdfs/request.rb

Overview

Class to make http requests

Constant Summary collapse

KNOWN_ERRORS =
['LeaseExpiredException'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, options = {}) ⇒ Request

Constructor



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/webhdfs/request.rb', line 20

def initialize(host, port, options = {})
  @host = host
  @port = port
  @username = options[:username]
  @doas = options[:doas]
  @proxy = options[:proxy]
  @ssl = options[:ssl]
  @kerberos = options[:kerberos]
  @open_timeout = options[:open_timeout]
  @read_timeout = options[:read_timeout]
  @retry_known_errors = options[:retry_known_errors]
  @retry_times = options[:retry_times]
  @retry_interval = options[:retry_interval]
end

Instance Attribute Details

#doasObject (readonly)

Returns the value of attribute doas.



13
14
15
# File 'lib/webhdfs/request.rb', line 13

def doas
  @doas
end

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/webhdfs/request.rb', line 13

def host
  @host
end

#kerberosObject (readonly)

Returns the value of attribute kerberos.



14
15
16
# File 'lib/webhdfs/request.rb', line 14

def kerberos
  @kerberos
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



15
16
17
# File 'lib/webhdfs/request.rb', line 15

def open_timeout
  @open_timeout
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/webhdfs/request.rb', line 13

def port
  @port
end

#proxyObject (readonly)

Returns the value of attribute proxy.



14
15
16
# File 'lib/webhdfs/request.rb', line 14

def proxy
  @proxy
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



15
16
17
# File 'lib/webhdfs/request.rb', line 15

def read_timeout
  @read_timeout
end

#sslObject (readonly)

Returns the value of attribute ssl.



14
15
16
# File 'lib/webhdfs/request.rb', line 14

def ssl
  @ssl
end

#usernameObject (readonly)

Returns the value of attribute username.



13
14
15
# File 'lib/webhdfs/request.rb', line 13

def username
  @username
end

Instance Method Details

#build_path(path, op, params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/webhdfs/request.rb', line 57

def build_path(path, op, params)
  path = Addressable::URI.escape(path)
  if op
    opts = if @username && @doas
             { 'op' => op, 'user.name' => @username, 'doas' => @doas }
           elsif @username
             { 'op' => op, 'user.name' => @username }
           elsif @doas
             { 'op' => op, 'doas' => @doas }
           else
             { 'op' => op }
           end
    WebHDFS.api_path(path) + '?' + URI.encode_www_form(params.merge(opts))
  else
    path
  end
end

#connectionObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/webhdfs/request.rb', line 35

def connection
  conn = if @proxy
           Net::HTTP.new(host, port, @proxy.address, @proxy.port)
         else
           Net::HTTP.new(host, port)
         end

  if @proxy.authentication?
    conn.proxy_user = @proxy.user
    conn.proxy_pass = @proxy.password
  end

  conn.open_timeout = @open_timeout if @open_timeout
  conn.read_timeout = @read_timeout if @read_timeout

  if @ssl
    @ssl.apply_to(conn)
  else
    conn
  end
end

#execute(path, method, header = nil, payload = nil, op = nil, params = {}, retries = 0) ⇒ Object

Execute request



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/webhdfs/request.rb', line 131

def execute(path, method, header = nil, payload = nil, op = nil,
            params = {}, retries = 0)
  conn = connection

  header = @kerberos.autorization(header) if @kerberos

  request_path = build_path(path, op, params)

  response = make_request(conn, payload)

  @kerberos.check_response(response) if @kerberos

  case response
  when Net::HTTPSuccess
    response
  when Net::HTTPRedirection
    response
  else
    message = if response.body && !response.body.empty?
                response.body.delete("\n")
              else
                'Response body is empty...'
              end
    if @retry_known_errors && retries < @retry_times
      detail = nil
      if message =~ /^\{"RemoteException":\{/
        begin
          detail = JSON.parse(message)

          if detail['RemoteException'] &&
             KNOWN_ERRORS.include?(detail['RemoteException']['exception'])
            sleep @retry_interval if @retry_interval > 0
            return execute(path, method, header, payload, op, params,
                           retries + 1)
          end
        rescue
          # ignore broken json response body
        end
      end
    end
    raise_response_error(response.code, message)
  end
end

#generic_request(connection, request_path, method, header = nil, payload = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/webhdfs/request.rb', line 75

def generic_request(connection, request_path, method, header = nil,
                    payload = nil)
  res = nil
  req = Net::HTTPGenericRequest.new(method, (payload ? true : false),
                                    true, request_path, header)
  raise WebHDFS::ClientError, 'Error accepting given IO resource as' \
   ' data payload, Not valid in methods' \
   ' other than PUT and POST' unless method == 'PUT' || method == 'POST'

  req.body_stream = payload
  req.content_length = payload.size
  begin
    res = connection.request(req)
  rescue => e
    raise WebHDFS::ServerError, 'Failed to connect to host' \
                                " #{@host}:#{@port}, #{e.message}"
  end
  res
end

#make_request(connection, request_path, method, header = nil, payload = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/webhdfs/request.rb', line 95

def make_request(connection, request_path, method, header = nil,
                 payload = nil)
  res = nil
  if !payload.nil? && payload.respond_to?(:read) &&
     payload.respond_to?(:size)
    res = generic_request(connection, request_path, method, header, payload)
  else
    begin
      res = connection.send_request(method, request_path, payload, header)
    rescue => e
      raise WebHDFS::ServerError, 'Failed to connect to host' \
                                  " #{@host}:#{@port}, #{e.message}"
    end
  end
  res
end

#raise_response_error(code, message) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/webhdfs/request.rb', line 112

def raise_response_error(code, message)
  case code
  when '400'
    raise WebHDFS::ClientError, message
  when '401'
    raise WebHDFS::SecurityError, message
  when '403'
    raise WebHDFS::IOError, message
  when '404'
    raise WebHDFS::FileNotFoundError, message
  when '500'
    raise WebHDFS::ServerError, message
  else
    raise WebHDFS::RequestFailedError, "response code:#{code}, " \
                                       "message:#{message}"
  end
end