binder.py 6.56 KB
Newer Older
1
# Tweepy
2
3
# Copyright 2009-2010 Joshua Roesslein
# See LICENSE for details.
4

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

Josh Roesslein's avatar
Josh Roesslein committed
10
from tweepy.error import TweepError
11

12
13
re_path_template = re.compile('{\w+}')

Josh Roesslein's avatar
Josh Roesslein committed
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
def bind_api(**config):

    class APIMethod(object):

        path = config['path']
        payload_type = config.get('payload_type', None)
        payload_list = config.get('payload_list', False)
        allowed_param = config.get('allowed_param', [])
        method = config.get('method', 'GET')
        require_auth = config.get('require_auth', False)
        search_api = config.get('search_api', False)

        def __init__(self, api, args, kargs):
            # If authentication is required and no credentials
            # are provided, throw an error.
            if self.require_auth and not api.auth:
                raise TweepError('Authentication required!')

            self.api = api
            self.post_data = kargs.pop('post_data', None)
            self.retry_count = kargs.pop('retry_count', api.retry_count)
            self.retry_delay = kargs.pop('retry_delay', api.retry_delay)
            self.retry_errors = kargs.pop('retry_errors', api.retry_errors)
            self.headers = kargs.pop('headers', {})
            self.build_parameters(args, kargs)

            # Pick correct URL root to use
            if self.search_api:
                self.api_root = api.search_root
            else:
                self.api_root = api.api_root

            # Perform any path variable substitution
            self.build_path()
49

50
            if api.secure:
51
                self.scheme = 'https://'
52
            else:
53
54
55
56
                self.scheme = 'http://'

            if self.search_api:
                self.host = api.search_host
57
            else:
58
59
                self.host = api.host

60
61
62
63
64
65
            # Manually set Host header to fix an issue in python 2.5
            # or older where Host is set including the 443 port.
            # This causes Twitter to issue 301 redirect.
            # See Issue http://github.com/joshthecoder/tweepy/issues/#issue/12
            self.headers['Host'] = self.host

66
67
68
69
70
        def build_parameters(self, args, kargs):
            self.parameters = {}
            for idx, arg in enumerate(args):

                try:
71
                    self.parameters[self.allowed_param[idx]] = convert_to_utf8_str(arg)
72
73
74
75
76
77
78
79
80
                except IndexError:
                    raise TweepError('Too many parameters supplied!')

            for k, arg in kargs.items():
                if arg is None:
                    continue
                if k in self.parameters:
                    raise TweepError('Multiple values for parameter %s supplied!' % k)

81
                self.parameters[k] = convert_to_utf8_str(arg)
82
83
84
85
86
87
88
89
90
91
92
93
94
95

        def build_path(self):
            for variable in re_path_template.findall(self.path):
                name = variable.strip('{}')

                if name == 'user' and self.api.auth:
                    value = self.api.auth.get_username()
                else:
                    try:
                        value = urllib.quote(self.parameters[name])
                    except KeyError:
                        raise TweepError('No parameter value found for path variable: %s' % name)
                    del self.parameters[name]

96
97
                self.path = self.path.replace(variable, value)

98
99
100
101
102
103
104
105
106
        def execute(self):
            # Build the request URL
            url = self.api_root + self.path
            if len(self.parameters):
                url = '%s?%s' % (url, urllib.urlencode(self.parameters))

            # Query the cache if one is available
            # and this request uses a GET method.
            if self.api.cache and self.method == 'GET':
107
                cache_result = self.api.cache.get(url)
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
                # 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 = self.api
                    else:
                        cache_result._api = self.api
                    return cache_result

            # Continue attempting request until successful
            # or maximum number of retries is reached.
            retries_performed = 0
            while retries_performed < self.retry_count + 1:
                # Open connection
                # FIXME: add timeout
                if self.api.secure:
                    conn = httplib.HTTPSConnection(self.host)
                else:
                    conn = httplib.HTTPConnection(self.host)

                # Apply authentication
                if self.api.auth:
                    self.api.auth.apply_auth(
                            self.scheme + self.host + url,
                            self.method, self.headers, self.parameters
                    )

                # Execute request
                try:
                    conn.request(self.method, url, headers=self.headers, body=self.post_data)
                    resp = conn.getresponse()
                except Exception, e:
                    raise TweepError('Failed to send request: %s' % e)

                # Exit request loop if non-retry error code
                if self.retry_errors:
                    if resp.status not in self.retry_errors: break
                else:
                    if resp.status == 200: break
148

149
150
151
                # Sleep before retrying request again
                time.sleep(self.retry_delay)
                retries_performed += 1
Josh Roesslein's avatar
Josh Roesslein committed
152

153
154
155
156
            # If an error was returned, throw an exception
            self.api.last_response = resp
            if resp.status != 200:
                try:
Joshua's avatar
Joshua committed
157
                    error_msg = self.api.parser.parse_error(self, resp.read())
158
159
160
161
162
                except Exception:
                    error_msg = "Twitter error response: status code = %s" % resp.status
                raise TweepError(error_msg)

            # Parse the response payload
163
            result = self.api.parser.parse(self, resp.read())
164
165

            conn.close()
Josh Roesslein's avatar
Josh Roesslein committed
166

167
168
169
            # Store result into cache if one is available.
            if self.api.cache and self.method == 'GET' and result:
                self.api.cache.store(url, result)
170

171
            return result
Josh Roesslein's avatar
Josh Roesslein committed
172
173


174
175
176
177
    def _call(api, *args, **kargs):

        method = APIMethod(api, args, kargs)
        return method.execute()
Josh Roesslein's avatar
Josh Roesslein committed
178

179
180

    # Set pagination mode
181
    if 'cursor' in APIMethod.allowed_param:
182
        _call.pagination_mode = 'cursor'
183
    elif 'page' in APIMethod.allowed_param:
184
        _call.pagination_mode = 'page'
185

Josh Roesslein's avatar
Josh Roesslein committed
186
187
    return _call