psytricks 2.2.0a22__tar.gz → 2.3.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: psytricks
3
- Version: 2.2.0a22
3
+ Version: 2.3.0
4
4
  Summary: PowerShell Python Citrix Tricks.
5
5
  License-Expression: GPL-3.0-or-later
6
6
  License-File: LICENSE
@@ -6,7 +6,7 @@ authors = [
6
6
  ]
7
7
  license = "GPL-3.0-or-later"
8
8
  dynamic = ["readme"]
9
- version = "2.2.0-a.22"
9
+ version = "2.3.0"
10
10
 
11
11
  [tool.poetry]
12
12
  documentation = "https://imcf.one/apidocs/psytricks/psytricks.html"
@@ -3,7 +3,7 @@
3
3
  .. include:: ../../CHANGELOG.md
4
4
  """
5
5
 
6
- __version__ = "2.2.0-a.22"
6
+ __version__ = "2.3.0"
7
7
 
8
8
  # style exceptions for pdoc-include-only docstrings:
9
9
  # ruff: noqa: D400 (missing-trailing-period)
@@ -5,7 +5,7 @@ Collection of functions and other definitions to be sourced by other scripts.
5
5
  #>
6
6
 
7
7
  # the version variable will be filled by poetry at build time:
8
- $Version = "2.2.0-a.22"
8
+ $Version = "2.3.0"
9
9
 
10
10
 
11
11
  #region properties-selectors
@@ -63,6 +63,7 @@ class ResTricksWrapper:
63
63
  self._connected = False
64
64
  self._verify = verify
65
65
  self._read_only = False
66
+ self._dump_responses_to = None
66
67
 
67
68
  if not lazy:
68
69
  self.connect()
@@ -91,6 +92,44 @@ class ResTricksWrapper:
91
92
  log.debug(f"{verb} 'read-only' mode.")
92
93
  self._read_only = value
93
94
 
95
+ @property
96
+ def dump_responses_to(self) -> Path | None:
97
+ """Path to dump responses to - **DO NOT USE IN PRODUCTION**.
98
+
99
+ Default: `None`, meaning responses will **not** be written to disk.
100
+
101
+ If `dump_responses_to` is set to a valid path, any response returned
102
+ from a request will be dumped to a file in that path. Each dump is
103
+ placed in a single file with the following naming pattern with the
104
+ components described below:
105
+
106
+ `<command>-<method>-<status>-<timestamp>.txt`
107
+
108
+ * `<command>`: e.g. `GetSessions`, `SetMaintenanceMode`
109
+ * `<method>`: one of [`GET`, `POST`]
110
+ * `<status>`: the HTTP status of the response
111
+ * `<timestamp>`: milliseconds since the epoch
112
+
113
+ This is intended for debugging and may also be used to craft data mocks
114
+ based on real data that can be used for testing.
115
+
116
+ Notes
117
+ -----
118
+ Existing files will **NOT** be overwritten, no matter how unlikely this
119
+ is given the above naming scheme.
120
+ """
121
+ return self._dump_responses_to
122
+
123
+ @dump_responses_to.setter
124
+ def dump_responses_to(self, value: Path | None) -> None:
125
+ if value and not (value.is_dir() and value.exists()):
126
+ log.error(f"Path needs to be an existing directory: {value}")
127
+ return
128
+
129
+ target = str(value) if value else "<INACTIVE>"
130
+ log.debug(f"Setting 'dump-responses' path: {target}")
131
+ self._dump_responses_to = value
132
+
94
133
  def connect(self) -> None:
95
134
  """Connect to the ResTricks service unless already connected.
96
135
 
@@ -186,9 +225,10 @@ class ResTricksWrapper:
186
225
  log.error("Version mismatch! 🧨")
187
226
  return False
188
227
 
189
- @staticmethod
190
- def _check_response(response) -> None:
228
+ def _check_response(self, response: requests.Response) -> None:
191
229
  """Check the HTTP response code and JSON status attributes."""
230
+ self._write_response_dump(response)
231
+
192
232
  if response.status_code == 200:
193
233
  return
194
234
 
@@ -209,6 +249,29 @@ class ResTricksWrapper:
209
249
  log.warning(response.text)
210
250
  raise ValueError(f"Malformed response: {response.text}") from ex
211
251
 
252
+ def _write_response_dump(self, response: requests.Response) -> Path | None:
253
+ """Dump the response to disk if `dump_responses_to` is set."""
254
+ if not self.dump_responses_to:
255
+ return
256
+
257
+ try:
258
+ timestamp = int(time.time_ns() / 1000000)
259
+ method = response.request.method
260
+ url = str(response.request.url)
261
+ log.trace(f"🌐 Request URL: {url}")
262
+ command = url.removeprefix(self.base_url).replace("/", "-")
263
+ status = str(response.status_code)
264
+ filename = f"{command}-{method}-{status}-{timestamp}.txt"
265
+ full_path = self.dump_responses_to / filename
266
+ log.warning(f"🚚 Dumping response to 🗃️: {full_path}")
267
+ if full_path.exists():
268
+ raise FileExistsError(f"File already exists: {full_path}")
269
+
270
+ with open(full_path, "w", encoding="utf8") as outfile:
271
+ outfile.write(response.text)
272
+ except Exception as ex:
273
+ log.error(f"🔥 Error dumping response: {ex}")
274
+
212
275
  def send_get_request(
213
276
  self, raw_url: str, auto_conn: bool = True
214
277
  ) -> list[dict] | dict | None:
@@ -258,7 +321,7 @@ class ResTricksWrapper:
258
321
  log.error(f"{msg}\n== STATUS CODE:{response.status_code}")
259
322
  raise json.JSONDecodeError(msg, doc=response.text, pos=0)
260
323
 
261
- ResTricksWrapper._check_response(response)
324
+ self._check_response(response)
262
325
 
263
326
  return data
264
327
 
@@ -319,7 +382,7 @@ class ResTricksWrapper:
319
382
  log.error(f"POST request [{raw_url}] failed: {ex}")
320
383
  raise ex
321
384
 
322
- ResTricksWrapper._check_response(response)
385
+ self._check_response(response)
323
386
 
324
387
  if no_json:
325
388
  log.debug(f"No-payload response status code: {response.status_code}")
File without changes
File without changes
File without changes