| 2394 | |
| 2395 | @functools.wraps(callback) |
| 2396 | def wrapper(*a, **ka): |
| 2397 | try: |
| 2398 | rv = callback(*a, **ka) |
| 2399 | except HTTPResponse as resp: |
| 2400 | rv = resp |
| 2401 | |
| 2402 | if isinstance(rv, dict): |
| 2403 | #Attempt to serialize, raises exception on failure |
| 2404 | json_response = dumps(rv) |
| 2405 | #Set content type only if serialization successful |
| 2406 | response.content_type = 'application/json' |
| 2407 | return json_response |
| 2408 | elif isinstance(rv, HTTPResponse) and isinstance(rv.body, dict): |
| 2409 | rv.body = dumps(rv.body) |
| 2410 | rv.content_type = 'application/json' |
| 2411 | return rv |
| 2412 | |
| 2413 | return wrapper |
| 2414 | |