*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit efba3713 authored by Moser, Maximilian's avatar Moser, Maximilian
Browse files

Extend wrapper interface with method for listing files for container

parent 1565e916
No related branches found
No related tags found
1 merge request!5v0.2.0
......@@ -4,7 +4,7 @@
#
import abc
from typing import Dict, Optional, Tuple
from typing import Dict, Iterable, Optional, Tuple
class BaseWrapper(abc.ABC):
......@@ -62,6 +62,14 @@ class BaseWrapper(abc.ABC):
"""
return None
@abc.abstractmethod
def list_files(self, container_id: str | int) -> Iterable[str]:
"""List all the file names attached to the container.
:param container_id: The container to list the files for.
"""
return []
@abc.abstractmethod
def url_to_parts(self, url: str) -> Tuple[Optional[str | int], Optional[str | int]]:
"""Parse the container and file from the URL.
......
......@@ -11,7 +11,7 @@ import os
import re
import urllib.parse
from collections import Counter
from typing import List, Optional, Tuple
from typing import Iterable, List, Optional, Tuple
import requests
from dbrepo.api.dto import (
......@@ -190,6 +190,12 @@ class DBRepo(BaseWrapper):
"""Clear the DBRepo client's credentials."""
self.client.username, self.client.password = None, None
def list_files(self, database_id: int) -> Iterable[str]:
"""List all tables by name for the container."""
url = f"{self.client.endpoint}/api/database/{database_id}/table"
response = requests.get(url, auth=(self.client.username, self.client.password))
return [tbl["name"] for tbl in response.json()]
def url_to_parts(self, url: str) -> Tuple[Optional[int], Optional[int]]:
"""Get the database ID and table ID from the URL."""
db_id, tbl_id = None, None
......
......@@ -9,7 +9,7 @@ import pathlib
import re
import tempfile
import urllib.parse
from typing import Optional, Tuple
from typing import Iterable, Optional, Tuple
from inveniordm_py.client import InvenioAPI as InvenioRESTClient
from inveniordm_py.files.metadata import FilesListMetadata, OutgoingStream
......@@ -60,6 +60,12 @@ class InvenioRDM(BaseWrapper):
self.client._access_token = None
self.client.session.headers.pop("Authorization", None)
def list_files(self, recid: str) -> Iterable[str]:
"""List all files for the record."""
url = f"{self.client._base_url}/records/{recid}/files"
response = self.client.session.get(url)
return [f["key"] for f in response.json()["entries"]]
def url_to_parts(self, url: str) -> Tuple[Optional[str], Optional[str]]:
"""Get the recid and filename from the URL."""
recid, filename = None, None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment