Skip to content

Commit 37defa9

Browse files
committed
Add RequestsFetcher settings as attributes
Stop using the settings module and add the RequestsFetcher specific config as instance attributes. Users of the requests-based fetcher implementation can modify them after instantiating a RequestsFetcher object if needed. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 520b344 commit 37defa9

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

tuf/ngclient/_internal/requests_fetcher.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
import logging
99
import time
10+
from typing import Optional
1011
from urllib import parse
1112

1213
# Imports
1314
import requests
1415
import urllib3.exceptions
1516

1617
import tuf
17-
from tuf import exceptions, settings
18+
from tuf import exceptions
1819
from tuf.ngclient.fetcher import FetcherInterface
1920

2021
# Globals
@@ -47,6 +48,11 @@ def __init__(self):
4748
# Some cookies may not be HTTP-safe.
4849
self._sessions = {}
4950

51+
# Default settings
52+
self.socket_timeout: int = 4 # seconds
53+
self.chunk_size: int = 400000 # bytes
54+
self.sleep_before_round: Optional[int] = None
55+
5056
def fetch(self, url, required_length):
5157
"""Fetches the contents of HTTP/HTTPS url from a remote server.
5258
@@ -75,9 +81,7 @@ def fetch(self, url, required_length):
7581
# requests as:
7682
# - connect timeout (max delay before first byte is received)
7783
# - read (gap) timeout (max delay between bytes received)
78-
response = session.get(
79-
url, stream=True, timeout=settings.SOCKET_TIMEOUT
80-
)
84+
response = session.get(url, stream=True, timeout=self.socket_timeout)
8185
# Check response status.
8286
try:
8387
response.raise_for_status()
@@ -99,11 +103,11 @@ def chunks():
99103
# large file in one shot. Before beginning the round, sleep
100104
# (if set) for a short amount of time so that the CPU is not
101105
# hogged in the while loop.
102-
if settings.SLEEP_BEFORE_ROUND:
103-
time.sleep(settings.SLEEP_BEFORE_ROUND)
106+
if self.sleep_before_round:
107+
time.sleep(self.sleep_before_round)
104108

105109
read_amount = min(
106-
settings.CHUNK_SIZE,
110+
self.chunk_size,
107111
required_length - bytes_received,
108112
)
109113

0 commit comments

Comments
 (0)