@@ -15,17 +15,23 @@ class MetadataRetriever:
15
15
on beamtime and run IDs.
16
16
"""
17
17
18
- def __init__ (self , metadata_config : Dict ) -> None :
18
+ def __init__ (self , metadata_config : Dict , scicat_token : str = None ) -> None :
19
19
"""
20
20
Initializes the MetadataRetriever class.
21
21
22
22
Args:
23
23
metadata_config (dict): Takes a dict containing
24
- at least url, username and password
24
+ at least url, and optionally token for the scicat instance.
25
+ scicat_token (str, optional): The token to use for fetching metadata.
25
26
"""
26
- self .url = metadata_config ["scicat_url" ]
27
- self .username = metadata_config ["scicat_username" ]
28
- self .password = metadata_config ["scicat_password" ]
27
+ self .token = metadata_config .get ("scicat_token" , None )
28
+ if scicat_token :
29
+ self .token = scicat_token
30
+ self .url = metadata_config .get ("scicat_url" , None )
31
+
32
+ if not self .token or not self .url :
33
+ raise ValueError ("No URL or token provided for fetching metadata from scicat." )
34
+
29
35
self .headers = {
30
36
"Content-Type" : "application/json" ,
31
37
"Accept" : "application/json" ,
@@ -80,9 +86,16 @@ def _get_metadata_per_run(self, pid: str) -> Dict:
80
86
Raises:
81
87
Exception: If the request to retrieve metadata fails.
82
88
"""
89
+ headers2 = dict (self .headers )
90
+ headers2 ["Authorization" ] = f"Bearer { self .token } "
83
91
try :
84
92
# Create the dataset URL using the PID
85
- dataset_response = requests .get (self ._create_dataset_url_by_PID (pid ), timeout = 10 )
93
+ dataset_response = requests .get (
94
+ self ._create_dataset_url_by_PID (pid ),
95
+ params = {"access_token" : self .token },
96
+ headers = headers2 ,
97
+ timeout = 10 ,
98
+ )
86
99
dataset_response .raise_for_status () # Raise HTTPError if request fails
87
100
# If the dataset request is successful, return the retrieved metadata
88
101
# as a JSON object
@@ -105,37 +118,9 @@ def _create_dataset_url_by_PID(self, pid: str) -> str: # pylint: disable=invali
105
118
Raises:
106
119
Exception: If the token request fails.
107
120
"""
108
- npid = ( "/" + pid ) .replace (
121
+ npid = pid .replace (
109
122
"/" ,
110
123
"%2F" ,
111
124
) # Replace slashes in the PID with URL-encoded slashes
112
- url = f"{ self .url } /RawDatasets /{ npid } ?access_token= { self . _get_token () } "
125
+ url = f"{ self .url } /Datasets /{ npid } "
113
126
return url
114
-
115
- def _get_token (self ) -> str :
116
- """
117
- Retrieves the access token for authentication.
118
-
119
- Returns:
120
- str: The access token.
121
-
122
- Raises:
123
- Exception: If the token request fails.
124
- """
125
- try :
126
- token_url = f"{ self .url } /Users/login"
127
- # Send a POST request to the token URL with the username and password
128
- token_response = requests .post (
129
- token_url ,
130
- headers = self .headers ,
131
- json = {"username" : self .username , "password" : self .password },
132
- timeout = 10 ,
133
- )
134
- token_response .raise_for_status ()
135
- # If the token request is successful, return the access token from the response
136
- return token_response .json ()["id" ]
137
-
138
- # Otherwise issue warning
139
- except requests .exceptions .RequestException as exception :
140
- warnings .warn (f"Failed to retrieve authentication token: { str (exception )} " )
141
- return "" # Return an empty string if token retrieval fails
0 commit comments