Class: Appium::Core::Base::Http::Default

Inherits:
Selenium::WebDriver::Remote::Http::Default
  • Object
show all
Defined in:
lib/appium_lib_core/common/base/http_default.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Accept' => CONTENT_TYPE,
  'Content-Type' => "#{CONTENT_TYPE}; charset=UTF-8",
  'User-Agent' =>
    "appium/ruby_lib_core/#{VERSION} (#{::Selenium::WebDriver::Remote::Http::Common::DEFAULT_HEADERS['User-Agent']})"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(open_timeout: nil, read_timeout: nil) ⇒ Default

Returns a new instance of Default.


39
40
41
42
43
# File 'lib/appium_lib_core/common/base/http_default.rb', line 39

def initialize(open_timeout: nil, read_timeout: nil)
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @additional_headers = {}
end

Instance Attribute Details

#additional_headersObject (readonly)

Returns the value of attribute additional_headers


37
38
39
# File 'lib/appium_lib_core/common/base/http_default.rb', line 37

def additional_headers
  @additional_headers
end

Instance Method Details

#call(verb, url, command_hash) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/appium_lib_core/common/base/http_default.rb', line 81

def call(verb, url, command_hash)
  url      = server_url.merge(url) unless url.is_a?(URI)
  headers  = DEFAULT_HEADERS.dup
  headers  = headers.merge @additional_headers unless @additional_headers.empty?
  headers['Cache-Control'] = 'no-cache' if verb == :get

  if command_hash
    payload                   = JSON.generate(command_hash)
    headers['Content-Length'] = payload.bytesize.to_s if [:post, :put].include?(verb)
  elsif verb == :post
    payload = '{}'
    headers['Content-Length'] = '2'
  end

  ::Appium::Logger.info("   >>> #{url} | #{payload}")
  ::Appium::Logger.info("     > #{headers.inspect}")

  request verb, url, headers, payload
end

#update_sending_request_to(scheme:, host:, port:, path:) ⇒ URI

Update server_url provided when ruby_lib _core created a default http client. Set @http as nil to re-create http client for the server_url

Parameters:

  • scheme (string)

    A scheme to update server_url to

  • host (string)

    A host to update server_url to

  • port (string|integer)

    A port number to update server_url to

  • path (string)

    A path to update server_url to

Returns:

  • (URI)

    An instance of URI updated to. Returns default server_url if some of arguments are nil


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/appium_lib_core/common/base/http_default.rb', line 54

def update_sending_request_to(scheme:, host:, port:, path:)
  return @server_url unless validate_url_param(scheme, host, port, path)

  ::Appium::Logger.debug("[experimental] This feature, #{__method__}, is an experimental")

  # Add / if 'path' does not have it
  path = path.start_with?('/') ? path : "/#{path}"
  path = path.end_with?('/') ? path : "#{path}/"

  @http = nil
  @server_url = URI.parse "#{scheme}://#{host}:#{port}#{path}"
end