Skip to content

Commit 454ec9f

Browse files
authored
Remove unions in params and delete request body (#13)
* removed tuples from all route parameters and base model definitions * removed unions in request body parameters, removed json body from any delete request * abstracted some parts of the code and made idx/tag query based parameters instead of body. Passed the responsibility of dealing with this to the service. Removed old comments. * remove all unions, inc in constraints * updated logic for tag/idx in query params for hkl routes also * minor changes to pass flake8 tests
1 parent 93d6ef7 commit 454ec9f

File tree

12 files changed

+214
-155
lines changed

12 files changed

+214
-155
lines changed

src/diffcalc_API/errors/ub.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union
1+
from typing import Optional, Union
22

33
import numpy as np
44

@@ -14,19 +14,40 @@ class ErrorCodes(ErrorCodesBase):
1414
INVALID_SET_LATTICE_PARAMS = 400
1515
REFERENCE_RETRIEVAL_ERROR = 403
1616
INVALID_PROPERTY = 400
17+
NO_TAG_OR_IDX_PROVIDED = 400
18+
BOTH_TAG_OR_IDX_PROVIDED = 400
1719

1820

1921
responses = {code: ALL_RESPONSES[code] for code in np.unique(ErrorCodes.all_codes())}
2022

2123

24+
class NoTagOrIdxProvidedError(DiffcalcAPIException):
25+
def __init__(self):
26+
self.detail = (
27+
"One of the following must be provided as a query parameter:"
28+
+ " tag (string), index (integer)"
29+
)
30+
self.status_code = ErrorCodes.NO_TAG_OR_IDX_PROVIDED
31+
32+
33+
class BothTagAndIdxProvidedError(DiffcalcAPIException):
34+
def __init__(self):
35+
self.detail = (
36+
"both the tag and index have been provided. These are identifiers"
37+
+ " for a specific orientation or reflection, and so both cannot be"
38+
+ " used. Retry with just one tag or index query parameter."
39+
)
40+
self.status_code = ErrorCodes.BOTH_TAG_OR_IDX_PROVIDED
41+
42+
2243
class InvalidSetLatticeParamsError(DiffcalcAPIException):
2344
def __init__(self):
2445
self.detail = ("please provide lattice parameters in request body",)
2546
self.status_code = ErrorCodes.INVALID_SET_LATTICE_PARAMS
2647

2748

2849
class ReferenceRetrievalError(DiffcalcAPIException):
29-
def __init__(self, handle: Union[str, int], reference_type: str) -> None:
50+
def __init__(self, handle: Optional[Union[str, int]], reference_type: str) -> None:
3051
self.detail = f"cannot retrieve {reference_type} with tag or index {handle}"
3152
self.status_code = ErrorCodes.REFERENCE_RETRIEVAL_ERROR
3253

src/diffcalc_API/examples/ub.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,23 @@
1414
"hkl": HklModel(h=0, k=0, l=1),
1515
"position": PositionModel(mu=7.31, delta=0.0, nu=10.62, eta=0, chi=0.0, phi=0),
1616
"energy": 12.39842,
17-
"tag": "refl1",
1817
}
1918
)
2019

2120
edit_reflection: EditReflectionParams = EditReflectionParams(
22-
**{"energy": 12.45, "tag_or_idx": "refl1"}
21+
**{"energy": 12.45, "set_tag": "refl2"}
2322
)
2423

2524
add_orientation: AddOrientationParams = AddOrientationParams(
2625
**{
2726
"hkl": HklModel(h=0, k=1, l=0),
2827
"xyz": XyzModel(x=0, y=1, z=0),
29-
"tag": "plane",
3028
}
3129
)
3230

3331
edit_orientation: EditOrientationParams = EditOrientationParams(
3432
**{
3533
"hkl": HklModel(h=0, k=1, l=0),
36-
"tag_or_idx": "plane",
3734
}
3835
)
3936

src/diffcalc_API/models/ub.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, Union
1+
from typing import Optional
22

33
from pydantic import BaseModel
44

@@ -25,7 +25,7 @@ class PositionModel(BaseModel):
2525

2626

2727
class SetLatticeParams(BaseModel):
28-
system: Optional[Union[str, float]] = None
28+
system: Optional[str] = None
2929
a: Optional[float] = None
3030
b: Optional[float] = None
3131
c: Optional[float] = None
@@ -38,29 +38,27 @@ class AddReflectionParams(BaseModel):
3838
hkl: HklModel
3939
position: PositionModel
4040
energy: float
41-
tag: Optional[str] = None
4241

4342

4443
class AddOrientationParams(BaseModel):
4544
hkl: HklModel
4645
xyz: XyzModel
4746
position: Optional[PositionModel] = None
48-
tag: Optional[str] = None
4947

5048

5149
class EditReflectionParams(BaseModel):
5250
hkl: Optional[HklModel] = None
5351
position: Optional[PositionModel] = None
5452
energy: Optional[float] = None
55-
tag_or_idx: Union[int, str]
53+
set_tag: Optional[str] = None
5654

5755

