explicitly pass user agent for the arch linux sites requests

This commit is contained in:
Evgenii Alekseev 2023-06-25 23:08:34 +03:00
parent 10100b20e1
commit 61c565ab0d
6 changed files with 19 additions and 1 deletions

View File

@ -115,7 +115,11 @@ class AUR(Remote):
query[key] = value query[key] = value
try: 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() response.raise_for_status()
return self.parse_response(response.json()) return self.parse_response(response.json())
except requests.HTTPError as e: except requests.HTTPError as e:

View File

@ -106,6 +106,7 @@ class Official(Remote):
response = requests.get( response = requests.get(
self.DEFAULT_RPC_URL, self.DEFAULT_RPC_URL,
params={by: args, "repo": self.DEFAULT_SEARCH_REPOSITORIES}, params={by: args, "repo": self.DEFAULT_SEARCH_REPOSITORIES},
headers={"User-Agent": self.DEFAULT_USER_AGENT},
timeout=self.DEFAULT_TIMEOUT) timeout=self.DEFAULT_TIMEOUT)
response.raise_for_status() response.raise_for_status()
return self.parse_response(response.json()) return self.parse_response(response.json())

View File

@ -17,6 +17,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from ahriman import version
from ahriman.core.alpm.pacman import Pacman from ahriman.core.alpm.pacman import Pacman
from ahriman.core.log import LazyLogging from ahriman.core.log import LazyLogging
from ahriman.models.aur_package import AURPackage from ahriman.models.aur_package import AURPackage
@ -26,6 +27,9 @@ class Remote(LazyLogging):
""" """
base class for remote package search base class for remote package search
Attributes:
DEFAULT_USER_AGENT(str): (class attribute) default user agent
Examples: Examples:
These classes are designed to be used without instancing. In order to achieve it several class methods are 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:: 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. directly, whereas ``multisearch`` splits search one by one and finds intersection between search results.
""" """
DEFAULT_USER_AGENT = f"ahriman/{version.__version__}"
@classmethod @classmethod
def info(cls, package_name: str, *, pacman: Pacman) -> AURPackage: def info(cls, package_name: str, *, pacman: Pacman) -> AURPackage:
""" """

View File

@ -24,6 +24,7 @@ import requests
from collections.abc import Generator from collections.abc import Generator
from urllib.parse import quote_plus as urlencode from urllib.parse import quote_plus as urlencode
from ahriman import version
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.log import LazyLogging from ahriman.core.log import LazyLogging
from ahriman.core.status.client import Client from ahriman.core.status.client import Client
@ -140,9 +141,11 @@ class WebClient(Client, LazyLogging):
if use_unix_socket: if use_unix_socket:
import requests_unixsocket # type: ignore[import] import requests_unixsocket # type: ignore[import]
session: requests.Session = requests_unixsocket.Session() session: requests.Session = requests_unixsocket.Session()
session.headers["User-Agent"] = f"ahriman/{version.__version__}"
return session return session
session = requests.Session() session = requests.Session()
session.headers["User-Agent"] = f"ahriman/{version.__version__}"
self._login(session) self._login(session)
return session return session

View File

@ -80,6 +80,7 @@ def test_make_request(aur: AUR, aur_package_ahriman: AURPackage,
request_mock.assert_called_once_with( request_mock.assert_called_once_with(
"https://aur.archlinux.org/rpc", "https://aur.archlinux.org/rpc",
params={"v": "5", "type": "info", "arg": ["ahriman"]}, params={"v": "5", "type": "info", "arg": ["ahriman"]},
headers={"User-Agent": AUR.DEFAULT_USER_AGENT},
timeout=aur.DEFAULT_TIMEOUT) 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( request_mock.assert_called_once_with(
"https://aur.archlinux.org/rpc", "https://aur.archlinux.org/rpc",
params={"v": "5", "type": "search", "arg[]": ["ahriman", "is", "cool"]}, params={"v": "5", "type": "search", "arg[]": ["ahriman", "is", "cool"]},
headers={"User-Agent": AUR.DEFAULT_USER_AGENT},
timeout=aur.DEFAULT_TIMEOUT) 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( request_mock.assert_called_once_with(
"https://aur.archlinux.org/rpc", "https://aur.archlinux.org/rpc",
params={"v": "5", "type": "search", "arg": ["ahriman"], "by": "name"}, params={"v": "5", "type": "search", "arg": ["ahriman"], "by": "name"},
headers={"User-Agent": AUR.DEFAULT_USER_AGENT},
timeout=aur.DEFAULT_TIMEOUT) timeout=aur.DEFAULT_TIMEOUT)

View File

@ -75,6 +75,7 @@ def test_make_request(official: Official, aur_package_akonadi: AURPackage,
request_mock.assert_called_once_with( request_mock.assert_called_once_with(
"https://archlinux.org/packages/search/json", "https://archlinux.org/packages/search/json",
params={"q": ("akonadi",), "repo": Official.DEFAULT_SEARCH_REPOSITORIES}, params={"q": ("akonadi",), "repo": Official.DEFAULT_SEARCH_REPOSITORIES},
headers={"User-Agent": Official.DEFAULT_USER_AGENT},
timeout=official.DEFAULT_TIMEOUT) timeout=official.DEFAULT_TIMEOUT)