Class: Vertx::HttpClient

Inherits:
Object
  • Object
show all
Includes:
ClientSSLSupport, SSLSupport, TCPSupport
Defined in:
lib/vertx/http.rb

Overview

An HTTP client. A client maintains a pool of connections to a specific host, at a specific port. The HTTP connections can act as pipelines for HTTP requests. It is used as a factory for HttpClientRequest instances which encapsulate the actual HTTP requests. It is also used as a factory for WebSockets.

Author:

Instance Method Summary collapse

Methods included from SSLSupport

#key_store_password, #key_store_password=, #key_store_path, #key_store_path=, #ssl, #ssl=, #trust_store_password, #trust_store_password=, #trust_store_path, #trust_store_path=

Methods included from ClientSSLSupport

#trust_all, #trust_all=

Methods included from TCPSupport

#receive_buffer_size=, #reuse_address, #reuse_address=, #send_buffer_size, #send_buffer_size=, #send_receive_size, #so_linger, #so_linger=, #tcp_keep_alive, #tcp_keep_alive=, #traffic_class, #traffic_class=, #use_pooled_buffers, #use_pooled_buffers=

Constructor Details

#initialize(config = {}) ⇒ HttpClient

Create a new HttpClient



33
34
35
36
# File 'lib/vertx/http.rb', line 33

def initialize(config = {})
  @j_del = org.jruby.jubilee.vertx.JubileeVertx.vertx().createHttpClient()
  self.compression = config[:try_compression] if config.has_key?(:try_compression)
end

Instance Method Details

#closeObject

Close the client. Any unclosed connections will be closed.



261
262
263
# File 'lib/vertx/http.rb', line 261

def close
  @j_del.close
end

#compressionBoolean Also known as: compression?

Tests if client is trying to use HTTP compression or not

Returns:

  • (Boolean)

    true if Client is trying using HTTP compression, false if it doesn’t



47
48
49
# File 'lib/vertx/http.rb', line 47

def compression
  @j_del.getTryUseCompression
end

#compression=(supported) ⇒ Object

Enables HTTP compression. exposes a = method to set or unset compression support.

Parameters:

  • supported (Boolean)

    whether enable compression or not



41
42
43
# File 'lib/vertx/http.rb', line 41

def compression=(supported)
  @j_del.setTryUseCompression(supported)
end

#connect(uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP CONNECT request with the specified uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the CONNECT on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



239
240
241
# File 'lib/vertx/http.rb', line 239

def connect(uri, &hndlr)
  HttpClientRequest.new(@j_del.connect(uri, resp_handler(hndlr)))
end

#connect_web_socket(uri, &hndlr) ⇒ Object

Attempt to connect a WebSocket to the specified URI. The connect is done asynchronously and the handler is called with a WebSocket on success.

Parameters:

  • uri. (String)

    A relative URI where to connect the WebSocket on the host, e.g. /some/path

  • hndlr. (Block)

    The handler to be called with the WebSocket



161
162
163
164
# File 'lib/vertx/http.rb', line 161

def connect_web_socket(uri, &hndlr)
  @j_del.connectWebsocket(uri) { |j_ws| hndlr.call(WebSocket.new(j_ws)) }
  self
end

#delete(uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP DELETE request with the specified uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the DELETE on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



223
224
225
# File 'lib/vertx/http.rb', line 223

def delete(uri, &hndlr)
  HttpClientRequest.new(@j_del.delete(uri, resp_handler(hndlr)))
end

#exception_handler(&hndlr) ⇒ Object

Set the exception handler.

Parameters:

  • hndlr (Block)

    A block to be used as the handler



56
57
58
59
# File 'lib/vertx/http.rb', line 56

def exception_handler(&hndlr)
  @j_del.exceptionHandler(hndlr)
  self
end

#get(uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP GET request with the specified uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the GET on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



191
192
193
# File 'lib/vertx/http.rb', line 191

def get(uri, &hndlr)
  HttpClientRequest.new(@j_del.get(uri, resp_handler(hndlr)))
end

#get_now(uri, headers = nil, &hndlr) ⇒ Object

This is a quick version of the #get method where you do not want to do anything with the request before sending. Normally with any of the HTTP methods you create the request then when you are ready to send it you call Vertx::HttpClientRequest#end on it. With this method the request is immediately sent. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the GET on the server.

  • headers. (Hash)

    A Hash of headers to pass with the request.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



174
175
176
177
# File 'lib/vertx/http.rb', line 174

def get_now(uri, headers = nil, &hndlr)
  @j_del.getNow(uri, headers, resp_handler(hndlr))
  self
end

#head(uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP HEAD request with the specified uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the HEAD on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



199
200
201
# File 'lib/vertx/http.rb', line 199

def head(uri, &hndlr)
  HttpClientRequest.new(@j_del.head(uri, resp_handler(hndlr)))
end

#host(val = nil) ⇒ Object

Get or set host



126
127
128
129
130
131
132
133
# File 'lib/vertx/http.rb', line 126

def host(val = nil)
  if val
    @j_del.setHost(val)
    self
  else
    @j_del.getHost
  end
end

#host=(val) ⇒ Object

Set the host name or ip address that the client will attempt to connect to on the server on.

Parameters:

  • host. (String)

    The host name or ip address to connect to.



