Merge branch 'develop'
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 · parents 4f6fc98, b37b977 · Browse files · 9 files·+186 added−85 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+186 added−85 removed
- other:+186 added−85 removed
Provenance
- Commit
82e8fdb- Tree
ee78ca2- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
4f6fc98- Parent 2
b37b977
9 files changed+186 added−85 removed
added:docs/community/out-there.rst+63 added
1+Modules2+=======3+4+- `requests-oauth-hook <https://github.com/maraujop/requests-oauth-hook>`_, adds OAuth support to Requests.5+- `FacePy <https://github.com/jgorset/facepy>`_, a Python wrapper to the Facebook API.6+- `robotframework-requests <https://github.com/bulkan/robotframework-requests>`_, a Robot Framework API wrapper.7+- `fullerene <https://github.com/bitprophet/fullerene>`_, a Graphite Dashboard.8+- `urbanairship-python <https://github.com/benjaminws/urbanairship-python>`_, a fork of the Urban Airship API wrapper.9+10+11+Articles & Talks12+================13+- `Python for the Web <http://gun.io/blog/python-for-the-web/>`_ teaches how to use Python to interact with the web, using Requests.14+- `Daniel Greenfield's Review of Requests <http://pydanny.blogspot.com/2011/05/python-http-requests-for-humans.html>`_15+- `My 'Python for Humans' talk <http://python-for-humans.heroku.com>`_ ( `audio <http://codeconf.s3.amazonaws.com/2011/pycodeconf/talks/PyCodeConf2011%20-%20Kenneth%20Reitz.m4a>`_ )16+- `Issac Kelly's 'Consuming Web APIs' talk <http://issackelly.github.com/Consuming-Web-APIs-with-Python-Talk/slides/slides.html>`_17+- `Blog post about Requests via Yum <http://arunsag.wordpress.com/2011/08/17/new-package-python-requests-http-for-humans/>`_18+- `Russian blog post introducing Requests <http://habrahabr.ru/blogs/python/126262/>`_19+20+21+Integrations22+============23+24+ScraperWiki25+------------26+27+`ScraperWiki <https://scraperwiki.com/>`_ is an excellent service that allows28+you to run Python, Ruby, and PHP scraper scripts on the web. Now, Requests29+v0.6.1 is available to use in your scrapers!30+31+To give it a try, simply::32+33+ import requests34+35+36+Managed Packages37+================38+39+Requests is available in a number of popular package formats. Of course,40+the ideal way to install Requests is via The Cheeseshop.41+42+43+Ubuntu & Debian44+---------------45+46+Requests is available installed as a Debian package! Debian Etch Ubuntu, since Oneiric::47+48+ $ apt-get install python-requests49+50+Unfortunately, the most recent version available is v0.5.0. If you're on the51+Debian Python Package team, I'd love an update of that :)52+53+54+Fedora and RedHat55+-----------------56+57+You can easily install Requests v0.6.1 with yum on rpm-based systems::58+59+ $ yum install python-requests60+61+62+63+modified:docs/index.rst+1 added
85 unmodified lines86 .. toctree::87 :maxdepth: 288 89 community/faq90+ community/out-there.rst91 community/support92 community/updates93 94 API Documentation23 unmodified linesmodified:requests/core.py+2 added-2 removed
10 unmodified lines11 12 """13 14 __title__ = 'requests'15-__version__ = '0.6.4'16-__build__ = 0x00060415+__version__ = '0.6.5'16+__build__ = 0x00060517 __author__ = 'Kenneth Reitz'18 __license__ = 'ISC'19 __copyright__ = 'Copyright 2011 Kenneth Reitz'20 9 unmodified linesmodified:requests/sessions.py+65 added-16 removed
14 unmodified lines15 from .utils import add_dict_to_cookiejar16 17 18 19+def merge_kwargs(local_kwarg, default_kwarg):20+ """Merges kwarg dictionaries.21+22+ If a local key in the dictionary is set to None, it will be removed.23+ """24+25+ if default_kwarg is None:26+ return local_kwarg27+28+ if local_kwarg is None:29+ return default_kwarg30+31+ # Bypass if not a dictionary (e.g. timeout)32+ if not hasattr(default_kwarg, 'items'):33+ return local_kwarg34+35+36+37+ # Update new values.38+ kwargs = default_kwarg.copy()39+ kwargs.update(local_kwarg)40+41+ # Remove keys that are set to None.42+ for (k,v) in local_kwarg.items():43+ if v is None:44+ del kwargs[k]45+46+ return kwargs47+48+49 class Session(object):50 """A Requests session."""51 52 __attrs__ = ['headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks']53 54 25- def __init__(self, **kwargs):55+ def __init__(self,56+ headers=None,57+ cookies=None,58+ auth=None,59+ timeout=None,60+ proxies=None,61+ hooks=None):62 63+ self.headers = headers or {}64+ self.cookies = cookies or {}65+ self.auth = auth66+ self.timeout = timeout67+ self.proxies = proxies or {}68+ self.hooks = hooks or {}69+70 # Set up a CookieJar to be used by default71 self.cookies = cookielib.FileCookieJar()72 30- # Map args from kwargs to instance-local variables31- map(lambda k, v: (k in self.__attrs__) and setattr(self, k, v),32- kwargs.iterkeys(), kwargs.itervalues())33-73 # Map and wrap requests.api methods74 self._map_api_methods()75 76 3 unmodified lines80 def __enter__(self):81 return self82 83 def __exit__(self, *args):45- # print args84 pass85 48-86 def _map_api_methods(self):87 """Reads each available method from requests.api and decorates88 them with a wrapper, which inserts any instance-local attributes89 (from __attrs__) that have been set, combining them with **kwargs.90 """91 92 def pass_args(func):93 def wrapper_func(*args, **kwargs):57- inst_attrs = dict((k, v) for k, v in self.__dict__.iteritems()58- if k in self.__attrs__)59- # Combine instance-local values with kwargs values, with60- # priority to values in kwargs61- kwargs = dict(inst_attrs.items() + kwargs.items())94 95+ # Argument collector.96+ _kwargs = {}97+98 # If a session request has a cookie_dict, inject the99 # values into the existing CookieJar instead.100 if isinstance(kwargs.get('cookies', None), dict):101 kwargs['cookies'] = add_dict_to_cookiejar(67- inst_attrs['cookies'], kwargs['cookies']102+ self.cookies, kwargs['cookies']103 )104 70- if kwargs.get('headers', None) and inst_attrs.get('headers', None):71- kwargs['headers'].update(inst_attrs['headers'])105+ for attr in self.__attrs__:106+ # for attr in ['headers',]:107+ s_val = self.__dict__.get(attr)108+ r_val = kwargs.get(attr)109 110+ new_attr = merge_kwargs(r_val, s_val)111+112+ # Skip attributes that were set to None.113+ if new_attr is not None:114+ _kwargs[attr] = new_attr115+116+ # Make sure we didn't miss anything.117+ for (k, v) in kwargs.items():118+ if k not in _kwargs:119+ _kwargs[k] = v120+73- return func(*args, **kwargs)121+ return func(*args, **_kwargs)122+123 return wrapper_func124 125 # Map and decorate each function available in requests.api126 map(lambda fn: setattr(self, fn, pass_args(getattr(api, fn))),7 unmodified linesmodified:.gitignore+1 added
5 unmodified lines6 *.pyc7 docs/_build8 toy.py9 .gitignore10+junit-report.xmlmodified:HISTORY.rst+7 added
1 History2 -------3 4+0.6.5 (2011-10-19)5+++++++++++++++++++6+7+* Offline (fast) test suite.8+* Session dictionary argument merging.9+10+11 0.6.4 (2011-10-13)12 ++++++++++++++++++13 14 * Automatic decoding of unicode, based on HTTP Headers.174 unmodified linesmodified:Makefile+4 added-2 removed
1 init:2 pip install -r reqs.txt3 4 test:5- nosetests test_requests.py --processes=305+ nosetests test_requests.py --with-color6 7 ci: init8- nosetests --search-test --processes=30 --with-nosexunit test_requests.py8+ nosetests test_requests.py --with-xunit --xunit-file=junit-report.xml9+10+stats:11 pyflakes requests | awk -F\: '{printf "%s:%s: [E]%s\n", $1, $2, $3}' > violations.pyflakes.txt12 13 site:14 cd docs; make dirhtml2 unmodified linesadded:reqs.txt+7 added
1+envoy==0.0.22+httpbin==0.0.43+gunicorn4+nose5+pyflakes6+omnijson7+rudolfmodified:test_requests.py+36 added-65 removed
1 unmodified line2 # -*- coding: utf-8 -*-3 4 from __future__ import with_statement5 6-import unittest6+import time7 import cookielib8+import os9+import unittest10 11+import envoy12+13 try:14 import omnijson as json15 except ImportError:16 import json1 unmodified line18 import requests19 20 from requests.sessions import Session21 22+PORT = os.environ.get('HTTPBIN_PORT', '7045')23 19-HTTPBIN_URL = 'http://httpbin.ep.io/'20-HTTPSBIN_URL = 'https://httpbin.ep.io/'24+HTTPBIN_URL = 'http://0.0.0.0:%s/' % (PORT)25+# HTTPBIN_URL = 'http://127.0.0.1:8000/'26 22-# HTTPBIN_URL = 'http://staging.httpbin.org/'23-# HTTPSBIN_URL = 'https://httpbin-staging.ep.io/'27 25-28 def httpbin(*suffix):29 """Returns url for HTTPBIN resource."""30 31 return HTTPBIN_URL + '/'.join(suffix)32 33 32-def httpsbin(*suffix):33- """Returns url for HTTPSBIN resource."""34+SERVICES = (httpbin, )35 35- return HTTPSBIN_URL + '/'.join(suffix)36+_httpbin = False37 37-38-SERVICES = (httpbin, httpsbin)39-40-41-38 class RequestsTestSuite(unittest.TestCase):39 """Requests test cases."""40 41 # It goes to eleven.42 _multiprocess_can_split_ = True43 44 def setUp(self):49- pass45 46+ global _httpbin47 48+ if not _httpbin:49+50+ self.httpbin = envoy.connect('gunicorn httpbin:app --bind=0.0.0.0:%s' % (PORT))51+52+ _httpbin = True53+ time.sleep(1)54+55+56+57 def tearDown(self):58 """Teardown."""54- pass59+ # self.httpbin.kill()60 61 62 def test_invalid_url(self):63 self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')64 65 66 def test_HTTP_200_OK_GET(self):62- r = requests.get(httpbin('/'))67+ r = requests.get(httpbin('/get'))68 self.assertEqual(r.status_code, 200)69 70 def test_HTTP_302_ALLOW_REDIRECT_GET(self):71 r = requests.get(httpbin('redirect', '1'))2 unmodified lines74 def test_HTTP_302_GET(self):75 r = requests.get(httpbin('redirect', '1'), allow_redirects=False)76 self.assertEqual(r.status_code, 302)77 73- def test_HTTPS_200_OK_GET(self):74- r = requests.get(httpsbin('/'))75- self.assertEqual(r.status_code, 200)78 77-79 def test_HTTP_200_OK_GET_WITH_PARAMS(self):80 heads = {'User-agent': 'Mozilla/5.0'}81 82 r = requests.get(httpbin('user-agent'), headers=heads)29 unmodified lines112 self.assertTrue(heads['user-agent'] in r.content)113 114 115 def test_HTTP_200_OK_HEAD(self):115- r = requests.head(httpbin('/'))116+ r = requests.head(httpbin('/get'))117 self.assertEqual(r.status_code, 200)118 119 119- def test_HTTPS_200_OK_HEAD(self):120- r = requests.head(httpsbin('/'))121- self.assertEqual(r.status_code, 200)122-123-120 def test_HTTP_200_OK_PUT(self):121 r = requests.put(httpbin('put'))122 self.assertEqual(r.status_code, 200)123 124 129- def test_HTTPS_200_OK_PUT(self):130- r = requests.put(httpsbin('put'))131- self.assertEqual(r.status_code, 200)132-133-125 def test_HTTP_200_OK_PATCH(self):126 r = requests.patch(httpbin('patch'))127 self.assertEqual(r.status_code, 200)128 129 139- def test_HTTPS_200_OK_PATCH(self):140- r = requests.patch(httpsbin('patch'))141- self.assertEqual(r.status_code, 200)142-143-130 def test_AUTH_HTTP_200_OK_GET(self):131 132 for service in SERVICES:133 57 unmodified lines191 192 r = requests.get(service('status', '500'))193 self.assertEqual(bool(r), False)194 209- r = requests.get(service('/'))195+ r = requests.get(service('/get'))196 self.assertEqual(bool(r), True)197 198 199 def test_request_ok_set(self):42 unmodified lines242 def test_unicode_get(self):243 244 for service in SERVICES:245 260- url = service('/')246+ url = service('/get')247 248 requests.get(url, params={'foo': u'føø'})249 requests.get(url, params={u'føø': u'føø'})250 requests.get(url, params={'føø': 'føø'})9 unmodified lines260 r = requests.get(service('basic-auth', 'user', 'pass'), auth=http_auth)261 self.assertEquals(r.status_code, 401)262 263 278- def test_settings(self):279-280- def test():281- r = requests.get(httpbin(''))282- r.raise_for_status()283-284- with requests.settings(timeout=0.0000001):285- self.assertRaises(requests.Timeout, test)286-287- with requests.settings(timeout=100):288- requests.get(httpbin(''))289-290-264 def test_urlencoded_post_data(self):265 266 for service in SERVICES:267 90 unmodified lines358 self.assertEquals(rbody.get('form'), None)359 self.assertEquals(rbody.get('data'), 'foobar')360 361 389- def test_idna(self):390- r = requests.get(u'http://➡.ws/httpbin')391- assert 'httpbin' in r.url362+ # def test_idna(self):363+ # r = requests.get(u'http://➡.ws/httpbin')364+ # assert 'httpbin' in r.url365 366 367 def test_urlencoded_get_query_multivalued_param(self):368 73 unmodified lines442 443 def test_session_HTTP_200_OK_GET(self):444 445 s = Session()473- r = s.get(httpbin('/'))446+ r = s.get(httpbin('/get'))447 self.assertEqual(r.status_code, 200)448 449 477- def test_session_HTTPS_200_OK_GET(self):478-479- s = Session()480- r = s.get(httpsbin('/'))481- self.assertEqual(r.status_code, 200)482-483-450 def test_session_persistent_headers(self):451 452 heads = {'User-agent': 'Mozilla/5.0'}453 454 s = Session()455 s.headers = heads456+457 # Make 2 requests from Session object, should send header both times458 r1 = s.get(httpbin('user-agent'))492-459 assert heads['User-agent'] in r1.content494- r2 = s.get(httpbin('user-agent'))460 461+ r2 = s.get(httpbin('user-agent'))462 assert heads['User-agent'] in r2.content463+464+ new_heads = {'User-agent': 'blah'}465+ r3 = s.get(httpbin('user-agent'), headers=new_heads)466+ assert new_heads['User-agent'] in r3.content467+468 self.assertEqual(r2.status_code, 200)469 470 471 if __name__ == '__main__':1 unmodified line