Skip to content

Commit

Permalink
Merge pull request #81 from SciCatProject/manual_testing
Browse files Browse the repository at this point in the history
changes from manual testing
  • Loading branch information
YooSunYoung authored Oct 31, 2024
2 parents abf6876 + 5cc22a7 commit f7361e5
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 112 deletions.
10 changes: 8 additions & 2 deletions resources/config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@
"scicat": {
"host": "https://scicat.host",
"token": "JWT_TOKEN",
"headers": {},
"additional_headers": {},
"timeout": 0,
"stream": true,
"verify": false
"verify": false,
"api_endpoints": {
"datasets": "datasets",
"proposals": "proposals",
"origdatablocks": "origdatablocks",
"instruments": "instruments"
}
}
}
36 changes: 24 additions & 12 deletions src/scicat_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
def retrieve_value_from_scicat(
*,
config: SciCatOptions,
variable_url: str, # It should be already rendered from variable_recipe["url"]
scicat_endpoint_url: str, # It should be already rendered
# from variable_recipe["url"]
field_name: str, # variable_recipe["field"]
) -> str:
url = config.host.removesuffix('/') + variable_url
response: dict = requests.get(
url, headers={"token": config.token}, timeout=config.timeout
scicat_endpoint_url, headers=config.additional_headers, timeout=config.timeout
).json()
return response[field_name]
return response[field_name] if field_name else response


class ScicatDatasetAPIError(Exception):
Expand Down Expand Up @@ -52,9 +52,9 @@ def create_scicat_dataset(
"""
logger.info("Sending POST request to create new dataset")
response = _post_to_scicat(
url=urljoin(config.host, "datasets"),
url=config.urls.datasets,
posting_obj=dataset,
headers={"token": config.token, **config.headers},
headers=config.additional_headers,
timeout=config.timeout,
)
result: dict = response.json()
Expand Down Expand Up @@ -84,9 +84,9 @@ def create_scicat_origdatablock(
"""
logger.info("Sending POST request to create new origdatablock")
response = _post_to_scicat(
url=urljoin(config.host, "origdatablocks"),
url=config.urls.origdatablocks,
posting_obj=origdatablock,
headers={"token": config.token, **config.headers},
headers=config.additional_headers,
timeout=config.timeout,
)
result: dict = response.json()
Expand All @@ -107,12 +107,24 @@ def create_scicat_origdatablock(
return result


def render_full_url(
url: str,
config: SciCatOptions,
) -> str:
if not url.startswith("http://") and not url.startswith("https://"):
for endpoint in config.urls.keys():
if url.startswith(endpoint):
url = url.replace(endpoint, config.urls[endpoint])
break
return url


def check_dataset_by_pid(
pid: str, config: SciCatOptions, logger: logging.Logger
) -> bool:
response = _get_from_scicat(
url=urljoin(config.host, f"datasets/{quote(pid)}"),
headers=config.headers,
url=urljoin(config.host_address, f"datasets/{quote(pid)}"),
headers=config.additional_headers,
timeout=config.timeout,
stream=config.stream,
verify=config.verify,
Expand Down Expand Up @@ -145,11 +157,11 @@ def check_dataset_by_metadata(
) -> bool:
metadata_dict = {f"scientificMetadata.{metadata_key}.value": metadata_value}
filter_string = '?filter={"where":' + json.dumps(metadata_dict) + "}"
url = urljoin(config.host, "datasets") + filter_string
url = urljoin(config.host_address, "datasets") + filter_string
logger.info("Checking if dataset exists by metadata with url: %s", url)
response = _get_from_scicat(
url=url,
headers=config.headers,
headers=config.additional_headers,
timeout=config.timeout,
stream=config.stream,
verify=config.verify,
Expand Down
46 changes: 44 additions & 2 deletions src/scicat_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pathlib import Path
from types import MappingProxyType
from typing import Any, TypeVar, get_origin
from urllib.parse import urljoin


def _load_config(config_file: Path) -> dict:
Expand Down Expand Up @@ -231,14 +232,54 @@ class DatasetOptions:
default_access_groups: list[str] = field(default_factory=default_access_groups)


@dataclass(kw_only=True)
class _ScicatAPIURLs:
datasets: str
proposals: str
origdatablocks: str
instruments: str


@dataclass(kw_only=True)
class ScicatEndpoints:
datasets: str = "datasets"
proposals: str = "proposals"
origdatablocks: str = "origdatablocks"
instruments: str = "instruments"


@dataclass(kw_only=True)
class SciCatOptions:
host: str = "https://scicat.host"
token: str = "JWT_TOKEN"
headers: dict = field(default_factory=dict)
additional_headers: dict = field(default_factory=dict)
timeout: int = 0
stream: bool = True
verify: bool = False
api_endpoints: ScicatEndpoints = field(default_factory=ScicatEndpoints)

@property
def urls(self) -> _ScicatAPIURLs:
return _ScicatAPIURLs(
datasets=urljoin(self.host_address, self.api_endpoints.datasets),
proposals=urljoin(self.host_address, self.api_endpoints.proposals),
origdatablocks=urljoin(
self.host_address, self.api_endpoints.origdatablocks
),
instruments=urljoin(self.host_address, self.api_endpoints.instruments),
)

@property
def headers(self) -> dict:
return {
**self.additional_headers,
**{"Authorization": f"Bearer {self.token}"},
}

@property
def host_address(self) -> str:
"""Return the host address ready to be used."""
return self.host.removesuffix('/') + "/"


@dataclass(kw_only=True)
Expand Down Expand Up @@ -330,9 +371,10 @@ def merge_config_and_input_args(


def _validate_config_file(target_type: type[T], config_file: Path) -> T:
config = {**_load_config(config_file), "config_file": config_file.as_posix()}
return build_dataclass(
target_type,
{**_load_config(config_file), "config_file": config_file.as_posix()},
config,
)


Expand Down
Loading

0 comments on commit f7361e5

Please sign in to comment.