2020# httplib.HTTPConnection.debuglevel = 1
2121
2222# Remove Unverified SSL warnings propagated by requests' internal urllib3 module
23- if hasattr (urllib3 , ' disable_warnings' ) and callable (urllib3 .disable_warnings ):
23+ if hasattr (urllib3 , " disable_warnings" ) and callable (urllib3 .disable_warnings ):
2424 urllib3 .disable_warnings ()
2525
2626
@@ -34,8 +34,12 @@ class AgentConnector(object):
3434 """
3535 Provides an API for communication with the Applitools server.
3636 """
37+
3738 _TIMEOUT = 60 * 5 # Seconds
38- _DEFAULT_HEADERS = {'Accept' : 'application/json' , 'Content-Type' : 'application/json' }
39+ _DEFAULT_HEADERS = {
40+ "Accept" : "application/json" ,
41+ "Content-Type" : "application/json" ,
42+ }
3943
4044 def __init__ (self , server_url ):
4145 # type: (tp.Text) -> None
@@ -60,18 +64,22 @@ def server_url(self):
6064 def server_url (self , server_url ):
6165 # type: (tp.Text) -> None
6266 self ._server_url = server_url # type: ignore
63- self ._endpoint_uri = server_url .rstrip ('/' ) + '/api/sessions/running' # type: ignore
67+ self ._endpoint_uri = ( # type: ignore
68+ server_url .rstrip ("/" ) + "/api/sessions/running"
69+ )
6470
6571 @staticmethod
6672 def _send_long_request (name , method , * args , ** kwargs ):
6773 # type: (tp.Text, tp.Callable, *tp.Any, **tp.Any) -> Response
6874 delay = 2 # type: Num # Seconds
69- headers = kwargs [' headers' ].copy ()
70- headers [' Eyes-Expect' ] = ' 202-accepted'
75+ headers = kwargs [" headers" ].copy ()
76+ headers [" Eyes-Expect" ] = " 202-accepted"
7177 while True :
7278 # Sending the current time of the request (in RFC 1123 format)
73- headers ['Eyes-Date' ] = time .strftime ('%a, %d %b %Y %H:%M:%S GMT' , time .gmtime ())
74- kwargs ['headers' ] = headers
79+ headers ["Eyes-Date" ] = time .strftime (
80+ "%a, %d %b %Y %H:%M:%S GMT" , time .gmtime ()
81+ )
82+ kwargs ["headers" ] = headers
7583 response = method (* args , ** kwargs )
7684 if response .status_code != 202 :
7785 return response
@@ -90,12 +98,20 @@ def start_session(self, session_start_info):
9098 :return: Represents the current running session.
9199 """
92100 data = '{"startInfo": %s}' % (general_utils .to_json (session_start_info ))
93- response = requests .post (self ._endpoint_uri , data = data , verify = False , params = dict (apiKey = self .api_key ),
94- headers = AgentConnector ._DEFAULT_HEADERS ,
95- timeout = AgentConnector ._TIMEOUT )
101+ response = requests .post (
102+ self ._endpoint_uri ,
103+ data = data ,
104+ verify = False ,
105+ params = dict (apiKey = self .api_key ),
106+ headers = AgentConnector ._DEFAULT_HEADERS ,
107+ timeout = AgentConnector ._TIMEOUT ,
108+ )
96109 parsed_response = _parse_response_with_json_data (response )
97- return dict (session_id = parsed_response ['id' ], session_url = parsed_response ['url' ],
98- is_new_session = (response .status_code == requests .codes .created ))
110+ return dict (
111+ session_id = parsed_response ["id" ],
112+ session_url = parsed_response ["url" ],
113+ is_new_session = (response .status_code == requests .codes .created ),
114+ )
99115
100116 def stop_session (self , running_session , is_aborted , save ):
101117 # type: (RunningSession, bool, bool) -> TestResults
@@ -107,39 +123,59 @@ def stop_session(self, running_session, is_aborted, save):
107123 :param save: Whether the session should be automatically saved if it is not aborted.
108124 :return: Test results of the stopped session.
109125 """
110- logger .debug ('Stop session called..' )
111- session_uri = "%s/%s" % (self ._endpoint_uri , running_session ['session_id' ])
112- params = {'aborted' : is_aborted , 'updateBaseline' : save , 'apiKey' : self .api_key }
113- response = AgentConnector ._send_long_request ("stop_session" , requests .delete , session_uri ,
114- params = params , verify = False ,
115- headers = AgentConnector ._DEFAULT_HEADERS ,
116- timeout = AgentConnector ._TIMEOUT )
126+ logger .debug ("Stop session called.." )
127+ session_uri = "%s/%s" % (self ._endpoint_uri , running_session ["session_id" ])
128+ params = {"aborted" : is_aborted , "updateBaseline" : save , "apiKey" : self .api_key }
129+ response = AgentConnector ._send_long_request (
130+ "stop_session" ,
131+ requests .delete ,
132+ session_uri ,
133+ params = params ,
134+ verify = False ,
135+ headers = AgentConnector ._DEFAULT_HEADERS ,
136+ timeout = AgentConnector ._TIMEOUT ,
137+ )
117138 pr = _parse_response_with_json_data (response )
118139 logger .debug ("stop_session(): parsed response: {}" .format (pr ))
119- return TestResults (pr ['steps' ], pr ['matches' ], pr ['mismatches' ], pr ['missing' ],
120- pr ['exactMatches' ], pr ['strictMatches' ], pr ['contentMatches' ],
121- pr ['layoutMatches' ], pr ['noneMatches' ], pr ['status' ])
140+ return TestResults (
141+ pr ["steps" ],
142+ pr ["matches" ],
143+ pr ["mismatches" ],
144+ pr ["missing" ],
145+ pr ["exactMatches" ],
146+ pr ["strictMatches" ],
147+ pr ["contentMatches" ],
148+ pr ["layoutMatches" ],
149+ pr ["noneMatches" ],
150+ pr ["status" ],
151+ )
122152
123153 def match_window (self , running_session , data ):
124154 # type: (RunningSession, tp.Text) -> bool
125155 """
126- Matches the current window to the immediate expected window in the Eyes server. Notice that
127- a window might be matched later at the end of the test, even if it was not immediately
128- matched in this call.
156+ Matches the current window to the immediate expected window in the Eyes server.
157+ Notice that a window might be matched later at the end of the test, even if it
158+ was not immediately matched in this call.
129159
130160 :param running_session: The current session that is running.
131161 :param data: The data for the requests.post.
132162 :return: The parsed response.
133163 """
134164 # logger.debug("Data length: %d, data: %s" % (len(data), repr(data)))
135- session_uri = "%s/%s" % (self ._endpoint_uri , running_session [' session_id' ])
165+ session_uri = "%s/%s" % (self ._endpoint_uri , running_session [" session_id" ])
136166 # Using the default headers, but modifying the "content type" to binary
137167 headers = AgentConnector ._DEFAULT_HEADERS .copy ()
138- headers ['Content-Type' ] = 'application/octet-stream'
139- response = requests .post (session_uri , params = dict (apiKey = self .api_key ), data = data , verify = False ,
140- headers = headers , timeout = AgentConnector ._TIMEOUT )
168+ headers ["Content-Type" ] = "application/octet-stream"
169+ response = requests .post (
170+ session_uri ,
171+ params = dict (apiKey = self .api_key ),
172+ data = data ,
173+ verify = False ,
174+ headers = headers ,
175+ timeout = AgentConnector ._TIMEOUT ,
176+ )
141177 parsed_response = _parse_response_with_json_data (response )
142- return parsed_response [' asExpected' ]
178+ return parsed_response [" asExpected" ]
143179
144180 def post_dom_snapshot (self , dom_json ):
145181 # type: (tp.Text) -> tp.Optional[tp.Text]
@@ -148,14 +184,16 @@ def post_dom_snapshot(self, dom_json):
148184 Return an URL of uploaded resource which should be posted to AppOutput.
149185 """
150186 headers = AgentConnector ._DEFAULT_HEADERS .copy ()
151- headers ['Content-Type' ] = 'application/octet-stream'
152- dom_bytes = gzip_compress (dom_json .encode ('utf-8' ))
153- response = requests .post (url = urljoin (self ._endpoint_uri , 'running/data' ),
154- data = dom_bytes ,
155- params = dict (apiKey = self .api_key ),
156- headers = headers ,
157- timeout = AgentConnector ._TIMEOUT )
187+ headers ["Content-Type" ] = "application/octet-stream"
188+ dom_bytes = gzip_compress (dom_json .encode ("utf-8" ))
189+ response = requests .post (
190+ url = urljoin (self ._endpoint_uri , "running/data" ),
191+ data = dom_bytes ,
192+ params = dict (apiKey = self .api_key ),
193+ headers = headers ,
194+ timeout = AgentConnector ._TIMEOUT ,
195+ )
158196 dom_url = None
159197 if response .ok :
160- dom_url = response .headers [' Location' ]
198+ dom_url = response .headers [" Location" ]
161199 return dom_url
0 commit comments