~fom-dev/fom/trunk

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""
    fom.db
    ~~~~~~

    Raw connection and querying.

    :copyright: 2009-2010 Fom Authors.
    :license: MIT, see LICENSE for more information.

    .. attribute:: BASE_URL

        The default FluidDB URL

    .. attribute:: SERIALIZABLE_TYPES

        A set of serializable content types

    .. attribute:: ITERABLE_TYPES

        A set of iterable types of primitive

    .. attribute:: PRIMITIVE_CONTENT_TYPE

        The primitive FluidDB content type

    .. attribute:: DESERIALIZABLE_CONTENT_TYPES

        Content types which can be deserialized
"""

import types
import urllib

import httplib2

try:
    import json
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        # For Google AppEngine
        from django.utils import simplejson as json

from errors import raise_error
from utils import fom_request_sent, fom_response_received
from version import version


BASE_URL = 'https://fluiddb.fluidinfo.com'
NO_CONTENT = object()
PRIMITIVE_CONTENT_TYPE = 'application/vnd.fluiddb.value+json'
DESERIALIZABLE_CONTENT_TYPES = set(
    (PRIMITIVE_CONTENT_TYPE, 'application/json'))
ITERABLE_TYPES = set((list, tuple))
SERIALIZABLE_TYPES = set((types.NoneType, bool, int, float, str, unicode,
                          list, tuple))


def _generate_endpoint_url(base, path, urlargs):
    path_parts = [base]
    for part in path:
        if isinstance(part, unicode):
            part = part.encode('utf-8')
        path_parts.append(urllib.quote(part, safe=''))
    url = '/'.join(path_parts)
    if urlargs:
        if isinstance(urlargs, dict):
            # convert the dict to tuple pairs
            urlargs = tuple(urlargs.items())
        # make sure we handle unicode characters as possible values
        # NOTE: only use UTF-8 unicode for urlargs values. Anything else will
        # break.
        clean_urlargs = []
        for (tag, value) in urlargs:
            if isinstance(value, unicode):
                clean_urlargs.append((tag, value.encode('utf-8')))
            else:
                clean_urlargs.append((tag, value))
        urlargs = tuple(clean_urlargs)
        url = '?'.join([url, urllib.urlencode(urlargs)])
    return url


def _get_body_and_type(payload, content_type):
    if content_type:
        if content_type == 'application/json':
            return json.dumps(payload), content_type
        return payload, content_type
    if payload is NO_CONTENT:
        return None, None
    if isinstance(payload, dict):
        return json.dumps(payload), 'application/json'
    pt = type(payload)
    if pt in SERIALIZABLE_TYPES:
        if pt in ITERABLE_TYPES:
            if not all(isinstance(x, basestring) for x in payload):
                raise ValueError('Non-string in list payload %r.' % (payload,))
        return json.dumps(payload), PRIMITIVE_CONTENT_TYPE
    raise ValueError("Can't handle payload %r of type %s" % (payload, pt))


class FluidResponse(object):
    """A response to a FluidDB request.

    These are generally created by the API, and returned, and there is little
    use to create them manually.

    :param response: An httplib2 response instance, which is a dict with an
        additional status attribute.
    :param content: The body of the HTTP response.
    :param is_value: A boolean flag to indicate whether the response is from a
        *value* request. Value requests are not deserialized unless they are
        of the primitive content type: `application/vnd.fluiddb.value+json`
        even if they are of a deserializable content type such as
        `application/json`

    .. attribute:: content_type

        The content type of the response.

    .. attribute:: value

        The deserialized value of the response body, if it is appropriate for
        deserialization.

    .. attribute:: content

        The raw content of the response body.

    .. attribute:: request_id

        The request id of the response. This is only available during errors.

    .. attribute:: error

        The error from the response. This is only available during errors.
    """

    def __init__(self, response, content, is_value):
        self.content_type = response['content-type']
        if ((is_value and self.content_type == PRIMITIVE_CONTENT_TYPE) or
            (self.content_type in DESERIALIZABLE_CONTENT_TYPES)):
            try:
                self.value = json.loads(content)
            except ValueError:
                self.value = content
        else:
            self.value = content
        self.status = response.status
        self.response = response
        self.content = content
        self.request_id = self.response.get('x-fluiddb-request-id')
        self.error = self.response.get('x-fluiddb-error-class')
        if self.status >= 400:
            raise_error(self)

    def __repr__(self):
        return '<FluidResponse (%s, %r, %r, %r)>' % (self.status,
            self.content_type, self.error, self.value)

    __str__ = __repr__

    # XXX Backwards Compatibility layer
    def __iter__(self):
        print 'Depravacamated use of status, response'
        yield self.status
        yield self.value


class FluidDB(object):
    """HTTP client.

    Could/Should be swapped out for other implementations. Although is
    generally synchronous.

    :param base_url: The base FluidDB url to use. Currently, this can only be
        either the main FluidDB instance, or the sandbox instance.
    """

    def __init__(self, base_url=BASE_URL):
        if base_url.endswith('/'):
            raise ValueError('The domain for FluidDB must *not* end with'\
                             ' "/". Correct example:'\
                             ' https://fluiddb.fluidinfo.com')
        self._http = httplib2.Http()
        self.base_url = base_url
        self.headers = {
            'User-agent': 'fom/%s' % version,
        }
        # XXX Backwards compat
        self.client = self

    def __call__(self, method, path, payload=NO_CONTENT, urlargs=None,
                       content_type=None, is_value=False):
        """Make a request and return a response.

        >>> db = FluidDB()
        >>> r = db('GET', '/users/aliafshar')
        >>> print r.value
        {u'name': u'aliafshar', u'id': u'11e00b96-e346-44e7-af7f-e1a3575ff43e'}

        :param method: The HTTP method
        :param path: The path to make the request to
        :param payload: The body of the request
        :param urlargs: URL arguments to be applied to the request
        :param content_type: The content type of the payload
        :param is_value: A boolean flag to indicate whether the response is
            from a *value* request. Value requests are not deserialized unless
            they are of the primitive content type:
            `application/vnd.fluiddb.value+json` even if they are of a
            deserializable content type such as `application/json`
        """
        req, params = self._build_request(method, path, payload, urlargs,
                                          content_type)
        fom_request_sent.send(self, request=params)
        response, content = req(*params)
        fom_response_received.send(self, response=(response.status,
                                   content, response.copy()))
        return FluidResponse(response, content, is_value)

    def _build_request(self, method, path, payload, urlargs, content_type):
        payload, content_type = _get_body_and_type(payload, content_type)
        urlargs = urlargs or {}
        headers = self._get_headers(content_type)
        url = self._get_url(path, urlargs)
        return self._http.request, (url, method, payload, headers)

    def _get_headers(self, content_type):
        headers = self.headers.copy()
        if content_type:
            headers['content-type'] = content_type
        return headers

    def _get_url(self, path, urlargs=None):
        return _generate_endpoint_url(self.base_url, path, urlargs)

    def login(self, username, password):
        """Log in to this instance of FluidDB

        :param username: The username to log in with.
        :param password: The password to log in with.
        """
        userpass = username + ':' + password
        auth = 'Basic ' + userpass.encode('base64').strip()
        self.headers['Authorization'] = auth

    def login_oauth2(self, token):
        """Prepare to make OAuth2 calls to Fluidinfo.

        :param token: The OAuth token to pass in calls to Fluidinfo.
        """
        self.headers['Authorization'] = 'oauth2'
        self.headers['X-FluidDB-Access-Token'] = token

    def logout(self):
        """Log out of this FluidDB instance
        """
        # Use pop here, to avoid catching KeyError if login was never called.
        self.headers.pop('Authorization', None)
        self.headers.pop('X-FluidDB-Access-Token', None)