Class: NetHTTP::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_fileObject

Returns the value of attribute ca_file.



12
13
14
# File 'lib/client/client.rb', line 12

def ca_file
  @ca_file
end

#certObject

Returns the value of attribute cert.



12
13
14
# File 'lib/client/client.rb', line 12

def cert
  @cert
end

#clientObject

Returns the value of attribute client.



12
13
14
# File 'lib/client/client.rb', line 12

def client
  @client
end

#keyObject

Returns the value of attribute key.



12
13
14
# File 'lib/client/client.rb', line 12

def key
  @key
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/client/client.rb', line 12

def logger
  @logger
end

#open_timeoutObject

Returns the value of attribute open_timeout.



12
13
14
# File 'lib/client/client.rb', line 12

def open_timeout
  @open_timeout
end

#pkcs12Object

Returns the value of attribute pkcs12.



12
13
14
# File 'lib/client/client.rb', line 12

def pkcs12
  @pkcs12
end

#pkcs12_fileObject

Returns the value of attribute pkcs12_file.



12
13
14
# File 'lib/client/client.rb', line 12

def pkcs12_file
  @pkcs12_file
end

#pkcs12_passphraseObject

Returns the value of attribute pkcs12_passphrase.



12
13
14
# File 'lib/client/client.rb', line 12

def pkcs12_passphrase
  @pkcs12_passphrase
end

#proxy_uriObject 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

#queryObject (readonly)

Returns the value of attribute query.



12
13
14
# File 'lib/client/client.rb', line 12

def query
  @query
end

#read_timeoutObject

Returns the value of attribute read_timeout.



12
13
14
# File 'lib/client/client.rb', line 12

def read_timeout
  @read_timeout
end

#ssl_pathObject

Returns the value of attribute ssl_path.



12
13
14
# File 'lib/client/client.rb', line 12

def ssl_path
  @ssl_path
end

#uriObject 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_proxyObject (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_sslObject

Returns the value of attribute use_ssl.



12
13
14
# File 'lib/client/client.rb', line 12

def use_ssl
  @use_ssl
end

#verify_modeObject

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
# 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)
  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 => ' + request_opts[:path])
  NetHTTP::Response.new(
    response: client.delete(
      request_opts[:path]
    ),
    logger: logger
  )
end

#get(opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/request/request.rb', line 46

def get(opts = {})
  raise 'Empty GET request options provided.' if opts.empty?

  opts[:method] = 'get'
  request_opts = request_opts(opts)
  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 => ' + request_opts[:path])
  logger.debug('Request Headers =>')
  logger.debug(request_opts[:headers])
  NetHTTP::Response.new(
    response: client.get(
      request_opts[:path],
      request_opts[:headers]
    ),
    logger: logger
  )
end

#post(opts = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/request/request.rb', line 66

def post(opts = {})
  raise 'Empty POST request options provided.' if opts.empty?

  opts[:method] = 'post'
  request_opts = request_opts(opts)
  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 => ' + request_opts[: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(
      request_opts[:path],
      request_opts[:body],
      request_opts[:headers]
    ),
    logger: logger
  )
end

#post_form(opts = {}) ⇒ Object Also known as: post_form_data



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/request/request.rb', line 89

def post_form(opts = {})
  raise 'Empty POST_FORM request options provided.' if opts.empty?

  opts[:method] = 'post_form'
  request_opts = request_opts(opts)
  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 => ' + request_opts[: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(
      uri,
      URI.encode_www_form(request_opts[:body]),
      request_opts[:headers]
    ),
    logger: logger
  )
end

#put(opts = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/request/request.rb', line 114

def put(opts = {})
  raise 'Empty PUT request options provided.' if opts.empty?

  opts[:method] = 'put'
  request_opts = request_opts(opts)
  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 => ' + request_opts[: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(
      request_opts[:path],
      request_opts[:body],
      request_opts[:headers]
    ),
    logger: logger
  )
end

#request_opts(opts) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/request/request.rb', line 137

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 = request_path(
    path: (opts[:path] || request_uri.path || path),
    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,
    headers: request_headers,
    body: request_body
  }
end

#request_path(opts) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/request/request.rb', line 162

def request_path(opts)
  # i.e. URI('https://www.google.com')
  # uri.path defaults to "" if empty / not set
  # uri.query defaults to nil if empty / not set
  path = opts[:path].to_s.chomp('?')
  if opts[:query].to_s.start_with?('?')
    query = opts[:query][1..-1]
  else
    query = opts[:query].to_s
  end

  return '' if path.empty? && query.empty?
  return path if query.empty?
  return '/' + query if path.empty?

  path + '?' + query
end