NGPIris 5.6.9__tar.gz → 5.7.0.dev2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/__init__.py +1 -1
  2. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/cli/__init__.py +5 -56
  3. ngpiris-5.7.0.dev2/NGPIris/hci/exceptions.py +25 -0
  4. ngpiris-5.7.0.dev2/NGPIris/hci/hci.py +245 -0
  5. ngpiris-5.7.0.dev2/NGPIris/hci/hci_api_requests.py +162 -0
  6. ngpiris-5.7.0.dev2/NGPIris/hci/helpers.py +86 -0
  7. ngpiris-5.7.0.dev2/NGPIris/hci/query_string.py +100 -0
  8. ngpiris-5.7.0.dev2/NGPIris/hci/types.py +300 -0
  9. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/hcp/hcp.py +18 -7
  10. ngpiris-5.7.0.dev2/NGPIris/parse_credentials/__init__.py +3 -0
  11. ngpiris-5.7.0.dev2/NGPIris/utils/__init__.py +3 -0
  12. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/utils/deprecation.py +6 -6
  13. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris.egg-info/PKG-INFO +16 -7
  14. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris.egg-info/SOURCES.txt +7 -0
  15. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris.egg-info/requires.txt +6 -5
  16. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/PKG-INFO +16 -7
  17. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/README.md +9 -1
  18. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/docs/NGPIris.cli.rst +3 -3
  19. ngpiris-5.7.0.dev2/docs/NGPIris.hci.rst +45 -0
  20. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/docs/NGPIris.hcp.rst +11 -11
  21. ngpiris-5.7.0.dev2/docs/NGPIris.parse_credentials.rst +12 -0
  22. ngpiris-5.7.0.dev2/docs/NGPIris.utils.rst +12 -0
  23. ngpiris-5.7.0.dev2/docs/NGPIris_tutorial.md +614 -0
  24. ngpiris-5.7.0.dev2/docs/_static/Iris_HCP_HCI.png +0 -0
  25. ngpiris-5.7.0.dev2/docs/_static/gms.png +0 -0
  26. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/docs/conf.py +18 -4
  27. ngpiris-5.7.0.dev2/docs/glossary.rst +49 -0
  28. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/docs/index.rst +4 -2
  29. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/pyproject.toml +15 -11
  30. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/80MB_test_file +1 -1
  31. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/A/A_80MB_file_0 +1 -1
  32. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/A/A_80MB_file_1 +1 -1
  33. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/A/B/B_80MB_file_0 +1 -1
  34. ngpiris-5.7.0.dev2/tests/test_hci.py +61 -0
  35. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/test_hcp.py +4 -38
  36. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/test_parse_credentials.py +6 -2
  37. ngpiris-5.6.9/NGPIris/hci/hci.py +0 -212
  38. ngpiris-5.6.9/NGPIris/hci/helpers.py +0 -88
  39. ngpiris-5.6.9/NGPIris/parse_credentials/__init__.py +0 -3
  40. ngpiris-5.6.9/NGPIris/utils/__init__.py +0 -3
  41. ngpiris-5.6.9/docs/NGPIris.hci.rst +0 -20
  42. ngpiris-5.6.9/docs/NGPIris.parse_credentials.rst +0 -12
  43. ngpiris-5.6.9/docs/NGPIris.utils.rst +0 -12
  44. ngpiris-5.6.9/docs/_static/gms.png +0 -0
  45. ngpiris-5.6.9/tests/test_hci.py +0 -91
  46. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/LICENSE +0 -0
  47. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/MANIFEST.in +0 -0
  48. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/cli/helpers.py +0 -0
  49. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/cli/sections.py +0 -0
  50. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/hci/__init__.py +0 -0
  51. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/hcp/__init__.py +0 -0
  52. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/hcp/exceptions.py +0 -0
  53. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/hcp/helpers.py +0 -0
  54. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/hcp/statistics.py +0 -0
  55. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/hcp/types.py +0 -0
  56. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/parse_credentials/parse_credentials.py +0 -0
  57. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris/utils/utils.py +0 -0
  58. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris.egg-info/dependency_links.txt +0 -0
  59. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris.egg-info/entry_points.txt +0 -0
  60. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/NGPIris.egg-info/top_level.txt +0 -0
  61. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/docs/Makefile +0 -0
  62. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/docs/NGPIris_base.rst +0 -0
  63. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/docs/make.bat +0 -0
  64. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/setup.cfg +0 -0
  65. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/complete_creds_0.json +0 -0
  66. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/complete_creds_1.json +0 -0
  67. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/complete_creds_2.json +0 -0
  68. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/incomplete_creds_0.json +0 -0
  69. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/incomplete_creds_1.json +0 -0
  70. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/incomplete_creds_2.json +0 -0
  71. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/incomplete_creds_3.json +0 -0
  72. {ngpiris-5.6.9 → ngpiris-5.7.0.dev2}/tests/data/incomplete_creds_4.json +0 -0
