Class: Applitools::Connectivity::ServerConnector
  
  
  
  
  
    - Inherits:
 
    - 
      Object
      
        
          - Object
 
          
            - Applitools::Connectivity::ServerConnector
 
          
        
        show all
      
     
  
  
  
  
  
      - Extended by:
 
      - Helpers
 
  
  
  
  
  
  
  
  
    - Defined in:
 
    - lib/applitools/connectivity/server_connector.rb
 
  
  
 
Defined Under Namespace
  
    
  
    
      Classes: ScreenshotUploadError
    
  
  
    
      Constant Summary
      collapse
    
    
      
        - DEFAULT_SERVER_URL =
          
        
 
        'https://eyesapi.applitools.com'.freeze
 
      
        - SSL_CERT =
          
        
 
        File.join(File.dirname(File.expand_path(__FILE__)), '../../../certs/cacert.pem').to_s.freeze
 
      
        - DEFAULT_TIMEOUT =
          
        
 
        300_000
 
      
        - API_SESSIONS =
          
        
 
        '/api/sessions'.freeze
 
      
        - API_SESSIONS_RUNNING =
          
        
 
        API_SESSIONS + '/running/'.freeze
 
      
        - API_SINGLE_TEST =
          
        
 
        API_SESSIONS + '/'.freeze
 
      
        - RENDER_INFO_PATH =
          
        
 
        API_SESSIONS + '/renderinfo'.freeze
 
      
        - RENDER =
          
        
 
        '/render'.freeze
 
      
        - RESOURCES_SHA_256 =
          
        
 
        '/resources/sha256/'.freeze
 
      
        - RENDER_STATUS =
          
        
 
        '/render-status'.freeze
 
      
        - CLOSE_BATCH =
          
        
 
        '/api/sessions/batches/%s/close/bypointerid'.freeze
 
      
        - HTTP_STATUS_CODES =
          
        
 
        {
  created: 201,
  accepted: 202,
  ok: 200,
  gone: 410
}.freeze 
      
        - RETRY_DELAY =
          
        
 
        0.5
 
      
        - RETRY_STEP_FACTOR =
          
        
 
        1.5
 
      
        - RETRY_MAX_DELAY =
          
        
 
        5
 
      
    
  
  Class Attribute Summary collapse
  
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
      
        - 
  
    
      #agent_id  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #close_batch(batch_id)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #download_resource(url, ua_string = nil, cookies = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #initialize(url = nil)  ⇒ ServerConnector 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of ServerConnector.
 
  
 
      
        - 
  
    
      #match_single_window(data)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #match_single_window_data(data)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #match_window(session, data)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #obtain_agent_id(&block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #post_dom_json(dom_data, rendering_info)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #put_data(rendering_info, data, content_type, log_message_entity)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #put_dom(rendering_info, dom_data)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #put_screenshot(rendering_info, screenshot)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #render(service_url, access_key, requests)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #render_put_resource(service_url, access_key, resource, render)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #render_status_by_id(service_url, access_key, running_renders_json)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #rendering_info  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #set_proxy(uri, user = nil, password = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #start_session(session_start_info)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
        - 
  
    
      #stop_session(session, aborted = nil, save = false)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
 
      
    
  
  
  
  
  
  
  
  
  
  Methods included from Helpers
  abstract_attr_accessor, abstract_method, env_variable, environment_attribute, environment_variables
  Constructor Details
  
    
  
  
    
Returns a new instance of ServerConnector.
   
 
  
  
    
      
49
50
51 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 49
def initialize(url = nil)
  self.server_url = url
end 
     | 
  
 
  
 
  
    Class Attribute Details
    
      
      
      
  
  
    .connection_timeout  ⇒ Object 
  
  
  
  
    
Returns the value of attribute connection_timeout.
   
 
  
  
    
      
16
17
18 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 16
def connection_timeout
  @connection_timeout
end 
     | 
  
 
    
      
      
      
  
  
    .faraday_adapter  ⇒ Object 
  
  
  
  
    
Returns the value of attribute faraday_adapter.
   
 
  
  
    
      
16
17
18 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 16
def faraday_adapter
  @faraday_adapter
