From 9169a18e4041b970df92f5acf39245192a0953ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 24 Jun 2017 23:50:54 +0200 Subject: [PATCH 3/5] jsonresponse: remove unmaintained doctests (#17175) --- passerelle/utils/jsonresponse.py | 224 --------------------------------------- 1 file changed, 224 deletions(-) diff --git a/passerelle/utils/jsonresponse.py b/passerelle/utils/jsonresponse.py index 7d2497b..a86d557 100644 --- a/passerelle/utils/jsonresponse.py +++ b/passerelle/utils/jsonresponse.py @@ -30,230 +30,6 @@ class APIError(RuntimeError): class to_json(object): - """ - Wrap view functions to render python native and custom - objects to json - - >>> from django.test.client import RequestFactory - >>> requests = RequestFactory() - - Simple wrap returning data into json - - >>> @to_json('plain') - ... def hello(request): - ... return dict(hello='world') - - >>> resp = hello(requests.get('/hello/')) - >>> print resp.status_code - 200 - >>> print resp.content - {"hello": "world"} - - Result can be wraped in some api manier - - >>> @to_json('api') - ... def goodbye(request): - ... return dict(good='bye') - >>> resp = goodbye(requests.get('/goodbye', {'debug': 1})) - >>> print resp.status_code - 200 - >>> print resp.content - { - "data": { - "good": "bye" - }, - "err": 0 - } - - Automaticaly error handling - - >>> @to_json('api') - ... def error(request): - ... raise Exception('Wooot!??') - - >>> resp = error(requests.get('/error', {'debug': 1})) - >>> print resp.status_code - 500 - >>> print resp.content # doctest: +NORMALIZE_WHITESPACE - { - "err_class": "Exception", - "err_desc": "Wooot!??", - "data": null, - "err": 1 - } - - >>> from django.core.exceptions import ObjectDoesNotExist - >>> @to_json('api') - ... def error_404(request): - ... raise ObjectDoesNotExist('Not found') - - >>> resp = error_404(requests.get('/error', {'debug': 1})) - >>> print resp.status_code - 404 - >>> print resp.content # doctest: +NORMALIZE_WHITESPACE - { - "err_class": "django.core.exceptions.ObjectDoesNotExist", - "err_desc": "Not found", - "data": null, - "err": 1 - } - - - You can serialize not only pure python data types. - Implement `serialize` method on toplevel object or - each element of toplevel array. - - >>> class User(object): - ... def __init__(self, name, age): - ... self.name = name - ... self.age = age - ... - ... def serialize(self, request): - ... if request.GET.get('with_age', False): - ... return dict(name=self.name, age=self.age) - ... else: - ... return dict(name=self.name) - - >>> @to_json('objects') - ... def users(request): - ... return [User('Bob', 10), User('Anna', 12)] - - >>> resp = users(requests.get('users', { 'debug': 1 })) - >>> print resp.status_code - 200 - >>> print resp.content # doctest: +NORMALIZE_WHITESPACE - { - "data": [ - { - "name": "Bob" - }, - { - "name": "Anna" - } - ], - "err": 0 - } - - You can pass extra args for serialization: - - >>> resp = users(requests.get('users', - ... { 'debug':1, 'with_age':1 })) - >>> print resp.status_code - 200 - >>> print resp.content # doctest: +NORMALIZE_WHITESPACE - { - "data": [ - { - "age": 10, - "name": "Bob" - }, - { - "age": 12, - "name": "Anna" - } - ], - "err": 0 - } - - It is easy to use jsonp, just pass format=jsonp - - >>> resp = users(requests.get('users', - ... { 'debug':1, 'format': 'jsonp' })) - >>> print resp.status_code - 200 - >>> print resp.content # doctest: +NORMALIZE_WHITESPACE - callback({ - "data": [ - { - "name": "Bob" - }, - { - "name": "Anna" - } - ], - "err": 0 - }); - - You can override the name of callback method using - JSONRESPONSE_CALLBACK_NAME option or query arg callback=another_callback - (in this case, format=jsonp if not specified) - - >>> resp = users(requests.get('users', - ... { 'debug':1, 'callback': 'my_callback' })) - >>> print resp.status_code - 200 - >>> print resp.content # doctest: +NORMALIZE_WHITESPACE - my_callback({ - "data": [ - { - "name": "Bob" - }, - { - "name": "Anna" - } - ], - "err": 0 - }); - - You can pass raise=1 to raise exceptions in debug purposes - instead of passing info to json response - - >>> @to_json('api') - ... def error(request): - ... raise Exception('Wooot!??') - - >>> resp = error(requests.get('/error', - ... {'debug': 1, 'raise': 1})) - Traceback (most recent call last): - Exception: Wooot!?? - - You can wraps both methods and functions - - >>> class View(object): - ... @to_json('plain') - ... def render(self, request): - ... return dict(data='ok') - ... @to_json('api') - ... def render_api(self, request): - ... return dict(data='ok') - - - >>> view = View() - >>> resp = view.render(requests.get('/render')) - >>> print resp.status_code - 200 - >>> print resp.content # doctest: +NORMALIZE_WHITESPACE - {"data": "ok"} - - Try it one more - - >>> resp = view.render(requests.get('/render')) - >>> print resp.status_code - 200 - >>> print resp.content # doctest: +NORMALIZE_WHITESPACE - {"data": "ok"} - - Try it one more with api - - >>> resp = view.render_api(requests.get('/render')) - >>> print resp.status_code - 200 - >>> print resp.content # doctest: +NORMALIZE_WHITESPACE - {"data": {"data": "ok"}, "err": 0} - - - You can pass custom kwargs to json.dumps, - just give them to constructor - - >>> @to_json('plain', separators=(', ', ': ')) - ... def custom_kwargs(request): - ... return ['a', { 'b': 1 }] - >>> resp = custom_kwargs(requests.get('/render')) - >>> print resp.status_code - 200 - >>> print resp.content - ["a", {"b": 1}] - """ def __init__(self, serializer_type, error_code=500, wrap_response=True, **kwargs): """ serializer_types: -- 2.13.2