Class: NetHTTP::Client
- Inherits:
-
Object
- Object
- NetHTTP::Client
- Defined in:
- lib/client/ext.rb,
lib/client/client.rb,
lib/client/schema.rb,
lib/request/request.rb
Defined Under Namespace
Classes: SchemaError
Constant Summary collapse
- Schema =
Dry::Validation.Schema do configure do def name 'NetHTTP::Client::Schema' end def true_or_false [true, false] end end optional(:ca_file).filled(type?: String) optional(:host).maybe(type?: String) optional(:logger).filled optional(:open_timeout).filled(type?: Integer) optional(:path).maybe(type?: String) optional(:port).maybe(type?: Integer) optional(:pkcs12_file).filled(type?: String) optional(:pkcs12_passphrase).filled(type?: String) optional(:proxy_uri_host).maybe(type?: String) optional(:proxy_uri_path).maybe(type?: String) optional(:proxy_uri_port).maybe(type?: Integer) optional(:proxy_uri_scheme).maybe(type?: String) optional(:proxy_uri).filled(type?: String) optional(:proxy_url).filled(type?: String) optional(:query).filled(type?: String) optional(:read_timeout).filled(type?: Integer) optional(:scheme).maybe(type?: String) optional(:ssl_path).filled(type?: String) optional(:uri).maybe(type?: String) optional(:url).maybe(type?: String) optional(:use_ssl).filled(included_in?: true_or_false) optional(:verify_mode).filled rule(if_url_and_uri_are_nil_must_provide_host: [:uri, :url, :host]) do |uri, url, host| uri.empty?.then(url.empty?.then(host.filled?)) end rule(if_url_and_uri_are_nil_must_provide_scheme_or_port: [:uri, :url, :scheme, :port]) do |uri, url, scheme, port| uri.empty?.then(url.empty?.then(scheme.empty?.then(port.filled?))) uri.empty?.then(url.empty?.then(port.empty?.then(scheme.filled?))) end end
Instance Attribute Summary collapse
-
#ca_file ⇒ Object
Returns the value of attribute ca_file.
-
#cert ⇒ Object
Returns the value of attribute cert.
-
#client ⇒ Object
Returns the value of attribute client.
-
#key ⇒ Object
Returns the value of attribute key.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
-
#pkcs12 ⇒ Object
Returns the value of attribute pkcs12.
-
#pkcs12_file ⇒ Object
Returns the value of attribute pkcs12_file.
-
#pkcs12_passphrase ⇒ Object
Returns the value of attribute pkcs12_passphrase.
-
#proxy_uri ⇒ Object
(also: #proxy_url)
Returns the value of attribute proxy_uri.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
-
#ssl_path ⇒ Object
Returns the value of attribute ssl_path.
-
#uri ⇒ Object
(also: #url)
Returns the value of attribute uri.
-
#use_proxy ⇒ Object
readonly
Returns the value of attribute use_proxy.
-
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
-
#verify_mode ⇒ Object
Returns the value of attribute verify_mode.
Instance Method Summary collapse
- #call_web_service(request_opts = {}) ⇒ Object
- #delete(opts = {}) ⇒ Object
- #get(opts = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #post(opts = {}) ⇒ Object
- #post_form(opts = {}) ⇒ Object (also: #post_form_data)
- #put(opts = {}) ⇒ Object
- #request_opts(opts) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/client/client.rb', line 33 def initialize(opts = {}) send('logger=', opts[:logger]) schema_results = Core.schema_validation(opts, NetHTTP::Client::Schema) unless opts[:enforce_schema_validation] == false unless schema_results.nil? logger.debug("NetHTTP::Client::SchemaError -> #{schema_results}") raise NetHTTP::Client::SchemaError.new(schema_results.to_s) end send('uri=', opts) send('client=', opts) end |
Instance Attribute Details
#ca_file ⇒ Object
Returns the value of attribute ca_file.
12 13 14 |
# File 'lib/client/client.rb', line 12 def ca_file @ca_file end |
#cert ⇒ Object
Returns the value of attribute cert.
12 13 14 |
# File 'lib/client/client.rb', line 12 def cert @cert end |
#client ⇒ Object
Returns the value of attribute client.
12 13 14 |
# File 'lib/client/client.rb', line 12 def client @client end |
#key ⇒ Object
Returns the value of attribute key.
12 13 14 |
# File 'lib/client/client.rb', line 12 def key @key end |
#logger ⇒ Object
Returns the value of attribute logger.
12 13 14 |
# File 'lib/client/client.rb', line 12 def logger @logger end |
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
12 13 14 |
# File 'lib/client/client.rb', line 12 def open_timeout @open_timeout end |
#pkcs12 ⇒ Object
Returns the value of attribute pkcs12.
12 13 14 |
# File 'lib/client/client.rb', line 12 def pkcs12 @pkcs12 end |
#pkcs12_file ⇒ Object
Returns the value of attribute pkcs12_file.
12 13 14 |
# File 'lib/client/client.rb', line 12 def pkcs12_file @pkcs12_file end |
#pkcs12_passphrase ⇒ Object
Returns the value of attribute pkcs12_passphrase.
12 13 14 |
# File 'lib/client/client.rb', line 12 def pkcs12_passphrase @pkcs12_passphrase end |
#proxy_uri ⇒ Object Also known as: proxy_url
Returns the value of attribute proxy_uri.
12 13 14 |
# File 'lib/client/client.rb', line 12 def proxy_uri @proxy_uri end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
12 13 14 |
# File 'lib/client/client.rb', line 12 def query @query end |
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
12 13 14 |
# File 'lib/client/client.rb', line 12 def read_timeout @read_timeout end |
#ssl_path ⇒ Object
Returns the value of attribute ssl_path.
12 13 14 |
# File 'lib/client/client.rb', line 12 def ssl_path @ssl_path end |
#uri ⇒ Object Also known as: url
Returns the value of attribute uri.
12 13 14 |
# File 'lib/client/client.rb', line 12 def uri @uri end |
#use_proxy ⇒ Object (readonly)
Returns the value of attribute use_proxy.
12 13 14 |
# File 'lib/client/client.rb', line 12 def use_proxy @use_proxy end |
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
12 13 14 |
# File 'lib/client/client.rb', line 12 def use_ssl @use_ssl end |
#verify_mode ⇒ Object
Returns the value of attribute verify_mode.
12 13 14 |
# File 'lib/client/client.rb', line 12 def verify_mode @verify_mode end |
Instance Method Details
#call_web_service(request_opts = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/request/request.rb', line 9 def call_web_service(request_opts = {}) @response = case request_opts[:method].to_s.upcase when 'DELETE' delete(request_opts) when 'GET' get(request_opts) when 'POST' post(request_opts) when 'POST_FORM', 'POST_FORM_DATA' post_form(request_opts) when 'PUT' put(request_opts) else logger.debug("Request method => '#{request_opts[:method]}' not yet supported.") raise "Request method => '#{request_opts[:method]}' not yet supported." end @response end |
#delete(opts = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/request/request.rb', line 29 def delete(opts = {}) raise 'Empty DELETE request options provided.' if opts.empty? opts[:method] = 'delete' request_opts = request_opts(opts) path = if request_opts[:query] request_opts[:path] + request_opts[:query] else request_opts[:path] end logger.debug('Request Method => ' + request_opts[:method].to_s.upcase) logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s) logger.debug('Request Port => ' + request_opts[:uri].port.to_s) logger.debug('Request Path => ' + path) NetHTTP::Response.new( response: client.delete( path ), logger: logger ) end |
#get(opts = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/request/request.rb', line 52 def get(opts = {}) raise 'Empty GET request options provided.' if opts.empty? opts[:method] = 'get' request_opts = request_opts(opts) path = if request_opts[:query] request_opts[:path] + request_opts[:query] else request_opts[:path] end logger.debug('Request Method => ' + request_opts[:method].to_s.upcase) logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s) logger.debug('Request Port => ' + request_opts[:uri].port.to_s) logger.debug('Request Path => ' + path) logger.debug('Request Headers =>') logger.debug(request_opts[:headers]) NetHTTP::Response.new( response: client.get( path, request_opts[:headers] ), logger: logger ) end |
#post(opts = {}) ⇒ Object
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 |
# File 'lib/request/request.rb', line 78 def post(opts = {}) raise 'Empty POST request options provided.' if opts.empty? opts[:method] = 'post' request_opts = request_opts(opts) path = if request_opts[:query] request_opts[:path] + request_opts[:query] else request_opts[:path] end logger.debug('Request Method => ' + request_opts[:method].to_s.upcase) logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s) logger.debug('Request Port => ' + request_opts[:uri].port.to_s) logger.debug('Request Path => ' + path) logger.debug('Request Headers =>') logger.debug(request_opts[:headers]) logger.debug('Request Body =>') logger.debug(request_opts[:body]) NetHTTP::Response.new( response: client.post( path, request_opts[:body], request_opts[:headers] ), logger: logger ) end |
#post_form(opts = {}) ⇒ Object Also known as: post_form_data
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/request/request.rb', line 107 def post_form(opts = {}) raise 'Empty POST_FORM request options provided.' if opts.empty? opts[:method] = 'post_form' request_opts = request_opts(opts) path = if request_opts[:query] request_opts[:path] + request_opts[:query] else request_opts[:path] end logger.debug('Request Method => ' + request_opts[:method].to_s.upcase) logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s) logger.debug('Request Port => ' + request_opts[:uri].port.to_s) logger.debug('Request Path => ' + path) logger.debug('Request Headers =>') logger.debug(request_opts[:headers]) logger.debug('Request Body =>') logger.debug(URI.encode_www_form(request_opts[:body])) NetHTTP::Response.new( response: client.post( path, URI.encode_www_form(request_opts[:body]), request_opts[:headers] ), logger: logger ) end |
#put(opts = {}) ⇒ Object
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 |
# File 'lib/request/request.rb', line 138 def put(opts = {}) raise 'Empty PUT request options provided.' if opts.empty? opts[:method] = 'put' request_opts = request_opts(opts) path = if request_opts[:query] request_opts[:path] + request_opts[:query] else request_opts[:path] end logger.debug('Request Method => ' + request_opts[:method].to_s.upcase) logger.debug('Request Host => ' + request_opts[:uri].scheme.to_s + '://' + request_opts[:uri].host.to_s) logger.debug('Request Port => ' + request_opts[:uri].port.to_s) logger.debug('Request Path => ' + path) logger.debug('Request Headers =>') logger.debug(request_opts[:headers]) logger.debug('Request Body =>') logger.debug(request_opts[:body]) NetHTTP::Response.new( response: client.put( path, request_opts[:body], request_opts[:headers] ), logger: logger ) end |
#request_opts(opts) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/request/request.rb', line 167 def request_opts(opts) schema_results = Core.schema_validation(opts, NetHTTP::Request::Schema) unless opts[:enforce_schema_validation] == false unless schema_results.nil? logger.debug("NetHTTP::Request::SchemaError -> #{schema_results}") raise NetHTTP::Request::SchemaError.new(schema_results.to_s) end request_method = opts[:method] ||= 'post' request_uri = Core::Utilities.parse_uri(opts[:uri] || opts[:url] || uri) request_path = (opts[:path] || request_uri.path || path).chomp('?') request_query = Core::Utilities.parse_query((opts[:query] || request_uri.query || query)) request_headers = opts[:headers] ||= {} request_body = opts[:body] ||= nil { method: request_method, uri: request_uri, path: request_path, query: request_query, headers: request_headers, body: request_body } end |