end 
     | 
  
 
    
   
  
    Instance Attribute Details
    
      
      
      
  
  
    #agent_id_proc  ⇒ Object  
  
  
  
  
    
Returns the value of attribute agent_id_proc.
   
 
  
  
    
      
46
47
48 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 46
def agent_id_proc
  @agent_id_proc
end 
     | 
  
 
    
      
      
      
  
  
    #endpoint_url  ⇒ Object  
  
  
  
  
    
Returns the value of attribute endpoint_url.
   
 
  
  
    
      
44
45
46 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 44
def endpoint_url
  @endpoint_url
end 
     | 
  
 
    
      
      
      
  
  
    #proxy  ⇒ Object 
  
  
  
  
    
Returns the value of attribute proxy.
   
 
  
  
    
      
45
46
47 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 45
def proxy
  @proxy
end 
     | 
  
 
    
      
      
      
  
  
    #server_url  ⇒ Object 
  
  
  
  
    
Returns the value of attribute server_url.
   
 
  
  
    
      
43
44
45 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 43
def server_url
  @server_url
end 
     | 
  
 
    
   
  
    Instance Method Details
    
      
  
  
    #agent_id  ⇒ Object 
  
  
  
  
    
      
57
58
59
60
61 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 57
def agent_id
  return agent_id_proc.call if agent_id_proc
  Applitools::EyesLogger.error('The agent id is not set!')
  "eyes.sdk.ruby/#{Applitools::VERSION}"
end
     | 
  
 
    
      
  
  
    #close_batch(batch_id)  ⇒ Object 
  
  
  
  
    
      
186
187
188
189
190
191
192
193
194
195
196
197 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 186
def close_batch(batch_id)
  if 'true'.casecmp(ENV['APPLITOOLS_DONT_CLOSE_BATCHES'] || '') == 0
    return Applitools::EyesLogger.info(
      'APPLITOOLS_DONT_CLOSE_BATCHES environment variable set to true. Doing nothing.'
    ) && false
  end
  Applitools::ArgumentGuard.not_nil(batch_id, 'batch_id')
  Applitools::EyesLogger.info("Called with #{batch_id}")
  url = CLOSE_BATCH % batch_id
  response = delete(URI.join(endpoint_url, url))
  Applitools::EyesLogger.info "delete batch is done with #{response.status} status"
end
     | 
  
 
    
      
  
  
    #download_resource(url, ua_string = nil, cookies = nil)  ⇒ Object 
  
  
  
  
    
      
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 170
def download_resource(url, ua_string = nil, cookies=nil)
  Applitools::EyesLogger.debug "Fetching #{url}..."
  resp_proc = proc do |u|
    faraday_connection(u, false).send(:get) do |req|
      req.options.timeout = self.class.connection_timeout || DEFAULT_TIMEOUT
      req.[:accept_encoding] = 'identity'
      req.[:accept_language] = '*'
      req.[:user_agent] = ua_string if ua_string
      req.[:Cookie] = cookies.map { |h| [h[:name], h[:value]].join('=') }.join('; ') unless cookies.to_a.empty?
    end
  end
  response = resp_proc.call(url)
  Applitools::EyesLogger.debug "Done. (#{url} #{response.status})"
  response
end
     | 
  
 
    
      
  
  
    #match_single_window(data)  ⇒ Object 
  
  
  
  
    
      
277
278
279
280 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 277
def match_single_window(data)
  res = match_single_window_data(data)
  Applitools::TestResults.new Oj.load(res.body)
end 
     | 
  
 
    
      
  
  
    #match_single_window_data(data)  ⇒ Object 
  
  
  
  
    
      
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 249
def match_single_window_data(data)
    json_data = Oj.dump(data.to_hash).force_encoding('BINARY')
  body = [json_data.length].pack('L>') + json_data + data.screenshot
    begin
    Applitools::EyesLogger.debug 'Sending match data...'
    res = long_post(
      @single_check_endpoint_url,
      content_type: 'application/octet-stream',
      body: body,
      query: { agent_id: data.agent_id }
    )
  rescue Errno::EWOULDBLOCK, Faraday::ConnectionFailed
    @delays ||= request_delay(RETRY_DELAY, RETRY_STEP_FACTOR, RETRY_MAX_DELAY)
    begin
      sleep @delays.next
    rescue StopIteration
      raise Applitools::UnknownNetworkStackError.new('Unknown network stack error')
    end
    res = match_single_window_data(data)
  ensure
    @delays = nil
  end
  raise Applitools::EyesError.new("Request failed: #{res.status} #{res.} #{res.body}") unless res.success?
  res
