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

changes from manual testing #81

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 24 additions & 9 deletions src/scicat_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
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.headers,
timeout=config.timeout
).json()
return response[field_name]
return response[field_name] if field_name else response


class ScicatDatasetAPIError(Exception):
Expand All @@ -44,9 +45,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.headers,
timeout=config.timeout,
)
result: dict = response.json()
Expand All @@ -69,16 +70,19 @@ class ScicatOrigDatablockAPIError(Exception):


def create_scicat_origdatablock(
*, origdatablock: dict, config: SciCatOptions, logger: logging.Logger
*,
origdatablock: dict,
config: SciCatOptions,
logger: logging.Logger
) -> dict:
"""
Execute a POST request to scicat to create a new 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.headers,
timeout=config.timeout,
)
result: dict = response.json()
Expand All @@ -97,3 +101,14 @@ def create_scicat_origdatablock(
result['_id'],
)
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
20 changes: 18 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 @@ -237,12 +238,23 @@ class SciCatOptions:
timeout: int = 0
stream: bool = True
verify: bool = False
urls: dict = field(default_factory=dict)

@classmethod
def from_configurations(cls, config: dict) -> "SciCatOptions":
"""Create SciCatOptions from a dictionary."""
options = cls(**config)
options.headers = {"Authorization": f"Bearer {options.token}"}
options.host = options.host.removesuffix('/') + "/"
options.headers = {
**options.headers,
**{"Authorization": f"Bearer {options.token}"}
}
options.urls = {
"datasets" : urljoin(options.host, "datasets"),
"proposals" : urljoin(options.host, "proposals"),
"origdatablocks" : urljoin(options.host, "origdatablocks"),
"instruments": urljoin(options.host, "instruments"),
}
Comment on lines +252 to +257
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 4 endpoints don't have to be configurable from the config file, no...?
Can I turn them to be a property instead?

return options


Expand Down Expand Up @@ -335,9 +347,13 @@ 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
Loading