Class List Method List File List

# Class: Vertx::HttpClient

Inherits:
  Object
  - Object
  - Vertx::HttpClient
  show all
Includes:
  SSLSupport, TCPSupport
Defined in:
  src/main/ruby_scripts/core/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 HTML5 websockets.

Author:

- Tim Fox

## Instance Method Summary (collapse)

- - (Object) close
  Close the client.
- - (Object) connect(uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP CONNECT request with the specified uri.
- - (Object) connect_web_socket(uri, &hndlr)
  Attempt to connect an HTML5 websocket to the specified URI.
- - (Object) delete(uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP DELETE request with the specified uri.
- - (Object) exception_handler(proc = nil, &hndlr)
  Set the exception handler.
- - (Object) get(uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP GET request with the specified uri.
- - (Object) get_now(uri, headers = nil, &hndlr)
  This is a quick version of the #get method where you do not want to do anything with the request before sending.
- - (Object) head(uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP HEAD request with the specified uri.
- - (Object) host=(val)
  Set the host name or ip address that the client will attempt to connect to on the server on.
- - (HttpClient) initialize constructor
  Create a new HttpClient.
- - (Object) keep_alive=(val)
  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.
- - (FixNum) max_pool_size
  The maxium number of connections this client will pool.
- - (Object) max_pool_size=(val)
  Set the maximum pool size.
- - (Object) options(uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP OPTIONS request with the specified uri.
- - (Object) patch(uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP PATCH request with the specified uri.
- - (Object) port=(val)
  Set the port that the client will attempt to connect to on the server on.
- - (Object) post(uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP POST request with the specified uri.
- - (Object) put(uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP PUT request with the specified uri.
- - (Object) request(method, uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP request with the specified method and uri.
- - (Object) trace(uri, &hndlr)
  This method returns an HttpClientRequest instance which represents an HTTP TRACE request with the specified uri.
- - (Object) trust_all=(val)
  Should the client trust ALL server certificates? against it's local client trust store.

### Methods included from SSLSupport

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

### Methods included from TCPSupport

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

## Constructor Details

### - (`HttpClient`) initialize

Create a new HttpClient

    96
    97
    98

    # File 'src/main/ruby_scripts/core/http.rb', line 96

    def initialize
      @j_del = org.vertx.java.deploy.impl.VertxLocator.vertx.createHttpClient
    end

## Instance Method Details

### - (`Object`) close

Close the client. Any unclosed connections will be closed.

    259
    260
    261

    # File 'src/main/ruby_scripts/core/http.rb', line 259

    def close
      @j_del.close
    end

### - (`Object`) connect(uri, &hndlr)

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

    237
    238
    239

    # File 'src/main/ruby_scripts/core/http.rb', line 237

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

### - (`Object`) connect_web_socket(uri, &hndlr)

Attempt to connect an HTML5 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

    # File 'src/main/ruby_scripts/core/http.rb', line 161

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

### - (`Object`) delete(uri, &hndlr)

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

    221
    222
    223

    # File 'src/main/ruby_scripts/core/http.rb', line 221

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

### - (`Object`) exception_handler(proc = nil, &hndlr)

Set the exception handler.

Parameters:

- proc (`Proc`) (defaults to: `nil`) —
  A proc to be used as the handler
- hndlr (`Block`) —
  A block to be used as the handler

    103
    104
    105
    106
    107

    # File 'src/main/ruby_scripts/core/http.rb', line 103

    def exception_handler(proc = nil, &hndlr)
      hndlr = proc if proc
      @j_del.exceptionHandler(hndlr)
      self
    end

### - (`Object`) get(uri, &hndlr)

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

    189
    190
    191

    # File 'src/main/ruby_scripts/core/http.rb', line 189

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

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

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

    173
    174
    175

    # File 'src/main/ruby_scripts/core/http.rb', line 173

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

### - (`Object`) head(uri, &hndlr)

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

    197
    198
    199

    # File 'src/main/ruby_scripts/core/http.rb', line 197

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

### - (`Object`) host=(val)

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.

    152
    153
    154
    155

    # File 'src/main/ruby_scripts/core/http.rb', line 152

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

### - (`Object`) keep_alive=(val)

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

    129
    130
    131
    132

    # File 'src/main/ruby_scripts/core/http.rb', line 129

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

### - (`FixNum`) max_pool_size

The maxium number of connections this client will pool.

Returns:

- (`FixNum`) —
  The maxium number of connections this client will pool.

    118
    119
    120

    # File 'src/main/ruby_scripts/core/http.rb', line 118

    def max_pool_size
      @j_del.getMaxPoolSize
    end

### - (`Object`) max_pool_size=(val)

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).

    112
    113
    114
    115

    # File 'src/main/ruby_scripts/core/http.rb', line 112

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

### - (`Object`) options(uri, &hndlr)

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

    181
    182
    183

    # File 'src/main/ruby_scripts/core/http.rb', line 181

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

### - (`Object`) patch(uri, &hndlr)

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

    245
    246
    247

    # File 'src/main/ruby_scripts/core/http.rb', line 245

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

### - (`Object`) port=(val)

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.

    145
    146
    147
    148

    # File 'src/main/ruby_scripts/core/http.rb', line 145

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

### - (`Object`) post(uri, &hndlr)

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

    205
    206
    207

    # File 'src/main/ruby_scripts/core/http.rb', line 205

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

### - (`Object`) put(uri, &hndlr)

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

    213
    214
    215

    # File 'src/main/ruby_scripts/core/http.rb', line 213

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

### - (`Object`) request(method, uri, &hndlr)

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

    254
    255
    256

    # File 'src/main/ruby_scripts/core/http.rb', line 254

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

### - (`Object`) trace(uri, &hndlr)

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

    229
    230
    231

    # File 'src/main/ruby_scripts/core/http.rb', line 229

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

### - (`Object`) trust_all=(val)

Should the client trust ALL server certificates? against it's local client trust store. The default value is false. Use this method with caution!

Parameters:

- val. (`Boolean`) —
  If val is set to true then the client will trust ALL server certificates and will not attempt to authenticate them

    138
    139
    140
    141

    # File 'src/main/ruby_scripts/core/http.rb', line 138

    def trust_all=(val)
      @j_del.setTrustAll(val)
      self
    end