Class: Rets::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rets/http_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http, options, logger, login_url) ⇒ HttpClient

Returns a new instance of HttpClient.



8
9
10
11
12
13
14
# File 'lib/rets/http_client.rb', line 8

def initialize(http, options, logger, )
  @http = http
  @options = options
  @logger = logger
  @login_url = 
  @options.fetch(:ca_certs, []).each {|c| @http.ssl_config.add_trust_ca(c) }
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



6
7
8
# File 'lib/rets/http_client.rb', line 6

def http
  @http
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/rets/http_client.rb', line 6

def logger
  @logger
end

#login_urlObject (readonly)

Returns the value of attribute login_url.



6
7
8
# File 'lib/rets/http_client.rb', line 6

def 
  @login_url
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/rets/http_client.rb', line 6

def options
  @options
end

Class Method Details



49
50
51
52
53
# File 'lib/rets/http_client.rb', line 49

def self.ensure_cookie_store_exists!(cookie_store)
  unless File.exist? cookie_store
    FileUtils.touch(cookie_store)
  end
end

.from_options(options, logger) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rets/http_client.rb', line 16

def self.from_options(options, logger)
  if options[:http_proxy]
    http = HTTPClient.new(options.fetch(:http_proxy))

    if options[:proxy_username]
      http.set_proxy_auth(options.fetch(:proxy_username), options.fetch(:proxy_password))
    end
  else
    http = HTTPClient.new
  end

  if options[:receive_timeout]
    http.receive_timeout = options[:receive_timeout]
  end

  if options[:cookie_store]
    ensure_cookie_store_exists! options[:cookie_store]
    http.set_cookie_store(options[:cookie_store])
  end

  http_client = new(http, options, logger, options[:login_url])

  if options[:http_timing_stats_collector]
    http_client = Rets::MeasuringHttpClient.new(http_client, options.fetch(:http_timing_stats_collector), options.fetch(:http_timing_stats_prefix))
  end

  if options[:lock_around_http_requests]
    http_client = Rets::LockingHttpClient.new(http_client, options.fetch(:locker), options.fetch(:lock_name), options.fetch(:lock_options))
  end

  http_client
end

Instance Method Details



123
124
125
126
127
128
129
130
131
# File 'lib/rets/http_client.rb', line 123

def http_cookie(name)
  @http.cookie_manager.cookies().each do |c|
    if c.name.downcase == name.downcase
      return c.value
    end
  end

  nil
end

#http_get(url, params = nil, extra_headers = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/rets/http_client.rb', line 55

def http_get(url, params=nil, extra_headers={})
  http.set_auth(url, options[:username], options[:password])
  headers = extra_headers.merge(rets_extra_headers)
  res = nil
  log_http_traffic("GET", url, params, headers) do
    res = http.get(url, params, headers)
  end
  Parser::ErrorChecker.check(res)
  res
end

#http_post(url, params, extra_headers = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/rets/http_client.rb', line 66

def http_post(url, params, extra_headers = {})
  http.set_auth(url, options[:username], options[:password])
  headers = extra_headers.merge(rets_extra_headers)
  res = nil
  log_http_traffic("POST", url, params, headers) do
    res = http.post(url, params, headers)
  end
  Parser::ErrorChecker.check(res)
  res
end

#log_http_traffic(method, url, params, headers, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rets/http_client.rb', line 77

def log_http_traffic(method, url, params, headers, &block)
  # optimization, we don't want to compute log params
  # if logging is off
  if logger.debug?
    logger.debug "Rets::Client >> #{method} #{url}"
    logger.debug "Rets::Client >> params = #{params.inspect}"
    logger.debug "Rets::Client >> headers = #{headers.inspect}"
  end

  res = block.call

  # optimization, we don't want to compute log params
  # if logging is off, especially when there is a loop just
  # for logging
  if logger.debug?
    logger.debug "Rets::Client << Status #{res.status_code}"
    res.headers.each { |k, v| logger.debug "Rets::Client << #{k}: #{v}" }
  end
end

#rets_extra_headersObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rets/http_client.rb', line 104

def rets_extra_headers
  user_agent = options[:agent] || "Client/1.0"
  rets_version = options[:version] || "RETS/1.7.2"

  headers = {
    "User-Agent"   => user_agent,
    "RETS-Version" => rets_version
  }

  if options[:ua_password]
    up = Digest::MD5.hexdigest "#{user_agent}:#{options[:ua_password]}"
    session_id = http_cookie('RETS-Session-ID') || ''
    digest = Digest::MD5.hexdigest "#{up}::#{session_id}:#{rets_version}"
    headers.merge!("RETS-UA-Authorization" => "Digest #{digest}")
  end

  headers
end


97
98
99
100
101
102
# File 'lib/rets/http_client.rb', line 97

def save_cookie_store
  if options[:cookie_store]
    #save session cookies
    @http.cookie_manager.save_cookies(true)
  end
end