Skip to content

Commit b7c4bff

Browse files
committed
simplify get_session and improve header handling
1 parent bdcd6cc commit b7c4bff

File tree

6 files changed

+13
-21
lines changed

6 files changed

+13
-21
lines changed

scripts/1-fetch/arxiv_fetch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def query_arxiv(args):
518518
"""
519519

520520
LOGGER.info("Beginning to fetch results from ArXiv API")
521-
session = shared.get_requests_session()
521+
session = shared.get_session()
522522

523523
results_per_iteration = 50
524524

scripts/1-fetch/europeana_fetch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def main():
419419
"EUROPEANA_API_KEY not found in environment variables", 1
420420
)
421421

422-
session = shared.get_requests_session(accept_header="application/json")
422+
session = shared.get_session(accept_header="application/json")
423423

424424
# Fetch facet lists once, including counts
425425
providers_full = get_facet_list(session, "DATA_PROVIDER")

scripts/1-fetch/github_fetch.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ def main():
141141
args = parse_arguments()
142142
shared.paths_log(LOGGER, PATHS)
143143
check_for_completion()
144-
session = shared.get_requests_session(
145-
headers={"accept": "application/vnd.github+json"},
146-
auth_token=GH_TOKEN,
147-
auth_prefix="Bearer",
144+
session = shared.get_session(
145+
accept_header="application/vnd.github+json",
148146
)
147+
if GH_TOKEN:
148+
session.headers.update({"authorization": f"Bearer {GH_TOKEN}"})
149+
149150
tool_data = query_github(args, session)
150151
args = write_data(args, tool_data)
151152
args = shared.git_add_and_commit(

scripts/1-fetch/openverse_fetch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def write_data(args, data):
210210
def main():
211211
args = parse_arguments()
212212
LOGGER.info("Starting Openverse Fetch Script...")
213-
session = shared.get_requests_session(accept_header="application/json")
213+
session = shared.get_session(accept_header="application/json")
214214
records = query_openverse(session)
215215
write_data(args, records)
216216
LOGGER.info(f"Fetched {len(records)} unique Openverse records.")

scripts/1-fetch/wikipedia_fetch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def main():
158158
args = parse_arguments()
159159
shared.paths_log(LOGGER, PATHS)
160160
shared.git_fetch_and_merge(args, PATHS["repo"])
161-
tool_data = query_wikipedia_languages(shared.get_requests_session())
161+
tool_data = query_wikipedia_languages(shared.get_session())
162162
args = write_data(args, tool_data)
163163
args = shared.git_add_and_commit(
164164
args,

scripts/shared.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,22 @@ def __init__(self, message, exit_code=None):
3333
super().__init__(self.message)
3434

3535

36-
def get_requests_session(
37-
accept_header: str | None = None,
38-
auth_token: str | None = None,
39-
mount_https: bool = True,
40-
) -> requests.Session:
41-
"""Create a reusable requests session with retry logic."""
36+
def get_session(accept_header=None):
37+
"""Create a reusable HTTP session with retry logic."""
4238
retry_strategy = Retry(
4339
total=5,
4440
backoff_factor=10,
4541
status_forcelist=STATUS_FORCELIST,
4642
)
4743

4844
session = requests.Session()
49-
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
5045

5146
headers = {"User-Agent": USER_AGENT}
5247
if accept_header:
5348
headers["accept"] = accept_header
54-
if auth_token:
55-
headers["authorization"] = f"Bearer {auth_token}"
56-
# Mount retry adapter for HTTPS
57-
if mount_https:
58-
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
59-
6049
session.headers.update(headers)
50+
51+
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
6152
return session
6253

6354

0 commit comments

Comments
 (0)