@@ -102,16 +102,17 @@ def _safe_add_to_kwargs(self, parent_key: str, child_key: str, value: str,
102
102
def _request_or_raise (self ,
103
103
method : str ,
104
104
uri : str ,
105
- object_name : str ,
105
+ object_name : str = "" ,
106
106
expected_status_code : int = 200 ,
107
107
** kwargs : dict [str , str ]) -> requests .Response :
108
108
"""Private method.
109
109
110
110
Supports passing `**kwargs` that are then passed on to `request`.
111
111
112
112
Args:
113
+ method (str): "GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", or "DELETE"
113
114
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.
115
116
expected_status_code (int, optional): Expected status code of the response. Defaults to 200.
116
117
117
118
Raises:
@@ -122,7 +123,9 @@ def _request_or_raise(self,
122
123
"""
123
124
res = self .request (method , uri , ** kwargs )
124
125
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 ))
126
129
return res
127
130
128
131
def _get_or_raise (self ,
@@ -133,8 +136,8 @@ def _get_or_raise(self,
133
136
"""Private method. Convenience wrapper for `_request_or_raise`."""
134
137
return self ._request_or_raise ("GET" , uri , object_name , expected_status_code , ** kwargs )
135
138
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 } .\n Raw 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 } .\n Raw response: { res } "
138
141
139
142
def _full_uri (self , endpoint : str ) -> str :
140
143
"""Private method.
@@ -152,7 +155,7 @@ def _full_uri(self, endpoint: str) -> str:
152
155
@staticmethod
153
156
def __request_or_raise (method : str ,
154
157
uri : str ,
155
- object_name : str ,
158
+ object_name : str = "" ,
156
159
json_decode : bool = True ,
157
160
single_element : bool = False ,
158
161
expected_status_code : int = 200 ,
@@ -161,17 +164,25 @@ def __request_or_raise(method: str,
161
164
162
165
Uplink-style decorator for requests. Use like `_request_or_raise` but as decorator.
163
166
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 .
165
168
166
169
Example usage:
167
170
```
168
171
@__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"})
172
175
def get_application_decorated(application_name):
173
176
\" ""Get application by name.\" ""
174
177
```
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.
175
186
"""
176
187
177
188
from inspect import signature
@@ -186,8 +197,10 @@ def __inner_request_or_raise_decorator(*args: list[Any]) -> str | list[dict[str,
186
197
self : AppDController = args [0 ] # type: ignore
187
198
188
199
bound_args = signature (func ).bind (* args ).arguments
200
+ if "self" in bound_args :
201
+ del bound_args ["self" ]
189
202
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 ""
191
204
192
205
k = kwargs
193
206
json = json_decode or single_element
0 commit comments