120
121
122
123
# File 'lib/vertx/http.rb', line 120

def host=(val)
  @j_del.setHost(val)
  self
end

#keep_alive(val = nil) ⇒ Object

Get or set keep alive



92
93
94
95
96
97
98
99
# File 'lib/vertx/http.rb', line 92

def keep_alive(val = nil)
  if val
    @j_del.setKeepAlive(val)
    self
  else
    @j_del.isKeepAlive
  end
end

#keep_alive=(val) ⇒ Object

If val is true then, after the request has ended the connection will be returned to the pool where it can be used by another request. In this manner, many HTTP requests can be pipe-lined over an HTTP connection. Keep alive connections will not be closed until the #close method is invoked. If val is false then a new connection will be created for each request and it won’t ever go in the pool, the connection will closed after the response has been received. Even with no keep alive, the client will not allow more than #max_pool_size connections to be created at any one time.

Parameters:

  • val. (Boolean)

    The value to use for keep_alive



86
87
88
89
# File 'lib/vertx/http.rb', line 86

def keep_alive=(val)
  @j_del.setTCPKeepAlive(val)
  self
end

#max_pool_size(val = nil) ⇒ Object

Get or set the max pool size



70
71
72
73
74
75
76
77
# File 'lib/vertx/http.rb', line 70

def max_pool_size(val = nil)
  if val
    @j_del.setMaxPoolSize(val)
    self
  else
    @j_del.getMaxPoolSize
  end
end

#max_pool_size=(val) ⇒ Object

Set the maximum pool size. The client will maintain up to this number of HTTP connections in an internal pool

Parameters:

  • val. (FixNum)

    The maximum number of connections (default to 1).



64
65
66
67
# File 'lib/vertx/http.rb', line 64

def max_pool_size=(val)
  @j_del.setMaxPoolSize(val)
  self
end

#max_websocket_frame_sizeint

Gets the max WebSocket Frame size in bytes

Returns:

  • (int)

    Max WebSocket frame size



147
148
149
# File 'lib/vertx/http.rb', line 147

def max_websocket_frame_size
  @j_del.getMaxWebSocketFrameSize
end

#max_websocket_frame_size=(size) ⇒ Object

Sets the maximum WebSocket frame size in bytes. Default is 65536 bytes.

Parameters:

  • size (int)


153
154
155
# File 'lib/vertx/http.rb', line 153

def max_websocket_frame_size=(size)
  @j_del.setMaxWebSocketFrameSize(size)
end

#options(uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP OPTIONS request with the specified uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the OPTIONS on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



183
184
185
# File 'lib/vertx/http.rb', line 183

def options(uri, &hndlr)
  HttpClientRequest.new(@j_del.options(uri, resp_handler(hndlr)))
end

#patch(uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP PATCH request with the specified uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the PATCH on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



247
248
249
# File 'lib/vertx/http.rb', line 247

def patch(uri, &hndlr)
  HttpClientRequest.new(@j_del.patch(uri, resp_handler(hndlr)))
end

#port(val = nil) ⇒ Object

Get or set port



109
110
111
112
113
114
115
116
# File 'lib/vertx/http.rb', line 109

def port(val = nil)
  if val
    @j_del.setPort(val)
    self
  else
    @j_del.getPort
  end
end

#port=(val) ⇒ Object

Set the port that the client will attempt to connect to on the server on. The default value is 80

Parameters:

  • val. (FixNum)

    The port value.



103
104
105
106
# File 'lib/vertx/http.rb', line 103

def port=(val)
  @j_del.setPort(val)
  self
end

#post(uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP POST request with the specified uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the POST on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



207
208
209
# File 'lib/vertx/http.rb', line 207

def post(uri, &hndlr)
  HttpClientRequest.new(@j_del.post(uri, resp_handler(hndlr)))
end

#put(uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP PUT request with the specified uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the PUT on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



215
216
217
# File 'lib/vertx/http.rb', line 215

def put(uri, &hndlr)
  HttpClientRequest.new(@j_del.put(uri, resp_handler(hndlr)))
end

#request(method, uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP request with the specified method and uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • method. (String)

    The HTTP method. Can be one of OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE, CONNECT.

  • uri. (String)

    A relative URI where to perform the OPTIONS on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



256
257
258
# File 'lib/vertx/http.rb', line 256

def request(method, uri, &hndlr)
  HttpClientRequest.new(@j_del.request(method, uri, resp_handler(hndlr)))
end

#trace(uri, &hndlr) ⇒ Object

This method returns an Vertx::HttpClientRequest instance which represents an HTTP TRACE request with the specified uri. When an HTTP response is received from the server the handler is called passing in the response.

Parameters:

  • uri. (String)

    A relative URI where to perform the TRACE on the server.

  • hndlr. (Block)

    The handler to be called with the Vertx::HttpClientResponse



231
232
233
# File 'lib/vertx/http.rb', line 231

def trace(uri, &hndlr)
  HttpClientRequest.new(@j_del.trace(uri, resp_handler(hndlr)))
end

#verify_host(val = nil) ⇒ Object

Get or set verify host



136
137
138
139
140
141
142
143
# File 'lib/vertx/http.rb', line 136

def verify_host(val = nil)
  if val
    @j_del.setVerifyHost(val)
    self
  else
    @j_del.isVerifyHost
  end
end