Skip to content

Commit dbe0ff6

Browse files
authored
Merge pull request #113 from SciCatProject/dataset-filter
Update logging for checking dataset by pid or metadata
2 parents 8cd6604 + f0e8db1 commit dbe0ff6

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/scicat_communication.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,27 @@ def check_dataset_by_pid(
127127
stream=config.stream,
128128
verify=config.verify,
129129
)
130-
dataset_exists: bool
131-
if not response.ok:
132-
logger.info("Request url : \n%s", response.url)
130+
dataset_exists = response.ok
131+
# Log the result
132+
if response.ok:
133+
logger.info("Retrieved %s dataset(s) from SciCat", len(response.json()))
134+
logger.info("Dataset with pid %s exists.", pid)
135+
# Filter 403 error code.
136+
# Scicat returns 403 error code when the file does not exist.
137+
# This function is trying to check the existence of the dataset,
138+
# therefore 403 error code should not be considered as an error.
139+
elif response.status_code == 403:
140+
logger.info("Dataset with pid %s does not exist.", pid)
141+
else:
133142
logger.error(
134-
"Failed to check dataset existence by pid with status code: %s. "
143+
"Failed to check dataset existence by pid %s\n"
144+
"with status code: %s. \n"
135145
"Error message from scicat backend: \n%s\n"
136146
"Assuming the dataset does not exist.",
147+
pid,
137148
response.status_code,
138149
response.reason,
139150
)
140-
dataset_exists = False
141-
elif response.json():
142-
logger.info("Dataset with pid %s exists.", pid)
143-
dataset_exists = True
144-
else:
145-
logger.info("Dataset with pid %s does not exist.", pid)
146-
dataset_exists = False
147151

148152
return dataset_exists
149153

@@ -157,31 +161,35 @@ def check_dataset_by_metadata(
157161
metadata_dict = {f"scientificMetadata.{metadata_key}.value": metadata_value}
158162
filter_string = '?filter={"where":' + json.dumps(metadata_dict) + "}"
159163
url = urljoin(config.host_address, "datasets") + filter_string
160-
logger.info("Checking if dataset exists by metadata with url: %s", url)
164+
logger.info("Checking if dataset exists by metadata key: %s", metadata_key)
161165
response = _get_from_scicat(
162166
url=url,
163167
headers=config.headers,
164168
timeout=config.timeout,
165169
stream=config.stream,
166170
verify=config.verify,
167171
)
168-
dataset_exists: bool
169-
if not response.ok:
172+
dataset_exists = response.ok
173+
174+
# Log the response
175+
if response.ok:
176+
logger.info("Retrieved %s dataset(s) from SciCat", len(response.json()))
177+
logger.info("Dataset with metadata %s exists.", metadata_dict)
178+
# Filter 403 error code.
179+
# Scicat returns 403 error code when the file does not exist.
180+
# This function is trying to check the existence of the dataset,
181+
# therefore 403 error code should not be considered as an error.
182+
elif response.status_code == 403:
183+
logger.info("Dataset with metadata %s does not exist.", metadata_dict)
184+
else:
170185
logger.error(
171-
"Failed to check dataset existence by metadata key %s with status code: %s "
186+
"Failed to check dataset existence by metadata key %s \n"
187+
"with status code: %s \n"
172188
"Error message from scicat backend: \n%s\n"
173189
"Assuming the dataset does not exist.",
174190
metadata_key,
175191
response.status_code,
176192
response.reason,
177193
)
178-
dataset_exists = False
179-
elif response.json():
180-
logger.info("Retrieved %s dataset(s) from SciCat", len(response.json()))
181-
logger.info("Dataset with metadata %s exists.", metadata_dict)
182-
dataset_exists = True
183-
else:
184-
logger.info("Dataset with metadata %s does not exist.", metadata_dict)
185-
dataset_exists = False
186194

187195
return dataset_exists

0 commit comments

Comments
 (0)