Skip to content

Commit 2eaeccb

Browse files
committed
refactor: make object_name parameter optional, defaulting to uri
docs: improved docstrings for __request_or_raise
1 parent 7f72e44 commit 2eaeccb

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/AppDPyAPI/controller.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,17 @@ def _safe_add_to_kwargs(self, parent_key: str, child_key: str, value: str,
102102
def _request_or_raise(self,
103103
method: str,
104104
uri: str,
105-
object_name: str,
105+
object_name: str = "",
106106
expected_status_code: int = 200,
107107
**kwargs: dict[str, str]) -> requests.Response:
108108
"""Private method.
109109
110110
Supports passing `**kwargs` that are then passed on to `request`.
111111
112112
Args:
113+
method (str): "GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", or "DELETE"
113114
uri (str): The URI to request.
114-
object_name (str): String describing what is being fetched, e.g. "transaction detection rules".
115+
object_name (str): String describing what is being fetched, e.g. "transaction detection rules". Defaults to `uri` if empty.
115116
expected_status_code (int, optional): Expected status code of the response. Defaults to 200.
116117
117118
Raises:
@@ -122,7 +123,9 @@ def _request_or_raise(self,
122123
"""
123124
res = self.request(method, uri, **kwargs)
124125
if res.status_code != expected_status_code:
125-
raise AppDException(self._could_not_get_exception_msg(object_name, res.status_code, res.text))
126+
raise AppDException(
127+
self._could_not_get_exception_msg(method, object_name if object_name else uri,
128+
res.status_code, res.text))
126129
return res
127130

128131
def _get_or_raise(self,
@@ -133,8 +136,8 @@ def _get_or_raise(self,
133136
"""Private method. Convenience wrapper for `_request_or_raise`."""
134137
return self._request_or_raise("GET", uri, object_name, expected_status_code, **kwargs)
135138

136-
def _could_not_get_exception_msg(self, object_name: str, status_code: int, res: str) -> str:
137-
return f"Could not get {object_name}, received status code {status_code}.\nRaw response: {res}"
139+
def _could_not_get_exception_msg(self, method: str, object_name: str, status_code: int, res: str) -> str:
140+
return f"Could not {method} {object_name}, received status code {status_code}.\nRaw response: {res}"
138141

139142
def _full_uri(self, endpoint: str) -> str:
140143
"""Private method.
@@ -152,7 +155,7 @@ def _full_uri(self, endpoint: str) -> str:
152155
@staticmethod
153156
def __request_or_raise(method: str,
154157
uri: str,
155-
object_name: str,
158+
object_name: str = "",
156159
json_decode: bool = True,
157160
single_element: bool = False,
158161
expected_status_code: int = 200,
@@ -161,17 +164,25 @@ def __request_or_raise(method: str,
161164
162165
Uplink-style decorator for requests. Use like `_request_or_raise` but as decorator.
163166
164-
URI and object name are expanded at runtime as `URITemplate`.
167+
URI and object name are expanded at runtime as `URITemplate`, (!) "self" cannot be used as a URI parameter.
165168
166169
Example usage:
167170
```
168171
@__request_or_raise("GET",
169-
"/controller/rest/applications/{application_name}",
170-
"application {application_name}",
171-
headers={"myHeader": "value"})
172+
"/controller/rest/applications/{application_name}",
173+
"application {application_name}",
174+
headers={"myHeader": "value"})
172175
def get_application_decorated(application_name):
173176
\"""Get application by name.\"""
174177
```
178+
179+
Args:
180+
method (str): "GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", or "DELETE"
181+
uri (str): The URI to request.
182+
object_name (str, optional): String describing what is being fetched. Defaults to `uri` if empty.
183+
json_decode (bool, optional): Call `res.json()` on response? Adds params={"output": "json"} to request if set to True. Defaults to True.
184+
single_element (bool, optional): If True, json_decode will also be set to True, and `res.json()[0]` will be returned. Defaults to False.
185+
expected_status_code (int, optional): Expected status code of the response. Defaults to 200.
175186
"""
176187

177188
from inspect import signature
@@ -186,8 +197,10 @@ def __inner_request_or_raise_decorator(*args: list[Any]) -> str | list[dict[str,
186197
self: AppDController = args[0] # type: ignore
187198

188199
bound_args = signature(func).bind(*args).arguments
200+
if "self" in bound_args:
201+
del bound_args["self"]
189202
expanded_uri = URITemplate(self._full_uri(uri)).expand(bound_args)
190-
expanded_object_name = URITemplate(object_name).expand(bound_args)
203+
expanded_object_name = URITemplate(object_name).expand(bound_args) if object_name else ""
191204

192205
k = kwargs
193206
json = json_decode or single_element

0 commit comments

Comments
 (0)