Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

fix: LEAP-938: Fix user-agent header when downloading files #78

Merged
merged 1 commit into from
Apr 10, 2024
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
8 changes: 5 additions & 3 deletions label_studio_tools/core/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,14 @@ def download_and_cache(
if not os.path.exists(filepath):
logger.info('Download {url} to {filepath}'.format(url=url, filepath=filepath))
if download_resources:
headers = {
# avoid requests.exceptions.HTTPError: 403 Client Error: Forbidden. Please comply with the User-Agent policy:
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
}
# check if url matches hostname - then uses access token to this Label Studio instance
if access_token and hostname and parsed_url.netloc == urlparse(hostname).netloc:
headers = {'Authorization': 'Token ' + access_token}
headers['Authorization'] = 'Token ' + access_token
logger.debug('Authorization token is used for download_and_cache')
else:
headers = {}
r = requests.get(url, stream=True, headers=headers)
r.raise_for_status()
with io.open(filepath, mode='wb') as fout:
Expand Down