Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get metadata from scicat using token #353

Merged
merged 6 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions sed/loader/flash/metadata.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
The module provides a MetadataRetriever class for retrieving metadata
from a Scicatinstance based on beamtime and run IDs.
from a Scicat Instance based on beamtime and run IDs.
"""

import warnings
from typing import Dict
from typing import Optional
Expand Down Expand Up @@ -36,6 +37,7 @@ def __init__(self, metadata_config: Dict, scicat_token: str = None) -> None:
"Content-Type": "application/json",
"Accept": "application/json",
}
self.token = metadata_config["scicat_token"]

def get_metadata(
self,
Expand Down Expand Up @@ -87,40 +89,38 @@ def _get_metadata_per_run(self, pid: str) -> Dict:
Exception: If the request to retrieve metadata fails.
"""
headers2 = dict(self.headers)
headers2["Authorization"] = f"Bearer {self.token}"
headers2["Authorization"] = "Bearer {}".format(self.token)

try:
# Create the dataset URL using the PID
dataset_response = requests.get(
self._create_dataset_url_by_PID(pid),
params={"access_token": self.token},
self._create_new_dataset_url(pid),
headers=headers2,
timeout=10,
)
dataset_response.raise_for_status() # Raise HTTPError if request fails
dataset_response.raise_for_status()
# Check if response is an empty object because wrong url for older implementation
if not dataset_response.content:
dataset_response = requests.get(
self._create_old_dataset_url(pid), headers=headers2, timeout=10
)
# If the dataset request is successful, return the retrieved metadata
# as a JSON object
return dataset_response.json()
except requests.exceptions.RequestException as exception:
# If the request fails, raise warning
warnings.warn(f"Failed to retrieve metadata for PID {pid}: {str(exception)}")
print(warnings.warn(f"Failed to retrieve metadata for PID {pid}: {str(exception)}"))
return {} # Return an empty dictionary for this run

def _create_dataset_url_by_PID(self, pid: str) -> str: # pylint: disable=invalid-name
"""
Creates the dataset URL based on the PID.
def _create_old_dataset_url(self, pid: str) -> str:
return "{burl}/{url}/%2F{npid}".format(
burl=self.url, url="Datasets", npid=self._reformat_pid(pid)
)

Args:
pid (str): The PID of the run.
def _create_new_dataset_url(self, pid: str) -> str:
return "{burl}/{url}/{npid}".format(
burl=self.url, url="Datasets", npid=self._reformat_pid(pid)
)

Returns:
str: The dataset URL.

Raises:
Exception: If the token request fails.
"""
npid = pid.replace(
"/",
"%2F",
) # Replace slashes in the PID with URL-encoded slashes
url = f"{self.url}/Datasets/{npid}"
return url
def _reformat_pid(self, pid: str) -> str:
"""SciCat adds a pid-prefix + "/" but at DESY prefix = "" """
return (pid).replace("/", "%2F")
7 changes: 5 additions & 2 deletions tests/loader/flash/test_flash_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def test_create_dataset_url_by_PID():
"scicat_token": "fake_token",
}
retriever = MetadataRetriever(metadata_config)
url = retriever._create_dataset_url_by_PID("11013410/43878")
# Assuming the dataset follows the new format
pid = "11013410/43878"
url = retriever._create_new_dataset_url(pid)
expected_url = "https://example.com/Datasets/11013410%2F43878"
assert isinstance(url, str)
assert url == "https://example.com/Datasets/11013410%2F43878"
assert url == expected_url
Loading