binder.py 5.94 KB
Newer Older
1
2
3
4
# Tweepy
# Copyright 2009 Joshua Roesslein
# See LICENSE

5
import httplib
Josh Roesslein's avatar
Josh Roesslein committed
6
import urllib
7
import time
8

Josh Roesslein's avatar
Josh Roesslein committed
9
10
from . parsers import parse_error
from . error import TweepError
11

12
13
14
15
16
17
18
19
20
21
22
try:
    import json #Python >= 2.6
except ImportError:
    try:
        import simplejson as json #Python < 2.6
    except ImportError:
        try:
            from django.utils import simplejson as json #Google App Engine
        except ImportError:
            raise ImportError, "Can't load a json library"

Josh Roesslein's avatar
Josh Roesslein committed
23

24
def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False,
25
              timeout=None, host=None):
26

Josh Roesslein's avatar
Josh Roesslein committed
27
28
29
30
31
    def _call(api, *args, **kargs):
        # If require auth, throw exception if credentials not provided
        if require_auth and not api.auth_handler:
            raise TweepError('Authentication required!')

Josh Roesslein's avatar
Josh Roesslein committed
32
33
        # check for post data
        post_data = kargs.pop('post_data', None)
Josh Roesslein's avatar
Josh Roesslein committed
34

35
        # check for retry request parameters
36
37
        retry_count = kargs.pop('retry_count', api.retry_count)
        retry_delay = kargs.pop('retry_delay', api.retry_delay)
38
        retry_errors = kargs.pop('retry_errors', api.retry_errors)
39

Josh Roesslein's avatar
Josh Roesslein committed
40
        # check for headers
Josh Roesslein's avatar
Josh Roesslein committed
41
        headers = kargs.pop('headers', {})
Josh Roesslein's avatar
Josh Roesslein committed
42
43
44
45
46

        # build parameter dict
        if allowed_param:
            parameters = {}
            for idx, arg in enumerate(args):
47
                encoded_arg = arg.encode('utf-8')  # must encode parameters to utf-8
Josh Roesslein's avatar
Josh Roesslein committed
48
                try:
49
                    parameters[allowed_param[idx]] = encoded_arg
Josh Roesslein's avatar
Josh Roesslein committed
50
51
52
53
54
55
56
57
58
                except IndexError:
                    raise TweepError('Too many parameters supplied!')
            for k, arg in kargs.items():
                if arg is None:
                    continue
                if k in parameters:
                    raise TweepError('Multiple values for parameter %s supplied!' % k)
                if k not in allowed_param:
                    raise TweepError('Invalid parameter %s supplied!' % k)
59
                parameters[k] = arg.encode('utf-8')
60
        else:
Josh Roesslein's avatar
Josh Roesslein committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
            if len(args) > 0 or len(kargs) > 0:
                raise TweepError('This method takes no parameters!')
            parameters = None

        # Build url with parameters
        if parameters:
            url = '%s?%s' % (api.api_root + path, urllib.urlencode(parameters))
        else:
            url = api.api_root + path

        # Check cache if caching enabled and method is GET
        if api.cache and method == 'GET':
            cache_result = api.cache.get(url, timeout)
            # if cache result found and not expired, return it
            if cache_result:
                # must restore api reference
                if isinstance(cache_result, list):
                    for result in cache_result:
                        result._api = api
                else:
                    cache_result._api = api
                return cache_result

84
        # get scheme and host
Josh Roesslein's avatar
Josh Roesslein committed
85
        if api.secure:
86
            scheme = 'https://'
Josh Roesslein's avatar
Josh Roesslein committed
87
        else:
88
89
            scheme = 'http://'
        _host = host or api.host
Josh Roesslein's avatar
Josh Roesslein committed
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
        # Continue attempting request until successful
        # or maxium number of retries is reached.
        retries_performed = 0
        while retries_performed < retry_count + 1:
            # Open connection
            # FIXME: add timeout
            if api.secure:
                conn = httplib.HTTPSConnection(_host)
            else:
                conn = httplib.HTTPConnection(_host)

            # Apply authentication
            if api.auth_handler:
                api.auth_handler.apply_auth(
                        scheme + _host + url,
                        method, headers, parameters
                )

            # Build request
            conn.request(method, url, headers=headers, body=post_data)

            # Get response
            resp = conn.getresponse()

115
            # Exit request loop if non-retry error code
116
117
118
119
            if retry_errors is None:
                if resp.status == 200: break
            else:
                if resp.status not in retry_errors: break
120
121
122
123

            # Sleep before retrying request again
            time.sleep(retry_delay)
            retries_performed += 1
Josh Roesslein's avatar
Josh Roesslein committed
124
125

        # If an error was returned, throw an exception
126
        api.last_response = resp
Josh Roesslein's avatar
Josh Roesslein committed
127
128
        if resp.status != 200:
            try:
Josh Roesslein's avatar
Josh Roesslein committed
129
                error_msg = parse_error(json.loads(resp.read()))
Josh Roesslein's avatar
Josh Roesslein committed
130
131
132
133
            except Exception:
                error_msg = "Twitter error response: status code = %s" % resp.status
            raise TweepError(error_msg)

134
135
136
137
138
139
        # Parse json respone body
        try:
            jobject = json.loads(resp.read())
        except Exception:
            raise TweepError("Failed to parse json response text")

140
141
142
143
144
145
146
147
        # Parse cursor infomation
        if isinstance(jobject, dict):
            next_cursor = jobject.get('next_cursor')
            prev_cursor = jobject.get('previous_cursor')
        else:
            next_cursor = None
            prev_cursor = None

148
        # Pass json object into parser
149
        try:
150
151
152
153
            if next_cursor is not None and prev_cursor is not None:
                out = parser(jobject, api), next_cursor, prev_cursor
            else:
                out = parser(jobject, api)
154
        except Exception:
155
            raise TweepError("Failed to parse json object")
156

Josh Roesslein's avatar
Josh Roesslein committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
        conn.close()

        # validate result
        if api.validate:
            # list of results
            if isinstance(out, list) and len(out) > 0:
                if hasattr(out[0], 'validate'):
                    for result in out:
                        result.validate()
            # single result
            else:
                if hasattr(out, 'validate'):
                    out.validate()

        # store result in cache
        if api.cache and method == 'GET':
            api.cache.store(url, out)

        return out

177
178
179
180
181
182

    # Set pagination mode
    if 'cursor' in allowed_param:
        _call.pagination_mode = 'cursor'
    elif 'page' in allowed_param:
        _call.pagination_mode = 'page'
183

Josh Roesslein's avatar
Josh Roesslein committed
184
185
    return _call