Class: WebHDFS::Request
- Inherits:
-
Object
- Object
- WebHDFS::Request
- Defined in:
- lib/webhdfs/request.rb
Overview
Class to make http requests
Constant Summary collapse
- KNOWN_ERRORS =
['LeaseExpiredException'].freeze
Instance Attribute Summary collapse
-
#doas ⇒ Object
readonly
Returns the value of attribute doas.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#kerberos ⇒ Object
readonly
Returns the value of attribute kerberos.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#read_timeout ⇒ Object
readonly
Returns the value of attribute read_timeout.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #build_path(path, op, params) ⇒ Object
- #connection ⇒ Object
-
#execute(path, method, header = nil, payload = nil, op = nil, params = {}, retries = 0) ⇒ Object
Execute request.
- #generic_request(connection, request_path, method, header = nil, payload = nil) ⇒ Object
-
#initialize(host, port, options = {}) ⇒ Request
constructor
Constructor.
- #make_request(connection, request_path, method, header = nil, payload = nil) ⇒ Object
- #raise_response_error(code, message) ⇒ Object
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, = {}) @host = host @port = port @username = [:username] @doas = [:doas] @proxy = [:proxy] @ssl = [:ssl] @kerberos = [:kerberos] @open_timeout = [:open_timeout] @read_timeout = [:read_timeout] @retry_known_errors = [:retry_known_errors] @retry_times = [:retry_times] @retry_interval = [:retry_interval] end |
Instance Attribute Details
#doas ⇒ Object (readonly)
Returns the value of attribute doas.
13 14 15 |
# File 'lib/webhdfs/request.rb', line 13 def doas @doas end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
13 14 15 |
# File 'lib/webhdfs/request.rb', line 13 def host @host end |
#kerberos ⇒ Object (readonly)
Returns the value of attribute kerberos.
14 15 16 |
# File 'lib/webhdfs/request.rb', line 14 def kerberos @kerberos end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
15 16 17 |
# File 'lib/webhdfs/request.rb', line 15 def open_timeout @open_timeout end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
13 14 15 |
# File 'lib/webhdfs/request.rb', line 13 def port @port end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
14 15 16 |
# File 'lib/webhdfs/request.rb', line 14 def proxy @proxy end |
#read_timeout ⇒ Object (readonly)
Returns the value of attribute read_timeout.
15 16 17 |
# File 'lib/webhdfs/request.rb', line 15 def read_timeout @read_timeout end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
14 15 16 |
# File 'lib/webhdfs/request.rb', line 14 def ssl @ssl end |
#username ⇒ Object (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 |
#connection ⇒ Object
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 = 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 =~ /^\{"RemoteException":\{/ begin detail = JSON.parse() 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, ) 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.}" 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.}" 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, ) case code when '400' raise WebHDFS::ClientError, when '401' raise WebHDFS::SecurityError, when '403' raise WebHDFS::IOError, when '404' raise WebHDFS::FileNotFoundError, when '500' raise WebHDFS::ServerError, else raise WebHDFS::RequestFailedError, "response code:#{code}, " \ "message:#{}" end end |