def build(resp):
response = Response()
- response.status_code = getattr(resp, 'code', None)
+ response.status_code = getattr(resp, 'status', None)
try:
- response.headers = CaseInsensitiveDict(getattr(resp.info(), 'dict', None))
- response.fo = resp
+ response.headers = CaseInsensitiveDict(getattr(resp, 'headers', None))
+ response.raw = resp._raw
- if self.cookiejar:
+ # if self.cookiejar:
+
++ response.cookies = dict_from_cookiejar(self.cookiejar)
++
++
+ # response.cookies = dict_from_cookiejar(self.cookiejar)
+ response.cookies = dict_from_cookiejar(self.cookiejar)
+
except AttributeError:
pass
if is_error:
response.error = resp
- response.url = getattr(resp, 'url', None)
-
return response
-
+ # Request collector.
history = []
+ # Create the lone response object.
r = build(resp)
+ # Store the HTTP response, just in case.
+ r._response = resp
+
+ # It's a redirect, and we're not already in a redirect loop.
if r.status_code in REDIRECT_STATI and not self.redirect:
while (
request.send()
r = request.response
+ # Insert collected history.
r.history = history
+ # Attach Response to Request.
self.response = r
+
+ # Give Response some context.
self.response.request = self
-
@staticmethod
def _encode_params(data):
"""Encode parameters in a piece of data.
if isinstance(path, unicode):
path = path.encode('utf-8')
+ # URL-encode the path.
path = urllib.quote(path, safe="%/:=&?~#+!$,;'@()*[]")
+ # Turn it back into a bytestring.
+ self.url = str(urlunparse([scheme, netloc, path, params, query, fragment]))
+ self.url = str(urlunparse(
+ [scheme, netloc, path, params, query, fragment]
+ ))
+ # Query Parameters?
if self._enc_params:
+
+ # If query parameters already exist in the URL, append.
if urlparse(self.url).query:
return '%s&%s' % (self.url, self._enc_params)
+
+ # Otherwise, have at it.
else:
return '%s?%s' % (self.url, self._enc_params)
+
else:
+ # Kosher URL.
return self.url
-
- def send(self, anyway=False):
+ def send(self, connection=None, anyway=False):
+ """Sends the shit."""
+
+ # Safety check.
+ self._checks()
+
+ # Build the final URL.
+ url = self._build_url()
+
+ # Setup Files.
+ if self.files:
+ pass
+
+ # Setup form data.
+ elif self.data:
+ pass
+
+ # Setup cookies.
+ # elif self.cookies:
+ # pass
+
+ # req = _Request(url, data=self._enc_data, method=self.method)
+
+ # Only send the Request if new or forced.
+ if (anyway) or (not self.sent):
+
+ try:
+ # Create a new HTTP connection, since one wasn't passed in.
+ if not connection:
+ connection = urllib3.connection_from_url(url,
+ timeout=self.timeout)
+
+ # One-off request. Delay fetching the content until needed.
+ do_block = False
+ else:
+ # Part of a connection pool, so no fancy stuff. Sorry!
+ do_block = True
+
+ # Create the connection.
+ r = connection.urlopen(
+ method=self.method,
+ url=url,
+ body=self.data,
+ headers=self.headers,
+ redirect=False,
+ assert_same_host=False,
+ block=do_block
+ )
+
+ # Extract cookies.
+ # if self.cookiejar is not None:
+ # self.cookiejar.extract_cookies(resp, req)
+
+ # except (urllib2.HTTPError, urllib2.URLError), why:
+ except Exception, why:
+ # if hasattr(why, 'reason'):
+ # if isinstance(why.reason, socket.timeout):
+ # why = Timeout(why)
+
+ # self._build_response(why, is_error=True)
+ print 'FUCK'
+ print why
+
+ else:
+ self._build_response(r)
+ self.response.ok = True
+
+
+ self.sent = self.response.ok
+
+ return self.sent
+
+
+
+ def old_send(self, anyway=False):
"""Sends the request. Returns True of successful, false if not.
If there was an HTTPError during transmission,
self.response.status_code will contain the HTTPError code.
self._content_consumed = True
return self._content
-
def raise_for_status(self):
- """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""
+ """Raises stored :class:`HTTPError` or :class:`URLError`,
+ if one occured.
+ """
+
if self.error:
raise self.error
+ if (self.status_code >= 300) and (self.status_code < 400):
+ raise Exception('300 yo')
+
++
+ elif (self.status_code >= 400) and (self.status_code < 500):
+ raise Exception('400 yo')
+
+ elif (self.status_code >= 500) and (self.status_code < 600):
+ raise Exception('500 yo')
+
class AuthManager(object):
"""Requests Authentication Manager."""