parameters working for GETs now. This is awesome.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 327d923 · Browse files · 1 file·+32 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+32 added−23 removed
- other:+32 added−23 removed
Provenance
- Commit
4f9d598- Tree
56ef04c- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
327d923
1 file changed+32 added−23 removed
modified:requests/core.py+32 added-23 removed
48 unmodified lines49 if not value.lower() in self._METHODS:50 raise InvalidMethod()51 52 object.__setattr__(self, name, value)53- 53 54+ 55 def _checks(self):56 pass57+58 59+ def _get_opener(self):60+ """ Creates appropriate opener object for urllib2.61+ """62+ 63+ if self.auth:64+65+ # create a password manager66+ authr = urllib2.HTTPPasswordMgrWithDefaultRealm()67+68+ authr.add_password(None, self.url, self.auth.username, self.auth.password)69+ handler = urllib2.HTTPBasicAuthHandler(authr)70+ opener = urllib2.build_opener(handler)71+72+ # use the opener to fetch a URL73+ return opener.open74+ else:75+ return urllib2.urlopen76+77+ 78 def send(self, anyway=False):79 """Sends the request. 80 81 :param anyway: If True, request will be sent, even if it has already been sent.2 unmodified lines84 85 if self.method.lower() == 'get':86 if (not self.sent) or anyway:87 try:68- req = urllib2.Request(self.url)69-70- if self.headers:71- r.headers = self.headers72-73- # url encode form data if it's a dict74- if isinstance(self.data, dict):75- req.params = urllib.urlencode(self.params)88+ # url encode GET params if it's a dict89+ if isinstance(self.params, dict):90+ params = urllib.urlencode(self.params)91 else:77- req.params = self.paams92 79- if self.auth:93+ params = self.params94 81- # create a password manager82- password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()95+ req = urllib2.Request("%s?%s" % (self.url, params))96 84- # Add the username and password.85- # If we knew the realm, we could use it instead of ``None``.86- password_mgr.add_password(None, self.url, self.auth.username, self.auth.password)87- handler = urllib2.HTTPBasicAuthHandler(password_mgr)88- opener = urllib2.build_opener(handler)97+ if self.headers:98+ req.headers = self.headers99 90- # use the opener to fetch a URL91- resp = opener.open(req)92- else:93- resp = urllib2.urlopen(req)100+ opener = self._get_opener()101+ resp = opener(req)102 103 self.response.status_code = resp.code104 self.response.headers = resp.info().dict105 self.response.content = resp.read()106 107 success = True108 109 102- except Exception:110+ except RequestException:111 raise RequestException112 113 114 elif self.method.lower() == 'head':105 unmodified lines220 r = Request()221 222 r.method = 'GET'223 r.url = url224+ r.params = params225 r.headers = headers226 r.auth = _detect_auth(url, auth)227 228 r.send()98 unmodified lines