psf / psf/requests

urllib3 Retry and Response Hooks (Duplicates #4514)

Open
#4,747 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
54.3k
Forks
10.4k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

Given you have a session, and have successfully attached the auth class and the response hooks accordingly; although you'd like to add the Retry class into the picture from urllib3.

Expected Result

I'd expect the response hooks to be honoured - at the very least on the first response (not retry).

Actual Result

The response hooks are ignored completely.

Reproduction Steps

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

retry = Retry(total=1, read=1, connect=1, backoff_factor=0.5,
                      status_forcelist=(401, 500))

adapter = HTTPAdapter(max_retries=retry)

session = requests.Session()
session.hooks = {'response': response_hook}

session.mount('http://', adapter)
session.mount('https://', adapter)

def response_hook(response, *args, **kwargs):
    """
    Response hook

    :param response:
    :return:
    """
    if response.status_code == requests.codes.unauthorized:
       print('Your logic here')

System Information

Python version: 3.5
Requests version: 2.11.0
OS: Ubuntu 16.04

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with Session, HTTPAdapter, Retry, and the response hook shown in the issue. Read duplicate issue #4514 and trace how retries relate to response hooks. Add a regression test for the expected first-response hook behavior; done when the hook is honored despite the configured Retry.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.