Skip to content

Commit

Permalink
BF: Properly handle HTTP403 by using internal download helper
Browse files Browse the repository at this point in the history
Previous request result handling assumed that HTTP error codes
necessarily result in exceptions, which they do not.

Instead of implementing error handling per request, use an existing
abstraction.

Fixes dataladgh-5023

... although it could be made better by supporting git-credentials as
a credential sources in DataLad.
  • Loading branch information
mih committed Oct 13, 2020
1 parent 9b0286f commit 4a05696
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions datalad/core/distributed/clone.py
Expand Up @@ -12,6 +12,7 @@
import logging
import re
import requests
from tempfile import TemporaryDirectory
from os.path import expanduser
from collections import OrderedDict
from urllib.parse import unquote as urlunquote
Expand Down Expand Up @@ -45,6 +46,7 @@
EnsureStr,
EnsureKeyChoice,
)
from datalad.support.exceptions import DownloadError
from datalad.support.param import Parameter
from datalad.support.network import (
get_local_file_url,
Expand All @@ -59,6 +61,7 @@
)
from datalad.utils import (
assure_bool,
download_url,
knows_annex,
make_tempfile,
Path,
Expand Down Expand Up @@ -630,12 +633,14 @@ def postclonecfg_ria(ds, props):
scheme = props['giturl'].split(':', 1)[0]
if scheme in ['http', 'https']:
try:
response = requests.get("{}{}config".format(
props['giturl'],
'/' if not props['giturl'].endswith('/') else '')
)
config_content = response.text
except requests.RequestException as e:
with TemporaryDirectory() as _dest:
config_content = open(
download_url(
"{}{}config".format(
props['giturl'],
'/' if not props['giturl'].endswith('/') else ''),
_dest)).read()
except DownloadError as e:
lgr.debug("Failed to get config file from source:\n%s",
exc_str(e))

Expand All @@ -662,8 +667,8 @@ def postclonecfg_ria(ds, props):
lgr.debug("Failed to get config file from source: %s",
exc_str(e))
else:
lgr.debug("Unknown URL-Scheme in %s. Can handle SSH, HTTP or "
"FILE scheme URLs.", props['source'])
lgr.debug("Unknown URL-Scheme %s in %s. Can handle SSH, HTTP or "
"FILE scheme URLs.", scheme, props['source'])

# 3. And read it
org_uuid = None
Expand Down

0 comments on commit 4a05696

Please sign in to comment.