From 77ec5b58e921c9586829f0d868ded0cc9f0c3412 Mon Sep 17 00:00:00 2001 From: Brian Scholer <1260690+briantist@users.noreply.github.com> Date: Sat, 7 Oct 2023 12:53:28 -0400 Subject: [PATCH] fix upstream download_url rewriting --- galactory/upstream.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/galactory/upstream.py b/galactory/upstream.py index 679f6ff..2b6e1fe 100644 --- a/galactory/upstream.py +++ b/galactory/upstream.py @@ -206,9 +206,33 @@ def proxy(self, request): return self._rewrite_upstream_response(cache.data, url_for('root.index', _external=True, _scheme=scheme)) def _rewrite_upstream_response(self, response_data, url_root) -> dict: - _SKIP_FIELDS = frozenset(['id', 'download_count']) + # Remove these keys from the response. + # If the value is not None, only remove when the type matches. + _REMOVE_FIELDS = { + # TODO: should id refer to some Artifactory ID? + 'id': (int, str), + # TODO: use artifactory download count, or combine with upstream? + 'download_count': int, + } + + # Leave these fields in the response without alteration or + # further processing. Same rules as _REMOVE_FIELDS. + _SKIP_FIELDS = { + # This field is an absolute URL, and will not necessarily map to + # a known API path, even if it does in public Galaxy. + # We will leave it unadultered and let the API paths that expect + # this field to overwrite it or not. + 'download_url': None, + } + ret = {} for k, v in response_data.items(): + if k in _SKIP_FIELDS and (_SKIP_FIELDS[k] is None or isinstance(v, _SKIP_FIELDS[k])): + ret[k] = v + continue + if k in _REMOVE_FIELDS and (_REMOVE_FIELDS[k] is None or isinstance(v, _REMOVE_FIELDS[k])): + continue + if isinstance(v, dict): ret[k] = self._rewrite_upstream_response(v, url_root) elif isinstance(v, list): @@ -217,8 +241,6 @@ def _rewrite_upstream_response(self, response_data, url_root) -> dict: if 'api/v1' in v: continue ret[k] = v.replace(self._upstream, url_root) - elif k in _SKIP_FIELDS: - continue else: ret[k] = v