- uri = self.uri
- method = "GET"
- for action in POST_ACTIONS:
- if self.uri.endswith(action):
- method = "POST"
- if (self.agent):
- kwargs["source"] = self.agent
- break
-
- if (not self.encoded_args):
- if kwargs.has_key('id'):
- uri += "/%s" %(kwargs['id'])
-
- self.encoded_args = urlencode(kwargs.items())
-
- argStr = ""
- argData = None
- if (method == "GET"):
- if self.encoded_args:
- argStr = "?%s" %(self.encoded_args)
- else:
- argData = self.encoded_args
-
- headers = {}
- if (self.agent):
- headers["X-Twitter-Client"] = self.agent
- if (self.username):
- headers["Authorization"] = "Basic " + b64encode("%s:%s" %(
- self.username, self.password))
-
- req = urllib2.Request(
- "http://%s/%s.%s%s" %(self.domain, uri, self.format, argStr),
- argData, headers
- )
+ # Build the uri.
+ uriparts = []
+ for uripart in self.uriparts:
+ # If this part matches a keyword argument, use the
+ # supplied value otherwise, just use the part.
+ uriparts.append(str(kwargs.pop(uripart, uripart)))
+ uri = '/'.join(uriparts)
+
+ method = kwargs.pop('_method', None)
+ if not method:
+ method = "GET"
+ for action in POST_ACTIONS:
+ if re.search("%s(/\d+)?$" % action, uri):
+ method = "POST"
+ break
+
+ # If an id kwarg is present and there is no id to fill in in
+ # the list of uriparts, assume the id goes at the end.
+ id = kwargs.pop('id', None)
+ if id:
+ uri += "/%s" %(id)
+
+ # If an _id kwarg is present, this is treated as id as a CGI
+ # param.
+ _id = kwargs.pop('_id', None)
+ if _id:
+ kwargs['id'] = _id
+
+ # If an _timeout is specified in kwargs, use it
+ _timeout = kwargs.pop('_timeout', None)
+
+ secure_str = ''
+ if self.secure:
+ secure_str = 's'
+ dot = ""
+ if self.format:
+ dot = "."
+ uriBase = "http%s://%s/%s%s%s" %(
+ secure_str, self.domain, uri, dot, self.format)
+
+ headers = {'Accept-Encoding': 'gzip'} if self.gzip else dict()
+ body = None; arg_data = None
+ if self.auth:
+ headers.update(self.auth.generate_headers())
+ arg_data = self.auth.encode_params(uriBase, method, kwargs)
+ if method == 'GET':
+ uriBase += '?' + arg_data
+ else:
+ body = arg_data.encode('utf8')
+
+ req = urllib_request.Request(uriBase, body, headers)
+ return self._handle_response(req, uri, arg_data, _timeout)
+
+ def _handle_response(self, req, uri, arg_data, _timeout=None):
+ kwargs = {}
+ if _timeout:
+ kwargs['timeout'] = _timeout