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

Multiple fixes for new backend #49

Merged
merged 6 commits into from
Oct 3, 2023
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
46 changes: 15 additions & 31 deletions pyscicat/client.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

You missed the error handling here

f'Error retrieving token for user: {err["name"]}, {err["statusCode"]}: {err["message"]}'
and here
f' Failed log in: {err["name"]}, {err["statusCode"]}: {err["message"]}'

Also, I noticed that Client._log_in_via_auth_msad doesn't return anything, this is probably on me. Is this method still used by anyone?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the catch. I just pushed a commit for that.

I don't know if anyone uses that method, but the server supports the endpoint, so I think we have to leave it in.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import hashlib
import logging
import json
import re
from typing import Optional
from urllib.parse import urljoin, quote_plus

Expand All @@ -15,9 +14,9 @@

from pyscicat.model import (
Attachment,
CreateDatasetOrigDatablockDto,
Dataset,
Instrument,
OrigDatablock,
Proposal,
Sample,
)
Expand Down Expand Up @@ -108,25 +107,17 @@ def _call_endpoint(
endpoint: str,
data: BaseModel = None,
operation: str = "",
allow_404=False,
) -> Optional[dict]:
response = self._send_to_scicat(cmd=cmd, endpoint=endpoint, data=data)
result = response.json()

result = response.json() if len(response.content) > 0 else None
if not response.ok:
err = result.get("error", {})
if (
allow_404
and response.status_code == 404
and re.match(r"Unknown (.+ )?id", err.get("message", ""))
):
# The operation failed but because the object does not exist in SciCat.
logger.error("Error in operation %s: %s", operation, err)
return None
raise ScicatCommError(f"Error in operation {operation}: {err}")
raise ScicatCommError(f"Error in operation {operation}: {result}")

logger.info(
"Operation '%s' successful%s",
operation,
f"pid={result['pid']}" if "pid" in result else "",
f"pid={result['pid']}" if result and "pid" in result else "",
)
return result

Expand Down Expand Up @@ -200,7 +191,9 @@ def datasets_update(self, dataset: Dataset, pid: str) -> str:
"""
update_dataset = datasets_update

def datasets_origdatablock_create(self, origdatablock: OrigDatablock) -> dict:
def datasets_origdatablock_create(
self, dataset_id: str, datablockDto: CreateDatasetOrigDatablockDto
) -> dict:
"""
Create a new SciCat Dataset OrigDatablock
This function has been renamed.
Expand All @@ -223,11 +216,11 @@ def datasets_origdatablock_create(self, origdatablock: OrigDatablock) -> dict:
Raises if a non-20x message is returned

"""
endpoint = f"Datasets/{quote_plus(origdatablock.datasetId)}/origdatablocks"
endpoint = f"Datasets/{quote_plus(dataset_id)}/origdatablocks"
return self._call_endpoint(
cmd="post",
endpoint=endpoint,
data=origdatablock,
data=datablockDto,
operation="datasets_origdatablock_create",
)

Expand Down Expand Up @@ -518,7 +511,6 @@ def datasets_find(
cmd="get",
endpoint=f"Datasets/fullquery?{query}",
operation="datasets_find",
allow_404=True,
)

"""
Expand Down Expand Up @@ -559,7 +551,7 @@ def datasets_get_many(self, filter_fields: Optional[dict] = None) -> Optional[di
filter_fields = json.dumps(filter_fields)
endpoint = f'Datasets?filter={{"where":{filter_fields}}}'
return self._call_endpoint(
cmd="get", endpoint=endpoint, operation="datasets_get_many", allow_404=True
cmd="get", endpoint=endpoint, operation="datasets_get_many"
)

"""
Expand Down Expand Up @@ -595,7 +587,6 @@ def published_data_get_many(self, filter=None) -> Optional[dict]:
cmd="get",
endpoint=endpoint,
operation="published_data_get_many",
allow_404=True,
)

"""
Expand All @@ -620,7 +611,6 @@ def datasets_get_one(self, pid: str) -> Optional[dict]:
cmd="get",
endpoint=f"Datasets/{quote_plus(pid)}",
operation="datasets_get_one",
allow_404=True,
)

get_dataset_by_pid = datasets_get_one
Expand Down Expand Up @@ -659,7 +649,6 @@ def instruments_get_one(self, pid: str = None, name: str = None) -> Optional[dic
cmd="get",
endpoint=endpoint,
operation="instruments_get_one",
allow_404=True,
)

get_instrument = instruments_get_one
Expand All @@ -685,7 +674,6 @@ def samples_get_one(self, pid: str) -> Optional[dict]:
cmd="get",
endpoint=f"Samples/{quote_plus(pid)}",
operation="samples_get_one",
allow_404=True,
)

