Skip to content

Commit 2fe62b7

Browse files
authored
Merge pull request #240 from creativecommons/only-specified-https-session-adapter
Ensure only specified https:// adapter is in session
2 parents 6482d63 + b9ea597 commit 2fe62b7

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

scripts/shared.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
import logging
33
import os
44
import sys
5+
from collections import OrderedDict
56
from datetime import datetime, timezone
67

78
# Third-party
89
from git import InvalidGitRepositoryError, NoSuchPathError, Repo
910
from pandas import PeriodIndex
1011
from requests import Session
11-
from requests.adapters import HTTPAdapter, Retry
12+
from requests.adapters import HTTPAdapter
13+
from urllib3.util import Retry
1214

1315
# Constants
1416
STATUS_FORCELIST = [
@@ -33,14 +35,26 @@ def __init__(self, message, exit_code=None):
3335
super().__init__(self.message)
3436

3537

36-
def get_session(accept_header=None):
37-
"""Create a reusable HTTP session with retry logic."""
38-
session = Session()
38+
def get_session(accept_header=None, session=None):
39+
"""
40+
Create or configure a reusable HTTPS session with retry logic and
41+
appropriate headers.
42+
"""
43+
if session is None:
44+
session = Session()
45+
46+
# Purge default and custom session connection adapters
47+
# (With only a https:// adapter, below, unencrypted requests will fail.)
48+
session.adapters = OrderedDict()
3949

50+
# Try again after 0s, 6s, 12s, 24s, 48s (total 90s) for the specified HTTP
51+
# error codes (STATUS_FORCELIST)
4052
retry_strategy = Retry(
4153
total=5,
42-
backoff_factor=10,
54+
backoff_factor=3,
4355
status_forcelist=STATUS_FORCELIST,
56+
allowed_methods=["GET", "POST"],
57+
raise_on_status=False,
4458
)
4559
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
4660

0 commit comments

Comments
 (0)