end
     | 
  
 
    
      
  
  
    #match_window(session, data)  ⇒ Object 
  
  
  
  
    
      
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 225
def match_window(session, data)
    json_data = Oj.dump(Applitools::Utils.camelcase_hash_keys(data.to_hash)).force_encoding('BINARY')
  body = if data.screenshot && data.app_output.screenshot_url.nil?
           content_type = 'application/octet-stream'
           [json_data.length].pack('L>') + json_data + data.screenshot
         else
           content_type = 'application/json'
           json_data
         end
  Applitools::EyesLogger.debug 'Sending match data...'
  Applitools::EyesLogger.debug json_data
  res = long_post(URI.join(endpoint_url, session.id.to_s), content_type: content_type, body: body)
  raise Applitools::EyesError.new("Request failed: #{res.status} #{res.}") unless res.success?
    Applitools::MatchResult.new Oj.load(res.body)
end
     | 
  
 
    
      
  
  
    #obtain_agent_id(&block)  ⇒ Object 
  
  
  
  
    
      
53
54
55 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 53
def obtain_agent_id(&block)
  @agent_id_proc = block if block_given?
end 
     | 
  
 
    
      
  
  
    #post_dom_json(dom_data, rendering_info)  ⇒ Object 
  
  
  
  
    
      
300
301
302
303
304
305
306
307
308 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 300
def post_dom_json(dom_data, rendering_info)
  Applitools::EyesLogger.debug 'About to send captured DOM...'
  request_body = dom_data
  Applitools::EyesLogger.debug request_body
  processed_request_body = yield(request_body) if block_given?
  location = put_dom(rendering_info, processed_request_body)
  Applitools::EyesLogger.debug 'Done!'
  location
end 
     | 
  
 
    
      
  
  
    #put_data(rendering_info, data, content_type, log_message_entity)  ⇒ Object 
  
  
  
  
    
      
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 95
def put_data(rendering_info, data, content_type, log_message_entity)
  uuid = SecureRandom.uuid
  upload_path = URI.parse(rendering_info.results_url.gsub(/__random__/, uuid))
  retry_count = 3
  wait = 0.5
  loop do
    raise ScreenshotUploadError, "Error uploading #{log_message_entity} (#{upload_path})" if retry_count <= 0
    Applitools::EyesLogger.info("Trying to upload #{log_message_entity} (#{upload_path})...")
    begin
      response = dummy_put(
        upload_path,
        body: data,
        headers: {
          'x-ms-blob-type': 'BlockBlob',
          'X-Auth-Token': rendering_info.access_token
        },
        query: URI.decode_www_form(upload_path.query).to_h,
        content_type: content_type
      )
      break if response.status == HTTP_STATUS_CODES[:created]
      Applitools::EyesLogger.info("Failed. Retrying in #{wait} seconds")
      sleep(wait)
    rescue StandardError => e
      Applitools::EyesLogger.error(e.class.to_s)
      Applitools::EyesLogger.error(e.message)
      Applitools::EyesLogger.info("Failed. Retrying in #{wait} seconds")
      sleep(wait)
    ensure
      retry_count -= 1
      wait *= 2
    end
  end
  Applitools::EyesLogger.info('Done.')
  upload_path.to_s
end
     | 
  
 
    
      
  
  
    #put_dom(rendering_info, dom_data)  ⇒ Object 
  
  
  
  
    
      
91
92
93 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 91
def put_dom(rendering_info, dom_data)
  put_data(rendering_info, dom_data, 'application/octet-stream', 'dom data')
end 
     | 
  
 
    
      
  
  
    #put_screenshot(rendering_info, screenshot)  ⇒ Object 
  
  
  
  
    
      
