| 239 | |
| 240 | |
| 241 | class StrCLIResponse(str, BaseCLIResponse): |
| 242 | |
| 243 | @property |
| 244 | def json(self) -> Optional[dict]: |
| 245 | """ |
| 246 | Return deserialized the request or response JSON body, |
| 247 | if one (and only one) included in the output and is parsable. |
| 248 | |
| 249 | """ |
| 250 | if not hasattr(self, '_json'): |
| 251 | self._json = None |
| 252 | # De-serialize JSON body if possible. |
| 253 | if COLOR in self: |
| 254 | # Colorized output cannot be parsed. |
| 255 | pass |
| 256 | elif self.strip().startswith('{'): |
| 257 | # Looks like JSON body. |
| 258 | self._json = json.loads(self) |
| 259 | elif self.count('Content-Type:') == 1: |
| 260 | # Looks like a HTTP message, |
| 261 | # try to extract JSON from its body. |
| 262 | try: |
| 263 | j = self.strip()[self.strip().rindex('\r\n\r\n'):] |
| 264 | except ValueError: |
| 265 | pass |
| 266 | else: |
| 267 | try: |
| 268 | # noinspection PyAttributeOutsideInit |
| 269 | self._json = json.loads(j) |
| 270 | except ValueError: |
| 271 | pass |
| 272 | return self._json |
| 273 | |
| 274 | |
| 275 | class ExitStatusError(Exception): |