get_sample = samples_get_one
Expand All @@ -707,7 +695,7 @@ def proposals_get_one(self, pid: str = None) -> Optional[dict]:
The proposal with the requested pid
"""
return self._call_endpoint(
cmd="get", endpoint=f"Proposals/{quote_plus(pid)}", allow_404=True
cmd="get", endpoint=f"Proposals/{quote_plus(pid)}"
)

get_proposal = proposals_get_one
Expand All @@ -732,7 +720,6 @@ def datasets_origdatablocks_get_one(self, pid: str) -> Optional[dict]:
cmd="get",
endpoint=f"Datasets/{quote_plus(pid)}/origdatablocks",
operation="datasets_origdatablocks_get_one",
allow_404=True,
)

get_dataset_origdatablocks = datasets_origdatablocks_get_one
Expand All @@ -757,7 +744,6 @@ def datasets_delete(self, pid: str) -> Optional[dict]:
cmd="delete",
endpoint=f"Datasets/{quote_plus(pid)}",
operation="datasets_delete",
allow_404=True,
)

delete_dataset = datasets_delete
Expand Down Expand Up @@ -823,9 +809,8 @@ def _log_in_via_auth_msad(base_url, username, password):
verify=True,
)
if not response.ok:
err = response.json()["error"]
logger.error(
f'Error retrieving token for user: {err["name"]}, {err["statusCode"]}: {err["message"]}'
f'Error retrieving token for user: {response.json()}'
)
raise ScicatLoginError(response.content)

Expand All @@ -846,8 +831,7 @@ def get_token(base_url, username, password):
if response.ok:
return response.json()["access_token"]

err = response.json()["error"]
logger.error(
f' Failed log in: {err["name"]}, {err["statusCode"]}: {err["message"]}'
f' Failed log in: {response.json()}'
)
raise ScicatLoginError(response.content)
26 changes: 18 additions & 8 deletions pyscicat/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ class Job(MongoQueryable):
executionTime: Optional[str] = None
jobParams: Optional[dict] = None
jobStatusMessage: Optional[str] = None
datasetList: Optional[dict] = None # documentation says dict, but should maybe be list?
datasetList: Optional[
dict
] = None # documentation says dict, but should maybe be list?
jobResultObject: Optional[dict] = None # ibid.


Expand All @@ -115,7 +117,9 @@ class Dataset(Ownable):
creationTime: str # datetime
datasetName: Optional[str] = None
description: Optional[str] = None
history: Optional[List[dict]] = None # list of foreigh key ids to the Messages table
history: Optional[
List[dict]
] = None # list of foreigh key ids to the Messages table
instrumentId: Optional[str] = None
isPublished: Optional[bool] = False
keywords: Optional[List[str]] = None
Expand Down Expand Up @@ -187,25 +191,31 @@ class Datablock(Ownable):

id: Optional[str] = None
# archiveId: str = None listed in catamel model, but comes back invalid?
size: int

packedSize: Optional[int] = None
chkAlg: Optional[int] = None
version: str = None
instrumentGroup: Optional[str] = None
dataFileList: List[DataFile]
datasetId: str


class OrigDatablock(Ownable):
class CreateDatasetOrigDatablockDto(BaseModel):
"""
DTO for creating a new dataset with an original datablock
"""

chkAlg: Optional[int] = None
dataFileList: List[DataFile]
size: int


class OrigDatablock(Ownable, CreateDatasetOrigDatablockDto):
"""
An Original Datablock maps between a Dataset and contains DataFiles
"""

id: Optional[str] = None
# archiveId: str = None listed in catamel model, but comes back invalid?
size: int
instrumentGroup: Optional[str] = None
dataFileList: List[DataFile]
datasetId: str


Expand Down
25 changes: 8 additions & 17 deletions tests/test_pyscicat/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from pyscicat.model import (
Attachment,
Datablock,
CreateDatasetOrigDatablockDto,
DataFile,
Instrument,
Proposal,
Expand Down Expand Up @@ -138,14 +138,13 @@ def test_scicat_ingest():

# Datablock with DataFiles
data_file = DataFile(path="/foo/bar", size=42)
data_block = Datablock(
data_block_dto = CreateDatasetOrigDatablockDto(
size=42,
version="1",
datasetId=dataset_id,
dataFileList=[data_file],
**ownable.dict()

)
scicat.upload_dataset_origdatablock(data_block)
scicat.upload_dataset_origdatablock(dataset_id, data_block_dto)

# Attachment
attachment = Attachment(
Expand Down Expand Up @@ -187,17 +186,9 @@ def test_get_dataset():
def test_get_nonexistent_dataset():
with requests_mock.Mocker() as mock_request:
mock_request.get(
local_url + "Datasets/74",
status_code=404,
reason="Not Found",
json={
"error": {
"statusCode": 404,
"name": "Error",
"message": 'Unknown "Dataset" id "74".',
"code": "MODEL_NOT_FOUND",
}
},
local_url + "datasets/74",
status_code=200,
content=b''
)
client = from_token(base_url=local_url, token="a_token")
assert client.datasets_get_one("74") is None
Expand All @@ -206,7 +197,7 @@ def test_get_nonexistent_dataset():
def test_get_dataset_bad_url():
with requests_mock.Mocker() as mock_request:
mock_request.get(
"http://localhost:3000/api/v100/Datasets/53",
"http://localhost:3000/api/v100/datasets/53",
status_code=404,
reason="Not Found",
json={
Expand Down