Skip to content

Commit 7a484dc

Browse files
committed
ENH: add 'search' API
1 parent 7a9baa2 commit 7a484dc

File tree

4 files changed

+114
-5
lines changed

4 files changed

+114
-5
lines changed

src/save_and_restore_api/_api_async.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,30 @@ async def send_request(
4545
# =============================================================================================
4646

4747
async def info_get(self):
48+
# Reusing docstrings from the threaded version
4849
method, url = self._prepare_info_get()
4950
return await self.send_request(method, url)
5051

5152
async def version_get(self):
52-
"""
53-
Returns current version of Save and Restore service.
54-
55-
API: GET /verson
56-
"""
53+
# Reusing docstrings from the threaded version
5754
method, url = self._prepare_version_get()
5855
return await self.send_request(method, url)
5956

57+
# =============================================================================================
58+
# SEARCH-CONTROLLER API METHODS
59+
# =============================================================================================
60+
61+
async def search(self, allRequestParams):
62+
# Reusing docstrings from the threaded version
63+
method, url, url_params = self._prepare_search(allRequestParams=allRequestParams)
64+
return await self.send_request(method, url, url_params=url_params)
65+
6066
# =============================================================================================
6167
# AUTHENTICATION-CONTROLLER API METHODS
6268
# =============================================================================================
6369

6470
async def login(self, *, username=None, password=None):
71+
# Reusing docstrings from the threaded version
6572
method, url, params = self._prepare_login(username=username, password=password)
6673
return await self.send_request(method, url, params=params)
6774

@@ -206,7 +213,9 @@ async def restore_items(self, *, snapshotItems, auth=None):
206213

207214

208215
SaveRestoreAPI.info_get.__doc__ = _SaveRestoreAPI_Threads.info_get.__doc__
216+
SaveRestoreAPI.version_get.__doc__ = _SaveRestoreAPI_Threads.version_get.__doc__
209217
SaveRestoreAPI.login.__doc__ = _SaveRestoreAPI_Threads.login.__doc__
218+
SaveRestoreAPI.search.__doc__ = _SaveRestoreAPI_Threads.search.__doc__
210219
SaveRestoreAPI.node_get.__doc__ = _SaveRestoreAPI_Threads.node_get.__doc__
211220
SaveRestoreAPI.nodes_get.__doc__ = _SaveRestoreAPI_Threads.nodes_get.__doc__
212221
SaveRestoreAPI.node_add.__doc__ = _SaveRestoreAPI_Threads.node_add.__doc__

src/save_and_restore_api/_api_base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ def _prepare_version_get(self):
176176
method, url = "GET", "/version"
177177
return method, url
178178

179+
# =============================================================================================
180+
# SEARCH-CONTROLLER API METHODS
181+
# =============================================================================================
182+
183+
def _prepare_search(self, *, allRequestParams):
184+
method, url = "GET", "/search"
185+
url_params = allRequestParams
186+
return method, url, url_params
187+
179188
# =============================================================================================
180189
# AUTHENTICATION-CONTROLLER API METHODS
181190
# =============================================================================================

src/save_and_restore_api/_api_threads.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ def version_get(self):
6161
method, url = self._prepare_version_get()
6262
return self.send_request(method, url)
6363

64+
# =============================================================================================
65+
# SEARCH-CONTROLLER API METHODS
66+
# =============================================================================================
67+
68+
def search(self, allRequestParams):
69+
"""
70+
Send search query to the database. Example search queries (``allRequestParams``):
71+
search for nodes with name containing 'test config': ``{"name": "test config"}``,
72+
search for nodes with the description containing 'backup pvs': ``{"description": "backup pvs"}``,
73+
74+
Returns a dictionary with the following keys: ``hitCount`` - the number of matching nodes,
75+
``nodes`` - a list of matching nodes (not including data).
76+
77+
API: GET /search
78+
"""
79+
method, url, url_params = self._prepare_search(allRequestParams=allRequestParams)
80+
return self.send_request(method, url, url_params=url_params)
81+
6482
# =============================================================================================
6583
# AUTHENTICATION-CONTROLLER API METHODS
6684
# =============================================================================================

tests/test_misc.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
from .common import (
1313
_is_async,
14+
_select_auth,
1415
admin_password,
1516
admin_username,
1617
base_url,
1718
clear_sar, # noqa: F401
19+
create_root_folder,
1820
read_password,
1921
read_username,
2022
user_password,
@@ -119,6 +121,77 @@ async def testing():
119121
asyncio.run(testing())
120122

121123

124+
# =============================================================================================
125+
# TESTS FOR SEARCH-CONTROLLER API METHODS
126+
# =============================================================================================
127+
128+
# fmt: off
129+
@pytest.mark.parametrize("library", ["THREADS", "ASYNC"])
130+
# fmt: on
131+
def test_search_01(clear_sar, library): # noqa: F811
132+
"""
133+
Tests for the 'search' API.
134+
"""
135+
root_folder_uid = create_root_folder()
136+
137+
if not _is_async(library):
138+
with SaveRestoreAPI_Threads(base_url=base_url, timeout=2) as SR:
139+
_select_auth(SR=SR, usesetauth=True)
140+
141+
configurationNode = {"name": "Test Config", "description": "Created for testing"}
142+
configurationData = {"pvList": []}
143+
144+
response = SR.config_create(
145+
root_folder_uid,
146+
configurationNode=configurationNode,
147+
configurationData=configurationData,
148+
)
149+
config_uid = response["configurationNode"]["uniqueId"]
150+
151+
response = SR.search({"name": "Test Config"})
152+
assert response["hitCount"] == 1
153+
assert response["nodes"][0]["name"] == "Test Config"
154+
assert response["nodes"][0]["uniqueId"] == config_uid
155+
156+
response = SR.search({"description": "for testing"})
157+
assert response["hitCount"] == 1
158+
assert response["nodes"][0]["name"] == "Test Config"
159+
assert response["nodes"][0]["uniqueId"] == config_uid
160+
161+
response = SR.search({"name": "No such config"})
162+
assert response["hitCount"] == 0
163+
164+
else:
165+
async def testing():
166+
async with SaveRestoreAPI_Async(base_url=base_url, timeout=2) as SR:
167+
_select_auth(SR=SR, usesetauth=True)
168+
169+
configurationNode = {"name": "Test Config", "description": "Created for testing"}
170+
configurationData = {"pvList": []}
171+
172+
response = await SR.config_create(
173+
root_folder_uid,
174+
configurationNode=configurationNode,
175+
configurationData=configurationData,
176+
)
177+
config_uid = response["configurationNode"]["uniqueId"]
178+
179+
response = await SR.search({"name": "Test Config"})
180+
assert response["hitCount"] == 1
181+
assert response["nodes"][0]["name"] == "Test Config"
182+
assert response["nodes"][0]["uniqueId"] == config_uid
183+
184+
response = await SR.search({"description": "for testing"})
185+
assert response["hitCount"] == 1
186+
assert response["nodes"][0]["name"] == "Test Config"
187+
assert response["nodes"][0]["uniqueId"] == config_uid
188+
189+
response = await SR.search({"name": "No such config"})
190+
assert response["hitCount"] == 0
191+
192+
asyncio.run(testing())
193+
194+
122195
# =============================================================================================
123196
# TESTS FOR AUTHENTICATION-CONTROLLER API METHODS
124197
# =============================================================================================

0 commit comments

Comments
 (0)