Changeset View
Changeset View
Standalone View
Standalone View
swh/graph/client.py
Show All 9 Lines | |||||
class GraphAPIError(Exception): | class GraphAPIError(Exception): | ||||
"""Graph API Error""" | """Graph API Error""" | ||||
def __str__(self): | def __str__(self): | ||||
return ('An unexpected error occurred in the Graph backend: {}' | return ('An unexpected error occurred in the Graph backend: {}' | ||||
.format(self.args)) | .format(self.args)) | ||||
class OutputFmt(Enum): | |||||
ONLY_NODES = 1 | |||||
ONLY_PATHS = 2 | |||||
NODES_AND_PATHS = 3 | |||||
class RemoteGraphClient(SWHRemoteAPI): | class RemoteGraphClient(SWHRemoteAPI): | ||||
"""Client to the Software Heritage Graph.""" | """Client to the Software Heritage Graph.""" | ||||
def __init__(self, url, timeout=None): | def __init__(self, url, timeout=None): | ||||
super().__init__( | super().__init__( | ||||
api_exception=GraphAPIError, url=url, timeout=timeout) | api_exception=GraphAPIError, url=url, timeout=timeout) | ||||
# Web API endpoints | # Web API endpoints | ||||
Show All 10 Lines | def neighbors(self, src, edges="*", direction="forward"): | ||||
params={ | params={ | ||||
'edges': edges, | 'edges': edges, | ||||
'direction': direction | 'direction': direction | ||||
}) | }) | ||||
def stats(self): | def stats(self): | ||||
return self.get('stats') | return self.get('stats') | ||||
def visit(self, src, edges="*", direction="forward", | def visit_nodes(self, src, edges="*", direction="forward"): | ||||
output_fmt=OutputFmt.NODES_AND_PATHS): | return self.get('visit/nodes/{}'.format(src), | ||||
subendpoint = "" | params={ | ||||
if output_fmt is OutputFmt.ONLY_NODES: | 'edges': edges, | ||||
subendpoint = "/nodes" | 'direction': direction | ||||
elif output_fmt is OutputFmt.ONLY_PATHS: | }) | ||||
subendpoint = "/paths" | |||||
return self.get('visit{}/{}'.format(subendpoint, src), | def visit_paths(self, src, edges="*", direction="forward"): | ||||
return self.get('visit/paths/{}'.format(src), | |||||
params={ | params={ | ||||
'edges': edges, | 'edges': edges, | ||||
'direction': direction | 'direction': direction | ||||
}) | }) | ||||
def walk(self, src, dst, edges="*", traversal="dfs", direction="forward"): | def walk(self, src, dst, edges="*", traversal="dfs", direction="forward"): | ||||
return self.get('walk/{}/{}'.format(src, dst), | return self.get('walk/{}/{}'.format(src, dst), | ||||
params={ | params={ | ||||
'edges': edges, | 'edges': edges, | ||||
'traversal': traversal, | 'traversal': traversal, | ||||
'direction': direction | 'direction': direction | ||||
}) | }) |