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: {}".format(self.args) | return "An unexpected error occurred in the Graph backend: {}".format(self.args) | ||||
class GraphArgumentException(Exception): | |||||
def __init__(self, *args, response): | |||||
super().__init__(*args) | |||||
self.response = response | |||||
class RemoteGraphClient(RPCClient): | class RemoteGraphClient(RPCClient): | ||||
"""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__(api_exception=GraphAPIError, url=url, timeout=timeout) | super().__init__(api_exception=GraphAPIError, url=url, timeout=timeout) | ||||
def raw_verb_lines(self, verb, endpoint, **kwargs): | def raw_verb_lines(self, verb, endpoint, **kwargs): | ||||
response = self.raw_verb(verb, endpoint, stream=True, **kwargs) | response = self.raw_verb(verb, endpoint, stream=True, **kwargs) | ||||
self.raise_for_status(response) | self.raise_for_status(response) | ||||
for line in response.iter_lines(): | for line in response.iter_lines(): | ||||
yield line.decode().lstrip("\n") | yield line.decode().lstrip("\n") | ||||
def get_lines(self, endpoint, **kwargs): | def get_lines(self, endpoint, **kwargs): | ||||
yield from self.raw_verb_lines("get", endpoint, **kwargs) | yield from self.raw_verb_lines("get", endpoint, **kwargs) | ||||
def raise_for_status(self, response) -> None: | |||||
if response.status_code // 100 == 4: | |||||
ardumont: neat ;) | |||||
vlorentzAuthorUnsubmitted Done Inline Actionsswh.core already does it this way vlorentz: swh.core already does it this way | |||||
raise GraphArgumentException( | |||||
response.content.decode("ascii"), response=response | |||||
) | |||||
super().raise_for_status(response) | |||||
# Web API endpoints | # Web API endpoints | ||||
def stats(self): | def stats(self): | ||||
return self.get("stats") | return self.get("stats") | ||||
def leaves(self, src, edges="*", direction="forward", max_edges=0): | def leaves(self, src, edges="*", direction="forward", max_edges=0): | ||||
return self.get_lines( | return self.get_lines( | ||||
"leaves/{}".format(src), | "leaves/{}".format(src), | ||||
▲ Show 20 Lines • Show All 72 Lines • Show Last 20 Lines |
neat ;)