Skip to content
Myr

Added request.session functionality

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Hogans · committed by Tom Hogans · parent 49e5df1 · Browse files · 2 files+46 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+46 added−1 removed

  • other:+46 added−1 removed

2 files+46 added−1 removed

added:requests/session.py+44 added
1# -*- coding: utf-8 -*-23"""4requests.session5~~~~~~~~~~~~~~~67This module provides a Session object to manage and persist settings across8requests (cookies, auth, proxies).910"""1112import requests.api13import cookielib1415class Session(object):16 __attrs__ = ['headers', 'cookies', 'auth', 'timeout', 'proxies']1718 def __init__(self, **kwargs):19 # Set up a CookieJar to be used by default20 self.cookies = cookielib.FileCookieJar()21 # Map args from kwargs to instance-local variables22 map(lambda k, v: (k in self.__attrs__) and setattr(self, k, v),23 kwargs.iterkeys(), kwargs.itervalues())24 # Map and wrap requests.api methods25 self._map_api_methods()26 27 def _map_api_methods(self):28 """ Reads each available method from requests.api and decorates29 them with a wrapper that inserts any instance-local attributes30 (from __attrs__) that have been set, combining them with **kwargs """31 def pass_args(func):32 def wrapper_func(*args, **kwargs):33 inst_attrs = dict((k, v) for k, v in self.__dict__.iteritems() 34 if k in self.__attrs__)35 # Combine instance-local values with kwargs values, with 36 # priority to values in kwargs37 kwargs = dict(inst_attrs.items() + kwargs.items())38 return func(*args, **kwargs)39 return wrapper_func40 # Map and decorate each function available in requests.api41 map(lambda fn: setattr(self, fn, pass_args(getattr(requests.api, fn))),42 requests.api.__all__) 4344
modified:AUTHORS+2 added-1 removed
29 unmodified lines
30- 潘旭 (Xu Pan)31- Tamás Gulácsi32- Rubén Abad33- Peter Manser34- Jeremy Selie34- Jeremy Selie35- Tom Hogans <tomhsx@gmail.com>