Skip to content

Commit

Permalink
fix(eos_downloader): Add User Agent in HTTP requests (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
titom73 committed Apr 15, 2024
1 parent e370e17 commit e077e5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
7 changes: 6 additions & 1 deletion eos_downloader/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
console = rich.get_console()
done_event = Event()

REQUEST_HEADERS = {
"Content-Type": "application/json",
"User-Agent": "Chrome/123.0.0.0",
}


def handle_sigint(signum: Any, frame: Any) -> None:
"""Progress bar handler"""
Expand Down Expand Up @@ -64,7 +69,7 @@ def _copy_url(
self, task_id: TaskID, url: str, path: str, block_size: int = 1024
) -> bool:
"""Copy data from a url to a local file."""
response = requests.get(url, stream=True, timeout=5)
response = requests.get(url, stream=True, timeout=5, headers=REQUEST_HEADERS)
# This will break if the response doesn't contain content length
self.progress.update(task_id, total=int(response.headers["Content-Length"]))
with open(path, "wb") as dest_file:
Expand Down
17 changes: 13 additions & 4 deletions eos_downloader/object_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
MSG_TOKEN_EXPIRED,
)
from eos_downloader.data import DATA_MAPPING
from eos_downloader.download import DownloadProgressBar
from eos_downloader.download import DownloadProgressBar, REQUEST_HEADERS

# logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -263,7 +263,10 @@ def get_folder_tree(self) -> ET.ElementTree:
self.authenticate()
jsonpost = {"sessionCode": self.session_id}
result = requests.post(
ARISTA_SOFTWARE_FOLDER_TREE, data=json.dumps(jsonpost), timeout=self.timeout
ARISTA_SOFTWARE_FOLDER_TREE,
data=json.dumps(jsonpost),
timeout=self.timeout,
headers=REQUEST_HEADERS,
)
try:
folder_tree = result.json()["data"]["xml"]
Expand Down Expand Up @@ -332,7 +335,10 @@ def _get_url(self, remote_file_path: str) -> str:
self.authenticate()
jsonpost = {"sessionCode": self.session_id, "filePath": remote_file_path}
result = requests.post(
ARISTA_DOWNLOAD_URL, data=json.dumps(jsonpost), timeout=self.timeout
ARISTA_DOWNLOAD_URL,
data=json.dumps(jsonpost),
timeout=self.timeout,
headers=REQUEST_HEADERS,
)
if "data" in result.json() and "url" in result.json()["data"]:
# logger.debug('URL to download file is: {}', result.json())
Expand Down Expand Up @@ -421,7 +427,10 @@ def authenticate(self) -> bool:
jsonpost = {"accessToken": credentials}

result = requests.post(
session_code_url, data=json.dumps(jsonpost), timeout=self.timeout
session_code_url,
data=json.dumps(jsonpost),
timeout=self.timeout,
headers=REQUEST_HEADERS,
)

if result.json()["status"]["message"] in [
Expand Down

0 comments on commit e077e5d

Please sign in to comment.