Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 87ff51a

Browse files
committed
More tweaking
1 parent a5e0c4b commit 87ff51a

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

coreapi/transports/http.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,6 @@
1212
import uritemplate
1313

1414

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-
5015
def _get_http_method(action):
5116
if not action:
5217
return 'GET'
@@ -135,6 +100,46 @@ def _make_http_request(url, method, headers=None, query_params=None, form_params
135100
return requests.request(method, url, **opts)
136101

137102

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+
138143
def _decode_result(response, decoders=None):
139144
"""
140145
Given an HTTP response, return the decoded Core API document.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy