mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-04-28 09:17:17 +00:00
92 lines
3.2 KiB
Python
92 lines
3.2 KiB
Python
#
|
|
# Copyright (c) 2021-2022 ahriman team.
|
|
#
|
|
# This file is part of ahriman
|
|
# (see https://github.com/arcan1s/ahriman).
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
import requests
|
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from ahriman.core.alpm.remote.remote import Remote
|
|
from ahriman.core.exceptions import InvalidPackageInfo
|
|
from ahriman.core.util import exception_response_text
|
|
from ahriman.models.aur_package import AURPackage
|
|
|
|
|
|
class Official(Remote):
|
|
"""
|
|
official repository RPC wrapper
|
|
:cvar DEFAULT_RPC_URL: default AUR RPC url
|
|
:ivar rpc_url: AUR RPC url
|
|
"""
|
|
|
|
DEFAULT_RPC_URL = "https://archlinux.org/packages/search/json"
|
|
|
|
def __init__(self, rpc_url: Optional[str] = None) -> None:
|
|
"""
|
|
default constructor
|
|
:param rpc_url: AUR RPC url
|
|
"""
|
|
Remote.__init__(self)
|
|
self.rpc_url = rpc_url or self.DEFAULT_RPC_URL
|
|
|
|
@staticmethod
|
|
def parse_response(response: Dict[str, Any]) -> List[AURPackage]:
|
|
"""
|
|
parse RPC response to package list
|
|
:param response: RPC response json
|
|
:return: list of parsed packages
|
|
"""
|
|
if not response["valid"]:
|
|
raise InvalidPackageInfo("API validation error")
|
|
return [AURPackage.from_repo(package) for package in response["results"]]
|
|
|
|
def make_request(self, *args: str, by: str) -> List[AURPackage]:
|
|
"""
|
|
perform request to official repositories RPC
|
|
:param args: list of arguments to be passed as args query parameter
|
|
:param by: search by the field
|
|
:return: response parsed to package list
|
|
"""
|
|
try:
|
|
response = requests.get(self.rpc_url, params={by: args})
|
|
response.raise_for_status()
|
|
return self.parse_response(response.json())
|
|
except requests.HTTPError as e:
|
|
self.logger.exception("could not perform request: %s", exception_response_text(e))
|
|
raise
|
|
except Exception:
|
|
self.logger.exception("could not perform request")
|
|
raise
|
|
|
|
def package_info(self, package_name: str) -> AURPackage:
|
|
"""
|
|
get package info by its name
|
|
:param package_name: package name to search
|
|
:return: package which match the package name
|
|
"""
|
|
packages = self.make_request(package_name, by="name")
|
|
return next(package for package in packages if package.name == package_name)
|
|
|
|
def package_search(self, *keywords: str) -> List[AURPackage]:
|
|
"""
|
|
search package in AUR web
|
|
:param keywords: keywords to search
|
|
:return: list of packages which match the criteria
|
|
"""
|
|
return self.make_request(*keywords, by="q")
|