5856
class EditOrientationParams(BaseModel):
5957
hkl: Optional[HklModel] = None
6058
xyz: Optional[XyzModel] = None
6159
position: Optional[PositionModel] = None
62-
tag_or_idx: Union[int, str]
60+
set_tag: Optional[str] = None
6361

6462

65-
class DeleteParams(BaseModel):
66-
tag_or_idx: Union[int, str]
63+
def select_idx_or_tag_str(idx: Optional[int], tag: Optional[str]):
64+
return f"index {idx}" if idx is not None else f"tag {tag}"

src/diffcalc_API/routes/constraints.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Optional, Union
1+
from typing import Dict, Optional
22

33
from fastapi import APIRouter, Body, Depends, Query
44

@@ -21,9 +21,7 @@ async def get_constraints(
2121
@router.post("/{name}")
2222
async def set_constraints(
2323
name: str,
24-
constraints: Dict[str, Union[float, bool]] = Body(
25-
example={"qaz": 0, "alpha": 0, "eta": 0}
26-
),
24+
constraints: Dict[str, float] = Body(example={"qaz": 0, "alpha": 0, "eta": 0}),
2725
store: HklCalcStore = Depends(get_store),
2826
collection: Optional[str] = Query(default=None, example="B07"),
2927
):
@@ -58,7 +56,7 @@ async def remove_constraint(
5856
async def set_constraint(
5957
name: str,
6058
property: str,
61-
value: Union[float, bool] = Body(...),
59+
value: float = Body(...),
6260
store: HklCalcStore = Depends(get_store),
6361
collection: Optional[str] = Query(default=None, example="B07"),
6462
):

src/diffcalc_API/routes/hkl.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Optional, Union
1+
from typing import List, Optional
22

33
from fastapi import APIRouter, Depends, Query
44

@@ -12,19 +12,22 @@
1212
@router.get("/{name}/UB")
1313
async def calculate_ub(
1414
name: str,
15-
first_tag: Optional[Union[int, str]] = Query(default=None, example="refl1"),
16-
second_tag: Optional[Union[int, str]] = Query(default=None, example="plane"),
15+
tag1: Optional[str] = Query(default=None, example="refl1"),
16+
idx1: Optional[int] = Query(default=None),
17+
tag2: Optional[str] = Query(default=None, example="plane"),
18+
idx2: Optional[int] = Query(default=None),
1719
store: HklCalcStore = Depends(get_store),
1820
collection: Optional[str] = Query(default=None, example="B07"),
1921
):
20-
content = await service.calculate_ub(name, first_tag, second_tag, store, collection)
22+
content = await service.calculate_ub(
23+
name, store, collection, tag1, idx1, tag2, idx2
24+
)
2125
return {"payload": content}
2226

2327

2428
@router.get("/{name}/position/lab")
2529
async def lab_position_from_miller_indices(
2630
name: str,
27-
# miller_indices: List[float] = Query(example=[0, 0, 1]),
2831
miller_indices: HklModel = Depends(),
2932
wavelength: float = Query(..., example=1.0),
3033
store: HklCalcStore = Depends(get_store),
@@ -40,10 +43,7 @@ async def lab_position_from_miller_indices(
4043
@router.get("/{name}/position/hkl")
4144
async def miller_indices_from_lab_position(
4245
name: str,
43-
pos: PositionModel = Depends(
44-
# ..., example={"mu": 7.31, "delta": 0, "nu": 10.62,
45-
# "eta": 0, "chi": 0, "phi": 0}
46-
),
46+
pos: PositionModel = Depends(),
4747
wavelength: float = Query(..., example=1.0),
4848
store: HklCalcStore = Depends(get_store),
4949
collection: Optional[str] = Query(default=None, example="B07"),
@@ -59,7 +59,7 @@ async def scan_hkl(
5959
name: str,
6060
start: List[float] = Query(..., example=[1, 0, 1]),
6161
stop: List[float] = Query(..., example=[2, 0, 2]),
62-
inc: List[float] = Query(..., example=(0.1, 0, 0.1)),
62+
inc: List[float] = Query(..., example=[0.1, 0, 0.1]),
6363
wavelength: float = Query(..., example=1),
6464
store: HklCalcStore = Depends(get_store),
6565
collection: Optional[str] = Query(default=None, example="B07"),
@@ -76,7 +76,6 @@ async def scan_wavelength(
7676
start: float = Query(..., example=1.0),
7777
stop: float = Query(..., example=2.0),
7878
inc: float = Query(..., example=0.2),
79-
# hkl: PositionType = Query(..., example=(1, 0, 1)),
8079
hkl: HklModel = Depends(),
8180
store: HklCalcStore = Depends(get_store),
8281
collection: Optional[str] = Query(default=None, example="B07"),
@@ -94,7 +93,6 @@ async def scan_constraint(
9493
start: float = Query(..., example=1),
9594
stop: float = Query(..., example=4),
9695
inc: float = Query(..., example=1),
97-
# hkl: PositionType = Query(..., example=(1, 0, 1)),
9896
hkl: HklModel = Depends(),
9997
wavelength: float = Query(..., example=1.0),
10098
store: HklCalcStore = Depends(get_store),

0 commit comments

Comments
 (0)