ansible-galaxy - Add User-Agent to requests (#65438)

pull/63219/head
Jordan Borean 5 years ago committed by GitHub
parent 6075536907
commit 8648003c1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- ansible-galaxy - Set ``User-Agent`` to Ansible version when interacting with Galaxy or Automation Hub

@ -13,6 +13,7 @@ import time
from ansible import context
from ansible.errors import AnsibleError
from ansible.module_utils.ansible_release import __version__ as ansible_version
from ansible.module_utils.six import string_types
from ansible.module_utils.six.moves.urllib.error import HTTPError
from ansible.module_utils.six.moves.urllib.parse import quote as urlquote, urlencode, urlparse
@ -184,7 +185,7 @@ class GalaxyAPI:
try:
display.vvvv("Calling Galaxy at %s" % url)
resp = open_url(to_native(url), data=args, validate_certs=self.validate_certs, headers=headers,
method=method, timeout=20)
method=method, timeout=20, http_agent='ansible-galaxy/%s' % ansible_version)
except HTTPError as e:
raise GalaxyError(e, error_context_msg)
except Exception as e:

@ -33,6 +33,7 @@ from ansible.galaxy import get_collections_galaxy_meta_info
from ansible.galaxy.api import CollectionVersionMetadata, GalaxyError
from ansible.module_utils import six
from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.module_utils.ansible_release import __version__ as ansible_version
from ansible.utils.collection_loader import AnsibleCollectionRef
from ansible.utils.display import Display
from ansible.utils.hashing import secure_hash, secure_hash_s
@ -876,7 +877,7 @@ def _download_file(url, b_path, expected_hash, validate_certs, headers=None):
display.vvv("Downloading %s to %s" % (url, to_text(b_path)))
# Galaxy redirs downloads to S3 which reject the request if an Authorization header is attached so don't redir that
resp = open_url(to_native(url, errors='surrogate_or_strict'), validate_certs=validate_certs, headers=headers,
unredirected_headers=['Authorization'])
unredirected_headers=['Authorization'], http_agent='ansible-galaxy/%s' % ansible_version)
with open(b_file_path, 'wb') as download_file:
data = resp.read(bufsize)

Loading…
Cancel
Save