From c93f7916b55c013d2a48af732ca09f78e8243f21 Mon Sep 17 00:00:00 2001 From: Egbert Bouman Date: Wed, 12 Jun 2024 11:15:40 +0200 Subject: [PATCH] Move API endpoints to /api --- src/tribler/core/components.py | 18 +++++++++--------- .../restapi/search_endpoint.py | 2 +- .../core/database/restapi/database_endpoint.py | 2 +- .../knowledge/restapi/knowledge_endpoint.py | 2 +- .../restapi/create_torrent_endpoint.py | 2 +- .../libtorrent/restapi/downloads_endpoint.py | 2 +- .../libtorrent/restapi/libtorrent_endpoint.py | 2 +- .../libtorrent/restapi/torrentinfo_endpoint.py | 2 +- src/tribler/core/restapi/events_endpoint.py | 2 +- src/tribler/core/restapi/ipv8_endpoint.py | 2 +- src/tribler/core/restapi/settings_endpoint.py | 2 +- src/tribler/core/restapi/shutdown_endpoint.py | 2 +- .../core/restapi/statistics_endpoint.py | 2 +- src/tribler/core/session.py | 6 +++--- src/tribler/gui/event_request_manager.py | 2 +- src/tribler/gui/network/request.py | 2 +- .../core/restapi/test_events_endpoint.py | 2 +- .../core/restapi/test_ipv8_endpoint.py | 2 +- 18 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/tribler/core/components.py b/src/tribler/core/components.py index 42352d50925..e5e5f13caf8 100644 --- a/src/tribler/core/components.py +++ b/src/tribler/core/components.py @@ -117,7 +117,7 @@ def finalize(self, ipv8: IPv8, session: Session, community: Community) -> None: """ When we are done launching, register our REST API. """ - session.rest_manager.get_endpoint("/search").content_discovery_community = community + session.rest_manager.get_endpoint("/api/search").content_discovery_community = community def get_endpoints(self) -> list[RESTEndpoint]: """ @@ -161,10 +161,10 @@ def finalize(self, ipv8: IPv8, session: Session, community: Community) -> None: """ When we are done launching, register our REST API. """ - session.rest_manager.get_endpoint("/downloads").mds = session.mds - session.rest_manager.get_endpoint("/statistics").mds = session.mds + session.rest_manager.get_endpoint("/api/downloads").mds = session.mds + session.rest_manager.get_endpoint("/api/statistics").mds = session.mds - db_endpoint = session.rest_manager.get_endpoint("/metadata") + db_endpoint = session.rest_manager.get_endpoint("/api/metadata") db_endpoint.download_manager = session.download_manager db_endpoint.mds = session.mds db_endpoint.tribler_db = session.db @@ -193,7 +193,7 @@ def finalize(self, ipv8: IPv8, session: Session, community: Community) -> None: """ When we are done launching, register our REST API. """ - endpoint = session.rest_manager.get_endpoint("/knowledge") + endpoint = session.rest_manager.get_endpoint("/api/knowledge") endpoint.db = session.db endpoint.community = community @@ -263,7 +263,7 @@ def finalize(self, ipv8: IPv8, session: Session, community: Community) -> None: When we are done launching, register our REST API. """ community.register_task("Start torrent checker", session.torrent_checker.initialize) - session.rest_manager.get_endpoint("/metadata").torrent_checker = session.torrent_checker + session.rest_manager.get_endpoint("/api/metadata").torrent_checker = session.torrent_checker @set_in_session("dht_discovery_community") @@ -278,7 +278,7 @@ def finalize(self, ipv8: IPv8, session: Session, community: Community) -> None: """ When we are done launching, register our REST API. """ - session.rest_manager.get_endpoint("/ipv8").endpoints["/dht"].dht = community + session.rest_manager.get_endpoint("/api/ipv8").endpoints["/dht"].dht = community @set_in_session("tunnel_community") @@ -314,8 +314,8 @@ def finalize(self, ipv8: IPv8, session: Session, community: Community) -> None: """ When we are done launching, register our REST API. """ - session.rest_manager.get_endpoint("/downloads").tunnel_community = community - session.rest_manager.get_endpoint("/ipv8").endpoints["/tunnel"].tunnels = community + session.rest_manager.get_endpoint("/api/downloads").tunnel_community = community + session.rest_manager.get_endpoint("/api/ipv8").endpoints["/tunnel"].tunnels = community @after("ContentDiscoveryComponent", "TorrentCheckerComponent") diff --git a/src/tribler/core/content_discovery/restapi/search_endpoint.py b/src/tribler/core/content_discovery/restapi/search_endpoint.py index f4188a44284..46f5e3e5b95 100644 --- a/src/tribler/core/content_discovery/restapi/search_endpoint.py +++ b/src/tribler/core/content_discovery/restapi/search_endpoint.py @@ -36,7 +36,7 @@ class SearchEndpoint(RESTEndpoint): This endpoint is responsible for searching in channels and torrents present in the local Tribler database. """ - path = "/search" + path = "/api/search" def __init__(self, middlewares: tuple = (), client_max_size: int = MAX_REQUEST_SIZE) -> None: """ diff --git a/src/tribler/core/database/restapi/database_endpoint.py b/src/tribler/core/database/restapi/database_endpoint.py index aee3264492a..d863d5d4eeb 100644 --- a/src/tribler/core/database/restapi/database_endpoint.py +++ b/src/tribler/core/database/restapi/database_endpoint.py @@ -74,7 +74,7 @@ class DatabaseEndpoint(RESTEndpoint): / """ - path = "/metadata" + path = "/api/metadata" def __init__(self, middlewares: tuple = (), client_max_size: int = MAX_REQUEST_SIZE) -> None: """ diff --git a/src/tribler/core/knowledge/restapi/knowledge_endpoint.py b/src/tribler/core/knowledge/restapi/knowledge_endpoint.py index a5cfb4be546..baae30ceb28 100644 --- a/src/tribler/core/knowledge/restapi/knowledge_endpoint.py +++ b/src/tribler/core/knowledge/restapi/knowledge_endpoint.py @@ -36,7 +36,7 @@ class KnowledgeEndpoint(RESTEndpoint): Top-level endpoint for knowledge management. """ - path = "/knowledge" + path = "/api/knowledge" def __init__(self, middlewares: tuple = (), client_max_size: int = MAX_REQUEST_SIZE) -> None: """ diff --git a/src/tribler/core/libtorrent/restapi/create_torrent_endpoint.py b/src/tribler/core/libtorrent/restapi/create_torrent_endpoint.py index 825cee8e62c..84501d03b53 100644 --- a/src/tribler/core/libtorrent/restapi/create_torrent_endpoint.py +++ b/src/tribler/core/libtorrent/restapi/create_torrent_endpoint.py @@ -47,7 +47,7 @@ class CreateTorrentEndpoint(RESTEndpoint): See: http://www.bittorrent.org/beps/bep_0012.html """ - path = "/createtorrent" + path = "/api/createtorrent" def __init__(self, download_manager: DownloadManager, client_max_size: int = MAX_REQUEST_SIZE) -> None: """ diff --git a/src/tribler/core/libtorrent/restapi/downloads_endpoint.py b/src/tribler/core/libtorrent/restapi/downloads_endpoint.py index fe67e863db2..bdca411fe59 100644 --- a/src/tribler/core/libtorrent/restapi/downloads_endpoint.py +++ b/src/tribler/core/libtorrent/restapi/downloads_endpoint.py @@ -57,7 +57,7 @@ class DownloadsEndpoint(RESTEndpoint): starting, pausing and stopping downloads. """ - path = "/downloads" + path = "/api/downloads" def __init__(self, download_manager: DownloadManager, metadata_store: MetadataStore | None = None, tunnel_community: TriblerTunnelCommunity | None = None) -> None: diff --git a/src/tribler/core/libtorrent/restapi/libtorrent_endpoint.py b/src/tribler/core/libtorrent/restapi/libtorrent_endpoint.py index 9c4e37cfe96..ed1af54190f 100644 --- a/src/tribler/core/libtorrent/restapi/libtorrent_endpoint.py +++ b/src/tribler/core/libtorrent/restapi/libtorrent_endpoint.py @@ -23,7 +23,7 @@ class LibTorrentEndpoint(RESTEndpoint): Endpoint for getting information about libtorrent sessions and settings. """ - path = "/libtorrent" + path = "/api/libtorrent" def __init__(self, download_manager: DownloadManager) -> None: """ diff --git a/src/tribler/core/libtorrent/restapi/torrentinfo_endpoint.py b/src/tribler/core/libtorrent/restapi/torrentinfo_endpoint.py index 1314e1ef579..a950031fa6a 100644 --- a/src/tribler/core/libtorrent/restapi/torrentinfo_endpoint.py +++ b/src/tribler/core/libtorrent/restapi/torrentinfo_endpoint.py @@ -87,7 +87,7 @@ class TorrentInfoEndpoint(RESTEndpoint): This endpoint is responsible for handing all requests regarding torrent info in Tribler. """ - path = "/torrentinfo" + path = "/api/torrentinfo" def __init__(self, download_manager: DownloadManager) -> None: """ diff --git a/src/tribler/core/restapi/events_endpoint.py b/src/tribler/core/restapi/events_endpoint.py index 917189896c4..b645b14ce93 100644 --- a/src/tribler/core/restapi/events_endpoint.py +++ b/src/tribler/core/restapi/events_endpoint.py @@ -48,7 +48,7 @@ class EventsEndpoint(RESTEndpoint): indicates the type of the event. Individual events are separated by a newline character. """ - path = "/events" + path = "/api/events" def __init__(self, notifier: Notifier, public_key: str | None = None) -> None: """ diff --git a/src/tribler/core/restapi/ipv8_endpoint.py b/src/tribler/core/restapi/ipv8_endpoint.py index 5bed31ff166..db64bf34da8 100644 --- a/src/tribler/core/restapi/ipv8_endpoint.py +++ b/src/tribler/core/restapi/ipv8_endpoint.py @@ -8,4 +8,4 @@ class IPv8RootEndpoint(RootEndpoint, RESTEndpoint): Make the IPv8 REST endpoint Tribler-compatible. """ - path = "/ipv8" + path = "/api/ipv8" diff --git a/src/tribler/core/restapi/settings_endpoint.py b/src/tribler/core/restapi/settings_endpoint.py index b4ca16a0ad1..e5df86097c0 100644 --- a/src/tribler/core/restapi/settings_endpoint.py +++ b/src/tribler/core/restapi/settings_endpoint.py @@ -13,7 +13,7 @@ class SettingsEndpoint(RESTEndpoint): This endpoint is responsible for handing all requests regarding settings and configuration. """ - path = "/settings" + path = "/api/settings" def __init__(self, tribler_config: TriblerConfigManager, download_manager: DownloadManager = None) -> None: """ diff --git a/src/tribler/core/restapi/shutdown_endpoint.py b/src/tribler/core/restapi/shutdown_endpoint.py index e583c723586..bb8b212da5e 100644 --- a/src/tribler/core/restapi/shutdown_endpoint.py +++ b/src/tribler/core/restapi/shutdown_endpoint.py @@ -13,7 +13,7 @@ class ShutdownEndpoint(RESTEndpoint): With this endpoint you can shut down Tribler. """ - path = "/shutdown" + path = "/api/shutdown" def __init__(self, shutdown_callback: Callable[[], None]) -> None: """ diff --git a/src/tribler/core/restapi/statistics_endpoint.py b/src/tribler/core/restapi/statistics_endpoint.py index a5d67bdd824..d2c99393932 100644 --- a/src/tribler/core/restapi/statistics_endpoint.py +++ b/src/tribler/core/restapi/statistics_endpoint.py @@ -20,7 +20,7 @@ class StatisticsEndpoint(RESTEndpoint): This endpoint is responsible for handing requests regarding statistics in Tribler. """ - path = "/statistics" + path = "/api/statistics" def __init__(self, middlewares: tuple = (), client_max_size: int = MAX_REQUEST_SIZE) -> None: """ diff --git a/src/tribler/core/session.py b/src/tribler/core/session.py index e21673bff55..61eb095cbf7 100644 --- a/src/tribler/core/session.py +++ b/src/tribler/core/session.py @@ -141,10 +141,10 @@ async def start(self) -> None: await self.ipv8.start() # REST (2/2) - self.rest_manager.get_endpoint("/ipv8").initialize(self.ipv8) - self.rest_manager.get_endpoint("/statistics").ipv8 = self.ipv8 + self.rest_manager.get_endpoint("/api/ipv8").initialize(self.ipv8) + self.rest_manager.get_endpoint("/api/statistics").ipv8 = self.ipv8 if self.config.get("statistics"): - self.rest_manager.get_endpoint("/ipv8").endpoints["/overlays"].enable_overlay_statistics(True, None, True) + self.rest_manager.get_endpoint("/api/ipv8").endpoints["/overlays"].enable_overlay_statistics(True, None, True) async def shutdown(self) -> None: """ diff --git a/src/tribler/gui/event_request_manager.py b/src/tribler/gui/event_request_manager.py index cb0179f5ac2..1da373f48cb 100644 --- a/src/tribler/gui/event_request_manager.py +++ b/src/tribler/gui/event_request_manager.py @@ -72,7 +72,7 @@ def create_request(self) -> QNetworkRequest | None: logger.warning("Can't create a request: api_port is not set (%d).", self.api_port) return - url = QUrl(f"http://127.0.0.1:{self.api_port}/events") + url = QUrl(f"http://127.0.0.1:{self.api_port}/api/events") request = QNetworkRequest(url) request.setRawHeader(b'X-Api-Key', self.api_key.encode('ascii')) return request diff --git a/src/tribler/gui/network/request.py b/src/tribler/gui/network/request.py index 1bac598094a..405a6bc3d5c 100644 --- a/src/tribler/gui/network/request.py +++ b/src/tribler/gui/network/request.py @@ -61,7 +61,7 @@ def __init__( super().__init__() self.logger = logging.getLogger(self.__class__.__name__) - self.endpoint = endpoint + self.endpoint = 'api/' + endpoint self.url_params = url_params self.priority = priority diff --git a/src/tribler/test_unit/core/restapi/test_events_endpoint.py b/src/tribler/test_unit/core/restapi/test_events_endpoint.py index 2798bf9163a..cb09a649d94 100644 --- a/src/tribler/test_unit/core/restapi/test_events_endpoint.py +++ b/src/tribler/test_unit/core/restapi/test_events_endpoint.py @@ -19,7 +19,7 @@ def __init__(self, endpoint: EventsEndpoint, count: int = 1) -> None: Create a new GetEventsRequest. """ self.payload_writer = MockStreamWriter(endpoint, count=count) - super().__init__({}, "GET", "/events", payload_writer=self.payload_writer) + super().__init__({}, "GET", "/api/events", payload_writer=self.payload_writer) class MockStreamWriter(AbstractStreamWriter): diff --git a/src/tribler/test_unit/core/restapi/test_ipv8_endpoint.py b/src/tribler/test_unit/core/restapi/test_ipv8_endpoint.py index 79fcf885665..3909d2b28a3 100644 --- a/src/tribler/test_unit/core/restapi/test_ipv8_endpoint.py +++ b/src/tribler/test_unit/core/restapi/test_ipv8_endpoint.py @@ -15,7 +15,7 @@ def test_binding(self) -> None: endpoint = IPv8RootEndpoint() endpoint.setup_routes() - self.assertEqual("/ipv8", endpoint.path) + self.assertEqual("/api/ipv8", endpoint.path) self.assertIn("/asyncio", endpoint.endpoints) self.assertIn("/attestation", endpoint.endpoints) self.assertIn("/dht", endpoint.endpoints)