@@ -1,4 +1,4 @@
1
- from NGPIris.hci import HCIHandler
1
+ from NGPIris.hci import HCIHandler # noqa: N999
2
2
  from NGPIris.hcp import HCPHandler
3
3
 
4
4
  __all__ = ["HCIHandler", "HCPHandler"]
@@ -1,4 +1,4 @@
1
- # ruff: noqa: SLF001
1
+ # ruff: noqa: SLF001 # noqa: N999
2
2
 
3
3
  import sys
4
4
  from json import dump
@@ -87,7 +87,7 @@ def cli(
87
87
  is_flag=True,
88
88
  )
89
89
  @click.pass_context
90
- def copy( # noqa: PLR0913
90
+ def copy(
91
91
  context: Context,
92
92
  bucket: str,
93
93
  source: str,
@@ -115,57 +115,6 @@ def copy( # noqa: PLR0913
115
115
  )
116
116
 
117
117
 
118
- @cli.command(
119
- section="Object commands",
120
- short_help="Move objects in a bucket/namespace on the HCP.",
121
- )
122
- @click.argument("bucket")
123
- @click.argument("source")
124
- @click.argument("destination")
125
- @click.option(
126
- "-db",
127
- "--destination_bucket",
128
- help="Choose another destination bucket than the source bucket",
129
- default="",
130
- )
131
- @click.option(
132
- "-dr",
133
- "--dry_run",
134
- help=(
135
- "Simulate the command execution without making actual changes. "
136
- "Useful for testing and verification"
137
- ),
138
- is_flag=True,
139
- )
140
- @click.pass_context
141
- def move( # noqa: PLR0913
142
- context: Context,
143
- bucket: str,
144
- source: str,
145
- destination: str,
146
- destination_bucket: str,
147
- dry_run: bool,
148
- ) -> None:
149
- """
150
- Move objects in a bucket/namespace on the HCP.
151
-
152
- BUCKET is the bucket where SOURCE is.
153
-
154
- SOURCE is the object to be moved.
155
-
156
- DESTINATION is the destination path (the path where the object will be
157
- moved to).
158
- """
159
- hcp_h: HCPHandler = create_HCPHandler(context)
160
- hcp_h.mount_bucket(bucket)
161
- if not dry_run:
162
- hcp_h.move_file(source, destination, destination_bucket)
163
- else:
164
- click.echo(
165
- 'This command would have moved the file object "' + source + '"',
166
- )
167
-
168
-
169
118
  @cli.command(
170
119
  section="Object commands",
171
120
  short_help="Delete objects in a bucket/namespace on the HCP.",
@@ -296,7 +245,7 @@ def delete(
296
245
  is_flag=True,
297
246
  )
298
247
  @click.pass_context
299
- def download( # noqa: PLR0913
248
+ def download(
300
249
  context: Context,
301
250
  bucket: str,
302
251
  source: str,
@@ -380,7 +329,7 @@ def download( # noqa: PLR0913
380
329
  is_flag=True,
381
330
  )
382
331
  @click.pass_context
383
- def list_objects( # noqa: PLR0913
332
+ def list_objects(
384
333
  context: Context,
385
334
  bucket: str,
386
335
  path: str | None,
@@ -499,7 +448,7 @@ def list_objects( # noqa: PLR0913
499
448
  default=5,
500
449
  )
501
450
  @click.pass_context
502
- def upload( # noqa: PLR0913
451
+ def upload(
503
452
  context: Context,
504
453
  bucket: str,
505
454
  source: str,
@@ -0,0 +1,25 @@
1
+ # ruff: noqa: N801
2
+
3
+
4
+ class HCI_ResponseError(Exception):
5
+ """
6
+ General Response Error from the HCI.
7
+ """
8
+
9
+
10
+ class DataNotSuppliedToPOSTError(Exception):
11
+ """
12
+ Data was not supplied to a POST request.
13
+ """
14
+
15
+
16
+ class DataSuppliedToGETError(Exception):
17
+ """
18
+ Data was supplied to a GET request.
19
+ """
20
+
21
+
22
+ class IndexNotFoundError(Exception):
23
+ """
24
+ The HCI index could not be found.
25
+ """
@@ -0,0 +1,245 @@
1
+ from json import load
2
+ from pathlib import Path
3
+ from typing import Literal
4
+ from warnings import deprecated
5
+
6
+ from requests import Response, post
7
+ from urllib3 import disable_warnings
8
+
9
+ from NGPIris.hci.exceptions import IndexNotFoundError
10
+ from NGPIris.hci.helpers import (
11
+ get_index_response,
12
+ get_query_response,
13
+ )
14
+ from NGPIris.hci.query_string import QueryString
15
+ from NGPIris.hci.types import (
16
+ FacetRequest,
17
+ IndexResponse,
18
+ QueryRequest,
19
+ QueryResponse,
20
+ SortField,
21
+ to_QueryRequest,
22
+ )
23
+ from NGPIris.parse_credentials import CredentialsHandler
24
+ from NGPIris.utils.deprecation import _add_deprecation_in_doc_string
25
+
26
+ _REQUEST_TOKEN_DEP_MSG = (
27
+ ":py:func:`request_token` is deprecated in favor of integrating its " # noqa: S105
28
+ "functionality into the :py:class:`HCIHandler` constructor"
29
+ )
30
+
31
+ _QUERY_FROM_JSON_DEP_MSG = (
32
+ ":py:meth:`query_from_JSON` is deprecated due to "
33
+ "implementation issues in a type safe way with "
34
+ ":py:class:`~NGPIris.hci.query_string.QueryString`"
35
+ )
36
+
37
+
38
+ class HCIHandler:
39
+ """
40
+ Class for handling HCI requests.
41
+ """
42
+
43
+ def __init__(
44
+ self, credentials: str | dict[str, str], use_ssl: bool = False
45
+ ) -> None:
46
+ """
47
+ Class for handling HCI requests.
48
+
49
+ :param credentials:
50
+ If ``credentials`` is a :py:type:`str`, then it will be interpreted
51
+ as a path to the JSON credentials file. If ``credentials`` is a
52
+ :py:type:`dict`, then a dictionary with the appropriate HCI
53
+ credentials is expected::
54
+
55
+ {
56
+ "username" : "",
57
+ "password" : "",
58
+ "address" : "",
59
+ "auth_port" : "",
60
+ "api_port" : ""
61
+ }
62
+
63
+ :param use_ssl: Boolean choice between using SSL
64
+ """
65
+ if type(credentials) is str:
66
+ credentials_handler = CredentialsHandler(credentials)
67
+ self.hci = credentials_handler.hci
68
+
69
+ self.address = self.hci["address"]
70
+ self.username = self.hci["username"]
71
+ self.password = self.hci["password"]
72
+ self.realm = self.hci.get("realm", "LOCAL")
73
+ elif type(credentials) is dict:
74
+ self.address = credentials["address"]
75
+ self.username = credentials["username"]
76
+ self.password = credentials["password"]
77
+ self.realm = self.hci.get("realm", "LOCAL")
78
+
79
+ self.use_ssl = use_ssl
80
+
81
+ if not self.use_ssl:
82
+ disable_warnings()
83
+
84
+ self.token = self._request_token()
85
+
86
+ @_add_deprecation_in_doc_string(_REQUEST_TOKEN_DEP_MSG, "v5.6.7")
87
+ @deprecated(_REQUEST_TOKEN_DEP_MSG)
88
+ def request_token(self) -> None:
89
+ """
90
+ Request a token from the HCI, which is stored in the HCIHandler object.
91
+ The token is used for every operation that needs to send a request to
92
+ HCI.
93
+
94
+ :raises ConnectionError:
95
+ If there was a problem when requesting a token
96
+ """
97
+ self.token = self._request_token()
98
+
99
+ def _request_token(self) -> str:
100
+ """
101
+ Request a token from the HCI. The token is used for every operation
102
+ that needs to send a request to HCI.
103
+
104
+ :raises ConnectionError:
105
+ If there was a problem when requesting a token
106
+ """
107
+ match self.realm:
108
+ case "HCI":
109
+ url = (
110
+ "https://keycloak."
111
+ + self.address
112
+ + "/realms/"
113
+ + self.realm
114
+ + "/protocol/openid-connect/token"
115
+ )
116
+ data = {
117
+ "grant_type": "client_credentials",
118
+ "client_id": self.username,
119
+ "client_secret": self.password,
120
+ "realm": self.realm,
121
+ }
122
+ case _: # Same as "LOCAL"
123
+ url = "https://" + self.address + ":8000" + "/auth/oauth/"
124
+ data = {
125
+ "grant_type": "password",
126
+ "username": self.username,
127
+ "password": self.password,
128
+ "scope": "*",
129
+ "client_secret": "hci-client",
130
+ "client_id": "hci-client",
131
+ "realm": "LOCAL",
132
+ }
133
+ try:
134
+ response: Response = post(
135
+ url, data=data, verify=self.use_ssl, timeout=15
136
+ )
137
+ except: # noqa: E722 # pragma: no cover
138
+ error_msg: str = (
139
+ "The token request made at "
140
+ + url
141
+ + " failed. Please check your connection."
142
+ )
143
+ raise ConnectionError(error_msg) from None
144
+
145
+ token: str = response.json()["access_token"]
146
+ return token
147
+
148
+ def list_index_names(self) -> list[str]:
149
+ """
150
+ Retrieve a list of all index names.
151
+
152
+ :return: A list of index names
153
+ """
154
+ return [entry["name"] for entry in get_index_response(self)]
155
+
156
+ def look_up_index(self, index_name: str) -> IndexResponse:
157
+ """
158
+ Look up index information in the form of a dictionary by submitting
159
+ the index name. Will return an empty dictionary if no index was found.
160
+
161
+ :param index_name: The index name
162
+
163
+ :return: A dictionary containing information about an index
164
+ """
165
+ for entry in get_index_response(self):
166
+ if entry["name"] == index_name:
167
+ return entry
168
+
169
+ msg = "The index " + index_name + " could not be found"
170
+ raise IndexNotFoundError(msg)
171
+
172
+ def query(self, query_dict: QueryRequest) -> QueryResponse:
173
+ """
174
+ Make query to an HCI index, with a :py:class:`QueryRequest` typed
175
+ dictionary.
176
+
177
+ :param query_dict:
178
+ Dictionary consisting of the query. For more information see the
179
+ specification of :py:class:`QueryRequest`
180
+
181
+ :return:
182
+ :py:class:`QueryResponse` typed dictionary containing the query
183
+ response
184
+ """
185
+ return get_query_response(query_dict, self)
186
+
187
+ @_add_deprecation_in_doc_string(_QUERY_FROM_JSON_DEP_MSG, "v5.7.0")
188
+ @deprecated(_QUERY_FROM_JSON_DEP_MSG)
189
+ def query_from_JSON(self, query_path: str) -> QueryResponse:
190
+ """
191
+ Make query to an HCI index, with pre-written query in a JSON file.
192
+
193
+ :param query_path: Path to the JSON file
194
+
195
+ :return: Dictionary containing the raw query
196
+ """
197
+ msg = "This method has not been properly implemented yet"
198
+ raise NotImplementedError(msg)
199
+ with Path(query_path).open() as inp:
200
+ return self.query(to_QueryRequest(load(inp)))
201
+
202
+ def basic_query(
203
+ self,
204
+ index_name: str,
205
+ query_string: QueryString | None = None,
206
+ items_to_return: int = 0,
207
+ facets: list[str] | None = None,
208
+ sort_fields: list[tuple[str, Literal["DESC", "ASC"]]] | None = None,
209
+ ) -> QueryResponse:
210
+ """
211
+ Make a query to the HCI based on the parameters of this method.
212
+
213
+ :param index_name: Name of the index
214
+
215
+ :param query_string: The Solr query string.
216
+
217
+ :param items_to_return: Number of results that should be returned
218
+
219
+ :param facets:
220
+ List of facets that should be returned included in the response.
221
+
222
+ :param sort_fields:
223
+ List of tuples of fields and sort orders
224
+
225
+ :return: The response in the form of a dictionary
226
+ """
227
+ if sort_fields is None:
228
+ sort_fields = []
229
+ if facets is None:
230
+ facets = []
231
+ if query_string is None:
232
+ query_string = QueryString()
233
+
234
+ facetRequests = [FacetRequest({"fieldName": facet}) for facet in facets]
235
+ sortFields = [
236
+ SortField(sortField=sf, sortOrder=so) for sf, so in sort_fields
237
+ ]
238
+ query_dict: QueryRequest = {
239
+ "indexName": index_name,
240
+ "queryString": query_string,
241
+ "itemsToReturn": items_to_return,
242
+ "facetRequests": facetRequests,
243
+ "sortFields": sortFields,
244
+ }
245
+ return self.query(query_dict)
@@ -0,0 +1,162 @@
1
+ from json import dumps, loads
2
+ from typing import TYPE_CHECKING, Literal
3
+
4
+ from requests import HTTPError, Response, get, post
5
+
6
+ from NGPIris.hci.exceptions import (
7
+ DataNotSuppliedToPOSTError,
8
+ DataSuppliedToGETError,
9
+ HCI_ResponseError,
10
+ )
11
+ from NGPIris.hci.types import to_QueryRequestTQS
12
+
13
+ if TYPE_CHECKING:
14
+ from NGPIris.hci import HCIHandler, types
15
+ from NGPIris.hci.types import QueryRequest
16
+
17
+ # ruff: noqa: PYI051
18
+
19
+
20
+ def get_response(
21
+ hci_handler: HCIHandler,
22
+ request_type: Literal["GET", "POST"],
23
+ api_endpoint: Literal[
24
+ "/api/search/indexes/",
25
+ "/api/search/query/",
26
+ ]
27
+ | str,
28
+ api_endpoint_extension: str = "",
29
+ extra_headers: dict[str, str] | None = None,
30
+ data: dict | types.Request | None = None,
31
+ ) -> Response:
32
+ """
33
+ A general implementation of making requests to the HCI.
34
+
35
+ :param hci_handler:
36
+ The :py:class:`HCIHandler` object to be used to make the request
37
+
38
+ :param request_type: The specified kind of request
39
+
40
+ :param api_endpoint: The specified API endpoint to make the request at
41
+
42
+ :param api_endpoint_extension: Optional extension of the API endpoint
43
+
44
+ :param extra_headers: Optional additional header values to the request
45
+
46
+ :param data: Optional data to be made
47
+
48
+ :raise DataNotSuppliedToPOST:
49
+ If :py:obj:`request_type` is set to :py:obj:`"POST"` but no data is
50
+ supplied
51
+
52
+ :raise DataSuppliedToGET:
53
+ If :py:obj:`request_type` is set to :py:obj:`"GET"` and data is supplied
54
+
55
+ :return: A response from the HCI
56
+ """
57
+ if data is None:
58
+ data = {}
59
+ if extra_headers is None:
60
+ extra_headers = {}
61
+
62
+ if (request_type == "POST") and not data:
63
+ msg = "Request type is set to POST, but no data was supplied"
64
+ raise DataNotSuppliedToPOSTError(msg)
65
+
66
+ if request_type == "GET" and data:
67
+ msg = "Request type is set to POST, but data was also supplied"
68
+ raise DataSuppliedToGETError(msg)
69
+
70
+ match hci_handler.realm:
71
+ case "HCI":
72
+ url = (
73
+ "https://hci."
74
+ + hci_handler.address
75
+ + api_endpoint
76
+ + api_endpoint_extension
77
+ )
78
+ case _: # Same as "LOCAL"
79
+ url = (
80
+ "https://"
81
+ + hci_handler.address
82
+ + ":8888"
83
+ + api_endpoint
84
+ + api_endpoint_extension
85
+ )
86
+
87
+ headers: dict[str, str] = extra_headers | {
88
+ "Accept": "application/json",
89
+ "Authorization": "Bearer " + hci_handler.token,
90
+ }
91
+
92
+ match request_type:
93
+ case "GET":
94
+ response = get(
95
+ url, headers=headers, verify=hci_handler.use_ssl, timeout=15
96
+ )
97
+ case "POST":
98
+ response = post(
99
+ url,
100
+ data=dumps(data),
101
+ headers=headers,
102
+ verify=hci_handler.use_ssl,
103
+ timeout=15,
104
+ )
105
+
106
+ try:
107
+ response.raise_for_status()
108
+ except HTTPError as http_error:
109
+ error_response = http_error.response
110
+ if error_response:
111
+ error_content = dict(loads(error_response.content))
112
+ error_msg = (
113
+ "\nstatusCode: "
114
+ + str(error_content.get("statusCode", ""))
115
+ + "\nerrorMessage: "
116
+ + str(error_content.get("errorMessage", ""))
117
+ + "\nerrorProperties: "
118
+ + str(error_content.get("errorProperties", ""))
119
+ )
120
+ else:
121
+ error_msg = "An unknown error occurred"
122
+ raise HCI_ResponseError(error_msg) from http_error
123
+
124
+ return response
125
+
126
+
127
+ def make_index_request(hci_handler: HCIHandler) -> Response:
128
+ """
129
+ Retrieve the index response by making a request with the address, API port
130
+ and token.
131
+
132
+ :param hci_handler:
133
+ The :py:class:`HCIHandler` object to be used to make the request
134
+
135
+ :return: A response containing information about the index
136
+ """
137
+ return get_response(hci_handler, "GET", "/api/search/indexes/")
138
+
139
+
140
+ def make_query_request(
141
+ query_dict: QueryRequest, hci_handler: HCIHandler
142
+ ) -> Response:
143
+ """
144
+ Retrieve the query response by making a request with the address, API port
145
+ and token.
146
+
147
+ :param query_dict: The query dictionary
148
+
149
+ :param hci_handler:
150
+ The :py:class:`HCIHandler` object to be used to make the request
151
+
152
+ :return: A response containing information about the query
153
+ """
154
+ return get_response(
155
+ hci_handler,
156
+ "POST",
157
+ "/api/search/query/",
158
+ extra_headers={
159
+ "Content-Type": "application/json",
160
+ },
161
+ data=to_QueryRequestTQS(query_dict),
162
+ )
@@ -0,0 +1,86 @@
1
+ from typing import TYPE_CHECKING, Any, overload
2
+
3
+ from NGPIris.hci.hci_api_requests import make_index_request, make_query_request
4
+ from NGPIris.hci.types import (
5
+ IndexResponse,
6
+ QueryRequest,
7
+ QueryResponse,
8
+ )
9
+
10
+ if TYPE_CHECKING:
11
+ from requests import Response
12
+
13
+ from NGPIris.hci import HCIHandler
14
+
15
+
16
+ @overload
17
+ def from_response(
18
+ response: Response, to: type[IndexResponse]
19
+ ) -> list[IndexResponse]:
20
+ pass
21
+
22
+
23
+ @overload
24
+ def from_response(response: Response, to: type[QueryResponse]) -> QueryResponse:
25
+ pass
26
+
27
+
28
+ def from_response(
29
+ response: Response, to: type[IndexResponse | QueryResponse]
30
+ ) -> list[IndexResponse] | QueryResponse:
31
+ """
32
+ Helper function for turning a :py:class:`requests.Response` object into a
33
+ IRIS :py:class:`~NGPIris.hci.types.Response`. It achieves this by a somewhat
34
+ *unorthodox* approach.
35
+
36
+ NOTE: The return type for when :py:obj:`to` is
37
+ :py:class:`~NGPIris.hci.types.IndexResponse` is a
38
+ :py:type:`list[IndexResponse]`. This is due to the fact that the response
39
+ produced by :py:func:`get_index_response` is a list of individual index
40
+ responses. This is how the HCI works by design.
41
+
42
+ :param response: The incoming :py:class:`requests.Response`
43
+
44
+ :param to:
45
+ The expected sub type of :py:class:`~NGPIris.hci.types.Response` that
46
+ should be returned
47
+
48
+ :return: The response as the chosen kind based on :py:obj:`to`
49
+ """
50
+ if to is IndexResponse:
51
+ list_of_index_response_dicts: list[dict[str, Any]] = response.json()
52
+ index_responses: list[IndexResponse] = [
53
+ IndexResponse(dict(index_response_dict)) # pyright: ignore[reportArgumentType]
54
+ for index_response_dict in list_of_index_response_dicts
55
+ ]
56
+ return index_responses
57
+ elif to is QueryResponse: # noqa: RET505
58
+ as_dict = dict(response.json())
59
+ return QueryResponse(as_dict) # pyright: ignore[reportArgumentType]
60
+
61
+ msg = "to_type = " + str(to)
62
+ raise RuntimeError(msg)
63
+
64
+
65
+ def get_index_response(hci_handler: HCIHandler) -> list[IndexResponse]:
66
+ """
67
+ A short hand function equivalent to
68
+ ::
69
+
70
+ from_response(make_index_request(hci_handler), IndexResponse)
71
+ """ # noqa: D400, D415
72
+ return from_response(make_index_request(hci_handler), IndexResponse)
73
+
74
+
75
+ def get_query_response(
76
+ query_dict: QueryRequest, hci_handler: HCIHandler
77
+ ) -> QueryResponse:
78
+ """
79
+ A short hand function equivalent to
80
+ ::
81
+
82
+ from_response(make_query_request(query_dict, hci_handler), QueryResponse)
83
+ """ # noqa: D400, D415, E501
84
+ return from_response(
85
+ make_query_request(query_dict, hci_handler), QueryResponse
86
+ )