File: v2.0.0.rst

package info (click to toggle)
python-restless 2.0.1-5~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 472 kB
  • sloc: python: 1,879; makefile: 149
file content (109 lines) | stat: -rw-r--r-- 2,721 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
restless v2.0.0
===============

:date: 2014-05-23

This release improves the way data preparation & serialization are handled.
It introduces these as separate, composable objects (``Preparer`` &
``Serializer``) that are assigned onto a ``Resource``.


Porting from 1.X.X to 2.0.0
---------------------------

Porting is relatively straightforward in both the preparation & serialization
cases.

Preparation
~~~~~~~~~~~

If you were supplying ``fields`` on your ``Resource``, such as::

    # posts/api.py
    from restless.dj import DjangoResource

    from posts.models import Post


    class PostResource(DjangoResource):
        fields = {
            'id': 'id',
            'title': 'title',
            'author': 'user.username',
            'body': 'content',
            'posted_on': 'posted_on',
        }

Porting is simply 1.adding an import & 2. changing the assignment.::

    # posts/api.py
    from restless.dj import DjangoResource
    # 1. ADDED IMPORT
    from restless.preparers import FieldsPreparer

    from posts.models import Post


    class PostResource(DjangoResource):
        # 2. CHANGED ASSIGNMENT
        preparer = FieldsPreparer{
            'id': 'id',
            'title': 'title',
            'author': 'user.username',
            'body': 'content',
            'posted_on': 'posted_on',
        }


Serialization
~~~~~~~~~~~~~

Serialization is even easier. If you performed no overridding, there's nothing
to update. You simply get the new ``JSONSerializer`` object automatically.

If you were overriding either ``raw_deserialize`` or ``raw_serialize``, you
should create a new ``Serializer`` subclass & move the methods over to it,
changing their signatures as you go. Then assign an instance of your new
``Serializer`` subclass onto your ``Resource``(s).

Unported YAML serialization::

    import yaml

    from restless import Resource


    class MyResource(Resource):
        def raw_deserialize(self, body):
            return yaml.safe_load(body)

        def raw_serialize(self, data):
            return yaml.dump(data)

Ported serialization::

    import yaml

    from restless import Resource
    from restless.serializers import Serializer


    class YAMLSerializer(Serializer):
        def deserialize(self, body):
            return yaml.safe_load(body)

        def serialize(self, data):
            return yaml.dump(data)


    class MyResource(Resource):
        serializer = YAMLSerializer()


Features
--------

* Added syntax highlighting to docs. (SHA: d398fdb)
* Added a ``BAD_REQUEST`` constant & associated ``BadRequest`` error.
  (SHA: 93d73d6, SHA: 8d49b51 & SHA: a719c88)
* Moved to composition for data preparation & serialization. (SHA: 38aabb9)