Class: Stream::StreamHTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/stream/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_generator) ⇒ StreamHTTPClient

Returns a new instance of StreamHTTPClient.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/stream/client.rb', line 168

def initialize(url_generator)
  @options = url_generator.options
  @conn = Faraday.new(url: url_generator.url) do |faraday|
    faraday.use RaiseHttpException
    faraday.options[:open_timeout] = @options[:default_timeout]
    faraday.options[:timeout] = @options[:default_timeout]
    faraday.adapter :net_http_persistent, pool_size: 5 do |http|
      # AWS load balancer idle timeout is 60 secs, so let's make it 59
      http.idle_timeout = 59
    end
  end
  @base_path = url_generator.base_path
  @conn.path_prefix = base_path
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



166
167
168
# File 'lib/stream/client.rb', line 166

def base_path
  @base_path
end

#connObject (readonly)

Returns the value of attribute conn.



164
165
166
# File 'lib/stream/client.rb', line 164

def conn
  @conn
end

#optionsObject (readonly)

Returns the value of attribute options.



165
166
167
# File 'lib/stream/client.rb', line 165

def options
  @options
end

Instance Method Details

#make_http_request(method, relative_url, params = nil, data = nil, headers = nil) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/stream/client.rb', line 183

def make_http_request(method, relative_url, params = nil, data = nil, headers = nil)
  headers['Content-Type'] = 'application/json'
  headers['X-Stream-Client'] = "stream-ruby-client-#{Stream::VERSION}"
  base_url = [base_path, relative_url].join('/').gsub(%r{/+}, '/')
  url = "#{base_url}?#{URI.encode_www_form(params)}"
  body = data.to_json if %w[post put].include? method.to_s
  response = @conn.run_request(
    method,
    url,
    body,
    headers
  )

  case response[:status].to_i
  when 200..203
    ::JSON.parse(response[:body])
  end
end