Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add headers option is aiohttp client session #1041

Merged
merged 1 commit into from Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/8083.feature
@@ -0,0 +1 @@
Added optional headers field to the aiohttp ClientSession.
19 changes: 19 additions & 0 deletions pulpcore/app/migrations/0053_remote_headers.py
@@ -0,0 +1,19 @@
# Generated by Django 2.2.17 on 2021-01-20 14:34

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('core', '0052_tasking_logging_cid'),
]

operations = [
migrations.AddField(
model_name='remote',
name='headers',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, null=True),
),
]
3 changes: 3 additions & 0 deletions pulpcore/app/models/repository.py
Expand Up @@ -19,6 +19,8 @@
from .content import Artifact, Content
from .task import CreatedResource, Task

from django.contrib.postgres.fields import JSONField


_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -266,6 +268,7 @@ class Remote(MasterModel):
sock_read_timeout = models.FloatField(
null=True, validators=[MinValueValidator(0.0, "Timeout must be >= 0")]
)
headers = JSONField(blank=True, null=True)
ThikaXer marked this conversation as resolved.
Show resolved Hide resolved

@property
def download_factory(self):
Expand Down
4 changes: 4 additions & 0 deletions pulpcore/app/serializers/repository.py
Expand Up @@ -137,6 +137,10 @@ class RemoteSerializer(ModelSerializer):
help_text="aiohttp.ClientTimeout.sock_read (q.v.) for download-connections.",
min_value=0.0,
)
headers = serializers.ListField(
child=serializers.DictField(),
help_text=_("Headers for aiohttp.Clientsession"),
)

def validate_url(self, value):
"""
Expand Down
11 changes: 10 additions & 1 deletion pulpcore/download/factory.py
Expand Up @@ -15,7 +15,14 @@
from .http import HttpDownloader
from .file import FileDownloader

PROTOCOL_MAP = {"http": HttpDownloader, "https": HttpDownloader, "file": FileDownloader}
import json


PROTOCOL_MAP = {
"http": HttpDownloader,
"https": HttpDownloader,
"file": FileDownloader,
}


def user_agent():
Expand Down Expand Up @@ -118,6 +125,8 @@ def _make_aiohttp_session_from_remote(self):
tcp_conn_opts["ssl_context"] = sslcontext

headers = {"User-Agent": user_agent()}
if self._remote.headers is not None:
headers.update(json.loads(self._remote.headers))

conn = aiohttp.TCPConnector(**tcp_conn_opts)
total = self._remote.total_timeout
Expand Down
4 changes: 3 additions & 1 deletion pulpcore/download/http.py
Expand Up @@ -119,6 +119,7 @@ def __init__(
proxy=None,
proxy_auth=None,
headers_ready_callback=None,
headers=None,
**kwargs,
):
"""
Expand All @@ -134,6 +135,7 @@ def __init__(
as its argument. The callback will be called when the response headers are
available. The dictionary passed has the header names as the keys and header values
as its values. e.g. `{'Transfer-Encoding': 'chunked'}`
headers (dict): Headers to be submitted with the request.
kwargs (dict): This accepts the parameters of
:class:`~pulpcore.plugin.download.BaseDownloader`.
"""
Expand All @@ -143,7 +145,7 @@ def __init__(
else:
timeout = aiohttp.ClientTimeout(total=None, sock_connect=600, sock_read=600)
conn = aiohttp.TCPConnector({"force_close": True})
self.session = aiohttp.ClientSession(connector=conn, timeout=timeout)
self.session = aiohttp.ClientSession(connector=conn, timeout=timeout, headers=headers)
ThikaXer marked this conversation as resolved.
Show resolved Hide resolved
self._close_session_on_finalize = True
self.auth = auth
self.proxy = proxy
Expand Down