From 61c565ab0de7ee3c9a08b8c2f76047fa1d6959f8 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Sun, 25 Jun 2023 23:08:34 +0300 Subject: [PATCH] explicitly pass user agent for the arch linux sites requests --- src/ahriman/core/alpm/remote/aur.py | 6 +++++- src/ahriman/core/alpm/remote/official.py | 1 + src/ahriman/core/alpm/remote/remote.py | 6 ++++++ src/ahriman/core/status/web_client.py | 3 +++ tests/ahriman/core/alpm/remote/test_aur.py | 3 +++ tests/ahriman/core/alpm/remote/test_official.py | 1 + 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ahriman/core/alpm/remote/aur.py b/src/ahriman/core/alpm/remote/aur.py index 14cb1150..bff37084 100644 --- a/src/ahriman/core/alpm/remote/aur.py +++ b/src/ahriman/core/alpm/remote/aur.py @@ -115,7 +115,11 @@ class AUR(Remote): query[key] = value try: - response = requests.get(self.DEFAULT_RPC_URL, params=query, timeout=self.DEFAULT_TIMEOUT) + response = requests.get( + self.DEFAULT_RPC_URL, + params=query, + headers={"User-Agent": self.DEFAULT_USER_AGENT}, + timeout=self.DEFAULT_TIMEOUT) response.raise_for_status() return self.parse_response(response.json()) except requests.HTTPError as e: diff --git a/src/ahriman/core/alpm/remote/official.py b/src/ahriman/core/alpm/remote/official.py index 1034441c..f627df7c 100644 --- a/src/ahriman/core/alpm/remote/official.py +++ b/src/ahriman/core/alpm/remote/official.py @@ -106,6 +106,7 @@ class Official(Remote): response = requests.get( self.DEFAULT_RPC_URL, params={by: args, "repo": self.DEFAULT_SEARCH_REPOSITORIES}, + headers={"User-Agent": self.DEFAULT_USER_AGENT}, timeout=self.DEFAULT_TIMEOUT) response.raise_for_status() return self.parse_response(response.json()) diff --git a/src/ahriman/core/alpm/remote/remote.py b/src/ahriman/core/alpm/remote/remote.py index 81660877..bbf88e49 100644 --- a/src/ahriman/core/alpm/remote/remote.py +++ b/src/ahriman/core/alpm/remote/remote.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # +from ahriman import version from ahriman.core.alpm.pacman import Pacman from ahriman.core.log import LazyLogging from ahriman.models.aur_package import AURPackage @@ -26,6 +27,9 @@ class Remote(LazyLogging): """ base class for remote package search + Attributes: + DEFAULT_USER_AGENT(str): (class attribute) default user agent + Examples: These classes are designed to be used without instancing. In order to achieve it several class methods are provided: ``info``, ``multisearch`` and ``search``. Thus, the basic flow is the following:: @@ -39,6 +43,8 @@ class Remote(LazyLogging): directly, whereas ``multisearch`` splits search one by one and finds intersection between search results. """ + DEFAULT_USER_AGENT = f"ahriman/{version.__version__}" + @classmethod def info(cls, package_name: str, *, pacman: Pacman) -> AURPackage: """ diff --git a/src/ahriman/core/status/web_client.py b/src/ahriman/core/status/web_client.py index cabdc0c1..44a98d07 100644 --- a/src/ahriman/core/status/web_client.py +++ b/src/ahriman/core/status/web_client.py @@ -24,6 +24,7 @@ import requests from collections.abc import Generator from urllib.parse import quote_plus as urlencode +from ahriman import version from ahriman.core.configuration import Configuration from ahriman.core.log import LazyLogging from ahriman.core.status.client import Client @@ -140,9 +141,11 @@ class WebClient(Client, LazyLogging): if use_unix_socket: import requests_unixsocket # type: ignore[import] session: requests.Session = requests_unixsocket.Session() + session.headers["User-Agent"] = f"ahriman/{version.__version__}" return session session = requests.Session() + session.headers["User-Agent"] = f"ahriman/{version.__version__}" self._login(session) return session diff --git a/tests/ahriman/core/alpm/remote/test_aur.py b/tests/ahriman/core/alpm/remote/test_aur.py index fa300eaa..af3b6d92 100644 --- a/tests/ahriman/core/alpm/remote/test_aur.py +++ b/tests/ahriman/core/alpm/remote/test_aur.py @@ -80,6 +80,7 @@ def test_make_request(aur: AUR, aur_package_ahriman: AURPackage, request_mock.assert_called_once_with( "https://aur.archlinux.org/rpc", params={"v": "5", "type": "info", "arg": ["ahriman"]}, + headers={"User-Agent": AUR.DEFAULT_USER_AGENT}, timeout=aur.DEFAULT_TIMEOUT) @@ -96,6 +97,7 @@ def test_make_request_multi_arg(aur: AUR, aur_package_ahriman: AURPackage, request_mock.assert_called_once_with( "https://aur.archlinux.org/rpc", params={"v": "5", "type": "search", "arg[]": ["ahriman", "is", "cool"]}, + headers={"User-Agent": AUR.DEFAULT_USER_AGENT}, timeout=aur.DEFAULT_TIMEOUT) @@ -112,6 +114,7 @@ def test_make_request_with_kwargs(aur: AUR, aur_package_ahriman: AURPackage, request_mock.assert_called_once_with( "https://aur.archlinux.org/rpc", params={"v": "5", "type": "search", "arg": ["ahriman"], "by": "name"}, + headers={"User-Agent": AUR.DEFAULT_USER_AGENT}, timeout=aur.DEFAULT_TIMEOUT) diff --git a/tests/ahriman/core/alpm/remote/test_official.py b/tests/ahriman/core/alpm/remote/test_official.py index 0431c72c..ee467319 100644 --- a/tests/ahriman/core/alpm/remote/test_official.py +++ b/tests/ahriman/core/alpm/remote/test_official.py @@ -75,6 +75,7 @@ def test_make_request(official: Official, aur_package_akonadi: AURPackage, request_mock.assert_called_once_with( "https://archlinux.org/packages/search/json", params={"q": ("akonadi",), "repo": Official.DEFAULT_SEARCH_REPOSITORIES}, + headers={"User-Agent": Official.DEFAULT_USER_AGENT}, timeout=official.DEFAULT_TIMEOUT)