|
1 | 1 | import io |
2 | 2 | import requests |
| 3 | +from .exception import {{spec.title | caseUcfirst}}Exception |
3 | 4 |
|
4 | 5 | class Client: |
5 | 6 | def __init__(self): |
@@ -63,26 +64,32 @@ class Client: |
63 | 64 | if isinstance(data[key], io.BufferedIOBase): |
64 | 65 | files[key] = data[key] |
65 | 66 | del data[key] |
66 | | - |
67 | | - response = requests.request( # call method dynamically https://stackoverflow.com/a/4246075/2299554 |
68 | | - method=method, |
69 | | - url=self._endpoint + path, |
70 | | - params=self.flatten(params), |
71 | | - data=self.flatten(data), |
72 | | - json=json, |
73 | | - files=files, |
74 | | - headers=headers, |
75 | | - verify=self._self_signed, |
76 | | - ) |
77 | | - |
78 | | - response.raise_for_status() |
79 | | - |
80 | | - content_type = response.headers['Content-Type'] |
81 | | - |
82 | | - if content_type.startswith('application/json'): |
83 | | - return response.json() |
84 | | - |
85 | | - return response._content |
| 67 | + response = None |
| 68 | + try: |
| 69 | + response = requests.request( # call method dynamically https://stackoverflow.com/a/4246075/2299554 |
| 70 | + method=method, |
| 71 | + url=self._endpoint + path, |
| 72 | + params=self.flatten(params), |
| 73 | + data=self.flatten(data), |
| 74 | + json=json, |
| 75 | + files=files, |
| 76 | + headers=headers, |
| 77 | + verify=self._self_signed, |
| 78 | + ) |
| 79 | + |
| 80 | + response.raise_for_status() |
| 81 | + |
| 82 | + content_type = response.headers['Content-Type'] |
| 83 | + |
| 84 | + if content_type.startswith('application/json'): |
| 85 | + return response.json() |
| 86 | + |
| 87 | + return response._content |
| 88 | + except Exception as e: |
| 89 | + if response.json(): |
| 90 | + raise {{spec.title | caseUcfirst}}Exception(response.json()['message'], response.status_code, response.json()) |
| 91 | + else: |
| 92 | + raise {{spec.title | caseUcfirst}}Exception(e) |
86 | 93 |
|
87 | 94 | def flatten(self, data, prefix=''): |
88 | 95 | output = {} |
|
0 commit comments