Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions scripts/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import logging
import os
import sys
from collections import OrderedDict
from datetime import datetime, timezone

# Third-party
from git import InvalidGitRepositoryError, NoSuchPathError, Repo
from pandas import PeriodIndex
from requests import Session
from requests.adapters import HTTPAdapter, Retry
from requests.adapters import HTTPAdapter
from urllib3.util import Retry

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


def get_session(accept_header=None):
"""Create a reusable HTTP session with retry logic."""
session = Session()
def get_session(accept_header=None, session=None):
"""
Create or configure a reusable HTTPS session with retry logic and
appropriate headers.
"""
if session is None:
session = Session()

# Purge default and custom session connection adapters
# (With only a https:// adapter, below, unencrypted requests will fail.)
session.adapters = OrderedDict()

# Try again after 0s, 6s, 12s, 24s, 48s (total 90s) for the specified HTTP
# error codes (STATUS_FORCELIST)
retry_strategy = Retry(
total=5,
backoff_factor=10,
backoff_factor=3,
status_forcelist=STATUS_FORCELIST,
allowed_methods=["GET", "POST"],
raise_on_status=False,
)
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))

Expand Down