Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/origin.py
Show All 15 Lines | 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.common import archive | from swh.web.common import archive | ||||
from swh.web.common.exc import BadInputExc | from swh.web.common.exc import BadInputExc | ||||
from swh.web.common.origin_visits import get_origin_visits | from swh.web.common.origin_visits import get_origin_visits | ||||
from swh.web.common.typing import OriginInfo | |||||
from swh.web.common.utils import origin_visit_types, reverse | from swh.web.common.utils import origin_visit_types, reverse | ||||
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 106 Lines • ▼ Show 20 Lines | def api_origin(request: Request, origin_url: str): | ||||
""" | """ | ||||
ori_dict = {"url": origin_url} | ori_dict = {"url": origin_url} | ||||
error_msg = "Origin with url %s not found." % ori_dict["url"] | error_msg = "Origin with url %s not found." % ori_dict["url"] | ||||
return api_lookup( | return api_lookup( | ||||
archive.lookup_origin, | archive.lookup_origin, | ||||
ori_dict, | ori_dict, | ||||
lookup_similar_urls=False, | |||||
notfound_msg=error_msg, | notfound_msg=error_msg, | ||||
enrich_fn=enrich_origin, | enrich_fn=enrich_origin, | ||||
request=request, | request=request, | ||||
) | ) | ||||
def _visit_types() -> str: | def _visit_types() -> str: | ||||
docstring = "" | docstring = "" | ||||
▲ Show 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | .. http:get:: /api/1/origin/(origin_url)/visits/ | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`origin/https://github.com/hylang/hy/visits/` | :swh_web_api:`origin/https://github.com/hylang/hy/visits/` | ||||
""" | """ | ||||
result = {} | result = {} | ||||
origin_query = {"url": origin_url} | origin_query = OriginInfo(url=origin_url) | ||||
notfound_msg = "No origin {} found".format(origin_url) | notfound_msg = "No origin {} found".format(origin_url) | ||||
url_args_next = {"origin_url": origin_url} | url_args_next = {"origin_url": origin_url} | ||||
per_page = int(request.query_params.get("per_page", "10")) | per_page = int(request.query_params.get("per_page", "10")) | ||||
last_visit_str = request.query_params.get("last_visit") | last_visit_str = request.query_params.get("last_visit") | ||||
last_visit = int(last_visit_str) if last_visit_str else None | last_visit = int(last_visit_str) if last_visit_str else None | ||||
def _lookup_origin_visits(origin_query, last_visit=last_visit, per_page=per_page): | def _lookup_origin_visits(origin_query, last_visit=last_visit, per_page=per_page): | ||||
all_visits = get_origin_visits(origin_query) | all_visits = get_origin_visits(origin_query, lookup_similar_urls=False) | ||||
all_visits.reverse() | all_visits.reverse() | ||||
visits = [] | visits = [] | ||||
if not last_visit: | if not last_visit: | ||||
visits = all_visits[:per_page] | visits = all_visits[:per_page] | ||||
else: | else: | ||||
for i, v in enumerate(all_visits): | for i, v in enumerate(all_visits): | ||||
if v["visit"] == last_visit: | if v["visit"] == last_visit: | ||||
visits = all_visits[i + 1 : i + 1 + per_page] | visits = all_visits[i + 1 : i + 1 + per_page] | ||||
break | break | ||||
for v in visits: | for v in visits: | ||||
yield v | yield v | ||||
results = api_lookup( | results = api_lookup( | ||||
_lookup_origin_visits, | _lookup_origin_visits, | ||||
douardda: I don't get why this (and similar snippets below) is related to this diff's description.
| |||||
Done Inline ActionsAll API endpoints looking for an origin should have the same behavior, see added test. But I will move that origin existence check in the archive module instead. anlambert: All API endpoints looking for an origin should have the same behavior, see added test.
But I… | |||||
origin_query, | origin_query, | ||||
notfound_msg=notfound_msg, | notfound_msg=notfound_msg, | ||||
enrich_fn=partial( | enrich_fn=partial( | ||||
enrich_origin_visit, with_origin_link=False, with_origin_visit_link=True | enrich_origin_visit, with_origin_link=False, with_origin_visit_link=True | ||||
), | ), | ||||
request=request, | request=request, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 47 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, | |||||
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( | ||||
enrich_origin_visit, with_origin_link=True, with_origin_visit_link=False | enrich_origin_visit, with_origin_link=True, with_origin_visit_link=False | ||||
), | ), | ||||
request=request, | request=request, | ||||
) | ) | ||||
Show All 20 Lines | .. http:get:: /api/1/origin/(origin_url)/visit/(visit_id)/ | ||||
archive | archive | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`origin/https://github.com/hylang/hy/visit/1/` | :swh_web_api:`origin/https://github.com/hylang/hy/visit/1/` | ||||
""" | """ | ||||
return api_lookup( | return api_lookup( | ||||
archive.lookup_origin_visit, | archive.lookup_origin_visit, | ||||
origin_url, | origin_url, | ||||
int(visit_id), | int(visit_id), | ||||
lookup_similar_urls=False, | |||||
notfound_msg=("No visit {} for origin {} found".format(visit_id, origin_url)), | notfound_msg=("No visit {} for origin {} found".format(visit_id, origin_url)), | ||||
enrich_fn=partial( | enrich_fn=partial( | ||||
enrich_origin_visit, with_origin_link=True, with_origin_visit_link=False | enrich_origin_visit, with_origin_link=True, with_origin_visit_link=False | ||||
), | ), | ||||
request=request, | request=request, | ||||
) | ) | ||||
Show All 33 Lines |
I don't get why this (and similar snippets below) is related to this diff's description.