Changeset View
Changeset View
Standalone View
Standalone View
swh/graph/server/app.py
Show All 11 Lines | |||||
from collections import deque | from collections import deque | ||||
import json | import json | ||||
from typing import Optional | from typing import Optional | ||||
import aiohttp.web | import aiohttp.web | ||||
from swh.core.api.asynchronous import RPCServerApp | from swh.core.api.asynchronous import RPCServerApp | ||||
from swh.model.exceptions import ValidationError | from swh.model.exceptions import ValidationError | ||||
from swh.model.identifiers import SWHID_TYPES | from swh.model.identifiers import EXTENDED_SWHID_TYPES | ||||
try: | try: | ||||
from contextlib import asynccontextmanager | from contextlib import asynccontextmanager | ||||
except ImportError: | except ImportError: | ||||
# Compatibility with 3.6 backport | # Compatibility with 3.6 backport | ||||
from async_generator import asynccontextmanager # type: ignore | from async_generator import asynccontextmanager # type: ignore | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | def get_direction(self): | ||||
raise aiohttp.web.HTTPBadRequest(text=f"invalid direction: {s}") | raise aiohttp.web.HTTPBadRequest(text=f"invalid direction: {s}") | ||||
return s | return s | ||||
def get_edges(self): | def get_edges(self): | ||||
"""Validate HTTP query parameter `edges`, i.e., edge restrictions""" | """Validate HTTP query parameter `edges`, i.e., edge restrictions""" | ||||
s = self.request.query.get("edges", "*") | s = self.request.query.get("edges", "*") | ||||
if any( | if any( | ||||
[ | [ | ||||
node_type != "*" and node_type not in SWHID_TYPES | node_type != "*" and node_type not in EXTENDED_SWHID_TYPES | ||||
for edge in s.split(":") | for edge in s.split(":") | ||||
for node_type in edge.split(",", maxsplit=1) | for node_type in edge.split(",", maxsplit=1) | ||||
] | ] | ||||
): | ): | ||||
raise aiohttp.web.HTTPBadRequest(text=f"invalid edge restriction: {s}") | raise aiohttp.web.HTTPBadRequest(text=f"invalid edge restriction: {s}") | ||||
return s | return s | ||||
def get_traversal(self): | def get_traversal(self): | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | class VisitNodesView(SimpleTraversalView): | ||||
simple_traversal_type = "visit_nodes" | simple_traversal_type = "visit_nodes" | ||||
class WalkView(StreamingGraphView): | class WalkView(StreamingGraphView): | ||||
async def prepare_response(self): | async def prepare_response(self): | ||||
src = self.request.match_info["src"] | src = self.request.match_info["src"] | ||||
dst = self.request.match_info["dst"] | dst = self.request.match_info["dst"] | ||||
self.src_node = self.node_of_swhid(src) | self.src_node = self.node_of_swhid(src) | ||||
if dst not in SWHID_TYPES: | if dst not in EXTENDED_SWHID_TYPES: | ||||
self.dst_thing = self.node_of_swhid(dst) | self.dst_thing = self.node_of_swhid(dst) | ||||
else: | else: | ||||
self.dst_thing = dst | self.dst_thing = dst | ||||
self.edges = self.get_edges() | self.edges = self.get_edges() | ||||
self.direction = self.get_direction() | self.direction = self.get_direction() | ||||
self.algo = self.get_traversal() | self.algo = self.get_traversal() | ||||
self.limit = self.get_limit() | self.limit = self.get_limit() | ||||
▲ Show 20 Lines • Show All 112 Lines • Show Last 20 Lines |