22import logging
33import os
44import sys
5+ from collections import OrderedDict
56from datetime import datetime , timezone
67
78# Third-party
89from git import InvalidGitRepositoryError , NoSuchPathError , Repo
910from pandas import PeriodIndex
1011from 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
1416STATUS_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