def _post(self, url, params, handle_redirects=True): """Make HTTP post request, handle redirects and timeouts. Args: url: url to make request. params: request parameters. handle_redirects: process redirects manually (use to handle cookies properly). """ request = HTTPRequest( url, method='POST', headers=self.DEFAULT_HEADERS.copy(), follow_redirects=not handle_redirects, request_timeout=self.default_timeout, body=urlencode(params), ) # Handle timeouts. while True: try: # If no redirect. response = yield self.client_session.fetch(request) return response except HTTPError as e: # If timeout happened just retry. if e.code == 599: continue # Else it is redirect. response = e.response break # If access token has been acquired. if response.code == 405: return response # Handle redirect. new_url = response.headers['Location'] if new_url == '/': new_url = self.LOGIN_URL # Save cookies. extract_cookies_to_jar(self.client_session.cookies, request, response) response = yield self._post(new_url, params, handle_redirects) return response
def wrapper(future): response = future.result() extract_cookies_to_jar(self.cookies, response.request, response) if callback: return callback(response)
def wrapper(response): extract_cookies_to_jar(self.cookies, response.request, response) if callback: return callback(response)