|
12 | 12 | import uritemplate
|
13 | 13 |
|
14 | 14 |
|
15 |
| -def _coerce_to_error_content(node): |
16 |
| - # Errors should not contain nested documents or links. |
17 |
| - # If we get a 4xx or 5xx response with a Document, then coerce it |
18 |
| - # into plain data. |
19 |
| - if isinstance(node, (Document, Object)): |
20 |
| - # Strip Links from Documents, treat Documents as plain dicts. |
21 |
| - return OrderedDict([ |
22 |
| - (key, _coerce_to_error_content(value)) |
23 |
| - for key, value in node.data.items() |
24 |
| - ]) |
25 |
| - elif isinstance(node, Array): |
26 |
| - # Strip Links from Arrays. |
27 |
| - return [ |
28 |
| - _coerce_to_error_content(item) |
29 |
| - for item in node |
30 |
| - if not isinstance(item, Link) |
31 |
| - ] |
32 |
| - return node |
33 |
| - |
34 |
| - |
35 |
| -def _coerce_to_error(obj, default_title): |
36 |
| - if isinstance(obj, Document): |
37 |
| - return Error( |
38 |
| - title=obj.title or default_title, |
39 |
| - content=_coerce_to_error_content(obj) |
40 |
| - ) |
41 |
| - elif isinstance(obj, dict): |
42 |
| - return Error(title=default_title, content=obj) |
43 |
| - elif isinstance(obj, list): |
44 |
| - return Error(title=default_title, content={'messages': obj}) |
45 |
| - elif obj is None: |
46 |
| - return Error(title=default_title) |
47 |
| - return Error(title=default_title, content={'message': obj}) |
48 |
| - |
49 |
| - |
50 | 15 | def _get_http_method(action):
|
51 | 16 | if not action:
|
52 | 17 | return 'GET'
|
@@ -135,6 +100,46 @@ def _make_http_request(url, method, headers=None, query_params=None, form_params
|
135 | 100 | return requests.request(method, url, **opts)
|
136 | 101 |
|
137 | 102 |
|
| 103 | +def _coerce_to_error_content(node): |
| 104 | + """ |
| 105 | + Errors should not contain nested documents or links. |
| 106 | + If we get a 4xx or 5xx response with a Document, then coerce |
| 107 | + the document content into plain data. |
| 108 | + """ |
| 109 | + if isinstance(node, (Document, Object)): |
| 110 | + # Strip Links from Documents, treat Documents as plain dicts. |
| 111 | + return OrderedDict([ |
| 112 | + (key, _coerce_to_error_content(value)) |
| 113 | + for key, value in node.data.items() |
| 114 | + ]) |
| 115 | + elif isinstance(node, Array): |
| 116 | + # Strip Links from Arrays. |
| 117 | + return [ |
| 118 | + _coerce_to_error_content(item) |
| 119 | + for item in node |
| 120 | + if not isinstance(item, Link) |
| 121 | + ] |
| 122 | + return node |
| 123 | + |
| 124 | + |
| 125 | +def _coerce_to_error(obj, default_title): |
| 126 | + """ |
| 127 | + Given an arbitrary return result, coerce it into an Error instance. |
| 128 | + """ |
| 129 | + if isinstance(obj, Document): |
| 130 | + return Error( |
| 131 | + title=obj.title or default_title, |
| 132 | + content=_coerce_to_error_content(obj) |
| 133 | + ) |
| 134 | + elif isinstance(obj, dict): |
| 135 | + return Error(title=default_title, content=obj) |
| 136 | + elif isinstance(obj, list): |
| 137 | + return Error(title=default_title, content={'messages': obj}) |
| 138 | + elif obj is None: |
| 139 | + return Error(title=default_title) |
| 140 | + return Error(title=default_title, content={'message': obj}) |
| 141 | + |
| 142 | + |
138 | 143 | def _decode_result(response, decoders=None):
|
139 | 144 | """
|
140 | 145 | Given an HTTP response, return the decoded Core API document.
|
|
0 commit comments