Skip to content

Commit

Permalink
add get some blparams methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dperl-dls committed Jul 9, 2024
1 parent 69e9e83 commit d462094
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/daq_config_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uvicorn
from fastapi import FastAPI, Request, Response, status
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from redis import Redis

from .beamline_parameters import (
Expand Down Expand Up @@ -50,11 +51,18 @@ def get_beamline_parameter(param: str):
return {param: BEAMLINE_PARAMS.params.get(param)}


class ParamList(BaseModel):
param_list: list[str]


@app.get(ENDPOINTS.BL_PARAM)
def get_all_beamline_parameters():
def get_all_beamline_parameters(param_list_data: ParamList | None):
"""Get a dict of all the current beamline parameters."""
print(param_list_data)
assert BEAMLINE_PARAMS is not None
return BEAMLINE_PARAMS.params
if param_list_data is None:
return BEAMLINE_PARAMS.params
return {k: BEAMLINE_PARAMS.params.get(k) for k in param_list_data.param_list}


@app.get(ENDPOINTS.FEATURE)
Expand Down
16 changes: 13 additions & 3 deletions src/daq_config_server/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from logging import Logger, getLogger
from typing import TypeVar
from typing import Any, TypeVar

import requests

from .constants import ENDPOINTS

T = TypeVar("T")
BlParamDType = str | int | float | bool


class ConfigServer:
Expand All @@ -18,13 +19,22 @@ def _get(
endpoint: str,
item: str | None = None,
options: dict[str, str] | None = None,
data: dict[str, Any] | None = None,
):
r = requests.get(self._url + endpoint + (f"/{item}" if item else ""), options)
r = requests.get(
self._url + endpoint + (f"/{item}" if item else ""), options, json=data
)
return r.json()

def get_beamline_param(self, param: str) -> str | int | float | bool | None:
def get_beamline_param(self, param: str) -> BlParamDType | None:
return self._get(ENDPOINTS.BL_PARAM, param).get(param)

def get_some_beamline_params(self, params: list[str]) -> dict[str, BlParamDType]:
return self._get(ENDPOINTS.BL_PARAM, data={"param_list": params})

def get_all_beamline_params(self) -> dict[str, BlParamDType]:
return self._get(ENDPOINTS.BL_PARAM)

def get_feature_flag(self, flag_name: str) -> bool | None:
"""Get the specified feature flag; returns None if it does not exist. Will check
that the HTTP response is correct and raise an AssertionError if not."""
Expand Down
11 changes: 10 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from daq_config_server.client import ConfigServer

SERVER_ADDRESS = "https://daq-config.diamond.ac.uk/api"
SERVER_ADDRESS = "http://localhost:8555" # "https://daq-config.diamond.ac.uk/api"
USE_PANDA_FLAG = "use_panda_for_gridscan"


Expand Down Expand Up @@ -43,3 +43,12 @@ def test_fetch_and_set_flags(self, server: ConfigServer):
)
assert r.json()["success"] is True
assert server.get_feature_flag(flag) is initial_value

def test_get_some_beamline_params(self, server: ConfigServer):
params_list = [
"miniap_x_ROBOT_LOAD",
"miniap_y_ROBOT_LOAD",
"miniap_z_ROBOT_LOAD",
]
params = server.get_some_beamline_params(params_list)
assert all(p in params.keys() for p in params_list)

0 comments on commit d462094

Please sign in to comment.