diff --git a/swh/web/client/client.py b/swh/web/client/client.py --- a/swh/web/client/client.py +++ b/swh/web/client/client.py @@ -410,6 +410,26 @@ else: done = True + def last_visit(self, origin: str, typify: bool = True) -> Dict[str, Any]: + """Return the last visit of an origin. + + Args: + origin: the URL of a software origin + typify: if True, convert return value to pythonic types wherever + possible, otherwise return raw JSON types (default: True) + + Returns: + The last visit for that origin + + Raises: + requests.HTTPError: if HTTP request fails + + """ + query = f"origin/{origin}/visit/latest/" + r = self._call(query, http_method="get") + visit = r.json() + return typify_json(visit, ORIGIN_VISIT) if typify else visit + def known( self, swhids: Iterator[SWHIDish], **req_args ) -> Dict[CoreSWHID, Dict[Any, Any]]: diff --git a/swh/web/client/tests/api_data.py b/swh/web/client/tests/api_data.py --- a/swh/web/client/tests/api_data.py +++ b/swh/web/client/tests/api_data.py @@ -7718,6 +7718,19 @@ "snapshot_url": "https://archive.softwareheritage.org/api/1/snapshot/100de51846f317e6ab48da79d985cefa6fdefe42/" } ] + """, # NoQA: E501 # NoQA: E501 + "origin/https://github.com/NixOS/nixpkgs/visit/latest/": r""" +{ + "origin": "https://github.com/NixOS/nixpkgs", + "visit": 128, + "date": "2021-09-02T20:20:31.231786+00:00", + "status": "full", + "snapshot": "6e1fe7858066ff1a6905080ac6503a3a12b84f59", + "type": "git", + "metadata": {}, + "origin_url": "https://archive.softwareheritage.org/api/1/origin/https://github.com/NixOS/nixpkgs/get/", + "snapshot_url": "https://archive.softwareheritage.org/api/1/snapshot/6e1fe7858066ff1a6905080ac6503a3a12b84f59/" +} """, # NoQA: E501 # NoQA: E501 "origin/search/foo%20bar%20baz%20qux/?with_visit=true": r""" [ diff --git a/swh/web/client/tests/gen-api-data.sh b/swh/web/client/tests/gen-api-data.sh --- a/swh/web/client/tests/gen-api-data.sh +++ b/swh/web/client/tests/gen-api-data.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Copyright (C) 2020 The Software Heritage developers +# Copyright (C) 2020-2021 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -20,6 +20,7 @@ urls="${urls} snapshot/cabcc7d7bf639bbe1cc3b41989e1806618dd5764/?branches_count=1000&branches_from=refs/tags/v3.0-rc7" urls="${urls} origin/https://github.com/NixOS/nixpkgs/visits/?last_visit=50&per_page=10" urls="${urls} origin/https://github.com/NixOS/nixpkgs/visits/?last_visit=40&per_page=10" +urls="${urls} origin/https://github.com/NixOS/nixpkgs/visit/latest/" urls="${urls} origin/search/foo%20bar%20baz%20qux/?with_visit=true" urls="${urls} origin/search/python/?limit=5" diff --git a/swh/web/client/tests/test_web_api_client.py b/swh/web/client/tests/test_web_api_client.py --- a/swh/web/client/tests/test_web_api_client.py +++ b/swh/web/client/tests/test_web_api_client.py @@ -151,6 +151,17 @@ assert visits[7]["snapshot"] == CoreSWHID.from_string(snapshot_swhid) +def test_get_last_visit(web_api_client, web_api_mock): + visit = web_api_client.last_visit("https://github.com/NixOS/nixpkgs") + assert visit is not None + + timestamp = parse_date("2021-09-02 20:20:31.231786+00:00") + assert visit["date"] == timestamp + + snapshot_swhid = "swh:1:snp:6e1fe7858066ff1a6905080ac6503a3a12b84f59" + assert visit["snapshot"] == CoreSWHID.from_string(snapshot_swhid) + + def test_origin_search(web_api_client, web_api_mock): limited_results = list(web_api_client.origin_search("python", limit=5)) assert len(limited_results) == 5