def send(self, request, **kwargs) -> UpgradeResponse: kwargs.setdefault('stream', self.stream) kwargs.setdefault('verify', self.verify) kwargs.setdefault('cert', self.cert) kwargs.setdefault('proxies', self.proxies) if isinstance(request, requests.Request): raise ValueError('You can only send PreparedRequests.') if isinstance(request, HttpRequest): request.check() allow_redirects = kwargs.pop('allow_redirects', True) hooks = request.hooks adapter = self.get_adapter(url=request.url) start = preferred_clock() r = adapter.send(request, **kwargs) elapsed = preferred_clock() - start r.elapsed = timedelta(seconds=elapsed) # this function not change response and result params if len(self._dispatch_hooks) > 0: dispatch(self._dispatch_hooks, hooks, r, **kwargs) # dispatch hook will change response and request params r = dispatch_hook('response', hooks, r, **kwargs) if r.history: for resp in r.history: extract_cookies_to_jar(self.cookies, resp.request, resp.raw) extract_cookies_to_jar(self.cookies, request, r.raw) gen = self.resolve_redirects(r, request, **kwargs) history = [resp for resp in gen] if allow_redirects else [] if history: history.insert(0, r) r = history.pop() r.history = history if not allow_redirects: try: r._next = next( self.resolve_redirects(r, request, yield_requests=True, **kwargs)) except StopIteration: pass return r
async def _send(self, request, **kwargs): """Send a given PreparedRequest. :rtype: requests.Response """ logger.debug(f'Send request: {request}') # Set defaults that the hooks can utilize to ensure they always have # the correct parameters to reproduce the previous request. kwargs.setdefault('stream', self.stream) kwargs.setdefault('verify', self.verify) kwargs.setdefault('cert', self.cert) kwargs.setdefault('proxies', self.proxies) # It's possible that users might accidentally send a Request object. # Guard against that specific failure case. if isinstance(request, Request): raise ValueError('You can only send PreparedRequests.') hooks = request.hooks # Get the appropriate adapter to use adapter = self.get_adapter(url=request.url) # Start time (approximately) of the request start = preferred_clock() # Send the request r = await adapter.send(request, **kwargs) if r.is_redirect: r.url = self._get_next_url(r) # Total elapsed time of the request (approximately) elapsed = preferred_clock() - start logger.debug(f'Request {request} elapsed {elapsed:.3f} seconds') r.elapsed = timedelta(seconds=elapsed) # Response manipulation hooks r = dispatch_hook('response', hooks, r, **kwargs) extract_cookies_to_jar(self.cookies, request, r.raw) return r
def send(self, req, **kwargs): """Send a given PreparedRequest. :rtype: trip.gen.Future """ if not isinstance(req, PreparedRequest): raise ValueError('You can only send PreparedRequests.') allow_redirects = kwargs.pop('allow_redirects', True) start_time = preferred_clock() r = yield self.adapter.send(req, **kwargs) if isinstance(r, Exception): raise gen.Return(r) else: r = self.prepare_response(req, r) r.elapsed = timedelta(seconds=(preferred_clock() - start_time)) # Response manipulation hooks r = dispatch_hook('response', req.hooks, r, **kwargs) # Persist cookies if r.history: # If the hooks create history then we want those cookies too for resp in r.history: self.cookies.extract_cookies( MockResponse(HTTPHeaderDict(resp.headers)), MockRequest(resp.request)) self.cookies.extract_cookies(MockResponse(HTTPHeaderDict(r.headers)), MockRequest(req)) # Redirect resolving generator. redirect_gen = self.resolve_redirects(r, req, **kwargs) # Resolve redirects if allowed. history = [] if allow_redirects: for resp in redirect_gen: resp = yield resp history.append(resp) # Shuffle things around if there's history. if history: # Insert the first (original) request at the start history.insert(0, r) # Get the last request made r = history.pop() r.history = history # If redirects aren't being followed, store the response on the Request for Response.next(). if not allow_redirects: try: r._next = next( self.resolve_redirects(r, req, yield_requests=True, **kwargs)) except StopIteration: pass raise gen.Return(r)