87
88
89 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 87
def put_screenshot(rendering_info, screenshot)
  put_data(rendering_info, screenshot.image.to_blob, 'image/png', 'screenshot')
end 
     | 
  
 
    
      
  
  
    #render(service_url, access_key, requests)  ⇒ Object 
  
  
  
  
    
      
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 71
def render(service_url, access_key, requests)
  uri = URI.join(URI(service_url), RENDER)
  response = dummy_post(
    uri,
    body: requests.json,
    headers: {
      'X-Auth-Token' => access_key
    },
    timeout: 10
  )
  unless response.status == HTTP_STATUS_CODES[:ok]
    raise Applitools::EyesError, "Error render processing (#{response.status}, #{response.body})"
  end
  Oj.load response.body
end
     | 
  
 
    
      
  
  
    #render_put_resource(service_url, access_key, resource, render)  ⇒ Object 
  
  
  
  
    
      
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 131
def render_put_resource(service_url, access_key, resource, render)
  uri = URI(service_url)
  uri.path = RESOURCES_SHA_256 + resource.hash
  Applitools::EyesLogger.debug("PUT resource: (#{resource.url}) - #{uri}")
    response = dummy_put(
    uri,
    body: resource.content,
    content_type: resource.content_type,
    headers: {
      'X-Auth-Token' => access_key
    },
    query: { 'render-id' => render['renderId'] }
  )
  unless response.status == HTTP_STATUS_CODES[:ok]
    raise Applitools::EyesError, "Error putting resource: #{response.status}, #{response.body}"
  end
  resource.hash
end
     | 
  
 
    
      
  
  
    #render_status_by_id(service_url, access_key, running_renders_json)  ⇒ Object 
  
  
  
  
    
      
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 151
def render_status_by_id(service_url, access_key, running_renders_json)
  uri = URI(service_url)
  uri.path = RENDER_STATUS
  response = dummy_post(
    uri,
    body: running_renders_json,
    content_type: 'application/json',
    headers: {
      'X-Auth-Token' => access_key
    },
    timeout: 2
  )
  unless response.status == HTTP_STATUS_CODES[:ok]
    raise Applitools::EyesError, "Error getting server status, #{response.status} #{response.body}"
  end
  Oj.load(response.body)
end
     | 
  
 
    
      
  
  
    #rendering_info  ⇒ Object 
  
  
  
  
    
      
63
64
65
66
67
68
69 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 63
def rendering_info
  response = long_get(URI.join(server_url, RENDER_INFO_PATH), content_type: 'application/json')
  unless response.status == HTTP_STATUS_CODES[:ok]
    raise Applitools::EyesError, "Error getting render info (#{response.status}})"
  end
  Oj.load response.body
end
     | 
  
 
    
      
  
  
    #set_proxy(uri, user = nil, password = nil)  ⇒ Object 
  
  
  
  
    
      
213
214
215 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 213
def set_proxy(uri, user = nil, password = nil)
  self.proxy = Proxy.new uri, user, password
end 
     | 
  
 
    
      
  
  
    #start_session(session_start_info)  ⇒ Object 
  
  
  
  
    
      
282
283
284
285
286
287
288
289
290 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 282
def start_session(session_start_info)
  request_body = session_start_info.json
  res = long_post(
    endpoint_url, body: request_body
  )
  raise Applitools::EyesError.new("Request failed: #{res.status} #{res.body} #{request_body}") unless res.success?
  response = Oj.load(res.body)
  Applitools::Session.new(response, res.status == HTTP_STATUS_CODES[:created])
end
     | 
  
 
    
      
  
  
    #stop_session(session, aborted = nil, save = false)  ⇒ Object 
  
  
  
  
    
      
292
293
294
295
296
297
298 
     | 
    
      # File 'lib/applitools/connectivity/server_connector.rb', line 292
def stop_session(session, aborted = nil, save = false)
  res = long_delete(URI.join(endpoint_url, session.id.to_s), query: { aborted: aborted, updateBaseline: save })
  raise Applitools::EyesError.new("Request failed: #{res.status}") unless res.success?
  response = Oj.load(res.body)
  Applitools::TestResults.new(response)
end
     |