Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/origin.py
Show All 12 Lines | |||||
from swh.web.api.apidoc import api_doc, format_docstring | from swh.web.api.apidoc import api_doc, format_docstring | ||||
from swh.web.api.apiurls import api_route | from swh.web.api.apiurls import api_route | ||||
from swh.web.api.utils import ( | from swh.web.api.utils import ( | ||||
enrich_origin, | enrich_origin, | ||||
enrich_origin_search_result, | enrich_origin_search_result, | ||||
enrich_origin_visit, | enrich_origin_visit, | ||||
) | ) | ||||
from swh.web.api.views.utils import api_lookup | from swh.web.api.views.utils import api_lookup | ||||
from swh.web.utils import archive, origin_visit_types, reverse | from swh.web.utils import archive, graphql, origin_visit_types, reverse | ||||
from swh.web.utils.exc import BadInputExc | from swh.web.utils.exc import BadInputExc | ||||
from swh.web.utils.origin_visits import get_origin_visits | from swh.web.utils.origin_visits import get_origin_visits | ||||
from swh.web.utils.typing import OriginInfo | from swh.web.utils.typing import OriginInfo | ||||
DOC_RETURN_ORIGIN = """ | DOC_RETURN_ORIGIN = """ | ||||
:>json string origin_visits_url: link to in order to get information | :>json string origin_visits_url: link to in order to get information | ||||
about the visits for that origin | about the visits for that origin | ||||
:>json string url: the origin canonical url | :>json string url: the origin canonical url | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | def api_origins(request: Request): | ||||
page_token = request.query_params.get("page_token", None) | page_token = request.query_params.get("page_token", None) | ||||
limit = min(int(request.query_params.get("origin_count", "100")), 10000) | limit = min(int(request.query_params.get("origin_count", "100")), 10000) | ||||
page_result = archive.lookup_origins(page_token, limit) | page_result = archive.lookup_origins(page_token, limit) | ||||
origins = [enrich_origin(o, request=request) for o in page_result.results] | origins = [enrich_origin(o, request=request) for o in page_result.results] | ||||
next_page_token = page_result.next_page_token | next_page_token = page_result.next_page_token | ||||
headers: Dict[str, str] = {} | headers: Dict[str, str] = {} | ||||
if next_page_token is not None: | if next_page_token is not None: | ||||
headers["link-next"] = reverse( | headers["link-next"] = reverse( | ||||
"api-1-origins", | "api-1-origins", | ||||
query_params={"page_token": next_page_token, "origin_count": str(limit)}, | query_params={"page_token": next_page_token, "origin_count": str(limit)}, | ||||
request=request, | request=request, | ||||
) | ) | ||||
return {"results": origins, "headers": headers} | return {"results": origins, "headers": headers} | ||||
Show All 18 Lines | .. http:get:: /api/1/origin/(origin_url)/get/ | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`origin/https://github.com/python/cpython/get/` | :swh_web_api:`origin/https://github.com/python/cpython/get/` | ||||
""" | """ | ||||
ori_dict = {"url": origin_url} | query = """ | ||||
query GetOrigin($url: String!) { | |||||
error_msg = "Origin with url %s not found." % ori_dict["url"] | origin(url: $url) { | ||||
url | |||||
return api_lookup( | } | ||||
archive.lookup_origin, | } | ||||
ori_dict, | """ | ||||
lookup_similar_urls=False, | return graphql.get_one( | ||||
notfound_msg=error_msg, | query, | ||||
enrich_fn=enrich_origin, | {"url": origin_url}, | ||||
query_root="origin", | |||||
enrich=enrich_origin, | |||||
error_msg=f"Origin with url {origin_url} not found.", | |||||
request=request, | request=request, | ||||
) | ) | ||||
def _visit_types() -> str: | def _visit_types() -> str: | ||||
docstring = "" | docstring = "" | ||||
# available visit types are queried using swh-search so we do it in a try | # available visit types are queried using swh-search so we do it in a try | ||||
# block in case of failure (for instance in docker environment when | # block in case of failure (for instance in docker environment when | ||||
▲ Show 20 Lines • Show All 258 Lines • ▼ Show 20 Lines | .. http:get:: /api/1/origin/(origin_url)/visit/latest/ | ||||
archive | archive | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`origin/https://github.com/hylang/hy/visit/latest/` | :swh_web_api:`origin/https://github.com/hylang/hy/visit/latest/` | ||||
""" | """ | ||||
require_snapshot = request.query_params.get("require_snapshot", "false") | require_snapshot = request.query_params.get("require_snapshot", "false") | ||||
return api_lookup( | return api_lookup( | ||||
archive.lookup_origin_visit_latest, | archive.lookup_origin_visit_latest, | ||||
origin_url, | origin_url, | ||||
bool(strtobool(require_snapshot)), | bool(strtobool(require_snapshot)), | ||||
lookup_similar_urls=False, | lookup_similar_urls=False, | ||||
notfound_msg=("No visit for origin {} found".format(origin_url)), | notfound_msg=("No visit for origin {} found".format(origin_url)), | ||||
enrich_fn=partial( | enrich_fn=partial( | ||||
▲ Show 20 Lines • Show All 81 Lines • Show Last 20 Lines |