really coming together
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 366e8e8 · Browse files · 2 files·+92 added−45 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+92 added−45 removed
- other:+92 added−45 removed
Provenance
- Commit
31d10c0- Tree
636f342- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
366e8e8
2 files changed+92 added−45 removed
modified:requests/models.py+87 added-44 removed
143 unmodified lines144 def __repr__(self):145 return '<Request [%s]>' % (self.method)146 147 def prepare(self):148+ """Constructs a PreparedRequest and returns it."""149 p = PreparedRequest()150 p.prepare_method(self.method)150- p.prepare_url(self.url)151+ p.prepare_url(self.url, self.params)152+ p.prepare_headers(self.headers)153+ p.prepare_auth(self.auth)154+ p.prepare_body(self.data, self.files)155 156 157 return p158 6 unmodified lines165 def __init__(self):166 self.method = None167 self.url = None168 self.headers = None165- self.data = None169+ self.body = None170 self.params = None171 self.auth = None172 # self.cookies = None173 self.timeout = None8 unmodified lines182 def __repr__(self):183 return '<PreparedRequest [%s]>' % (self.method)184 185 def prepare_method(self, method):182-186+ """Prepares the given HTTP method."""187 try:188 method = unicode(method)189 except NameError:190 # We're on Python 3.191 method = str(method)192 189-193 self.method = method.upper()194 192- def prepare_url(self, url):193- """Request URL."""195+ def prepare_url(self, url, params):196+ """Prepares the given HTTP URL."""197 #: Accept objects that have string representations.198 try:199 url = unicode(url)200 except NameError:32 unmodified lines233 query = query.encode('utf-8')234 if isinstance(fragment, str):235 fragment = fragment.encode('utf-8')236 234- enc_params = self._encode_params(self.params)237+ enc_params = self._encode_params(params)238 if enc_params:239 if query:240 query = '%s&%s' % (query, enc_params)241 else:2 unmodified lines244 url = (urlunparse([scheme, netloc, path, params, query, fragment]))245 246 # if self.config.get('encode_uri', True):247 # url = requote_uri(url)248+ # TODO: re-evaluate quote param (perhaps not, people can create this themselves now)249+ url = requote_uri(url)250 251 self.url = url252 253 def prepare_headers(self, headers):249- """change to list"""254+ """Prepares the given HTTP headers."""255 256 if headers:257 self.headers = CaseInsensitiveDict(self.headers)258 else:259 self.headers = CaseInsensitiveDict()260 256- def prepare_data(self, headers, files):261+ def prepare_body(self, data, files):262+ """Prepares the given HTTP body data."""263 # if a generator is provided, error out.258- pass264 260- def prepare_params(self, params):261- pass265+ # Nottin' on you.266+ body = None267+ content_type = None268 269+ # Multi-part file uploads.270+ if files:271+ (body, content_type) = self._encode_files(files)272+ else:273+ if data:274+275+ body = self._encode_params(data)276+ if isinstance(data, str) or isinstance(data, builtin_str) or hasattr(data, 'read'):277+ content_type = None278+ else:279+ content_type = 'application/x-www-form-urlencoded'280+281+ self.headers['Content-Length'] = '0'282+ if hasattr(body, 'seek') and hasattr(body, 'tell'):283+ body.seek(0, 2)284+ self.headers['Content-Length'] = str(body.tell())285+ body.seek(0, 0)286+ elif body is not None:287+ self.headers['Content-Length'] = str(len(body))288+289+ # Add content-type if it wasn't explicitly provided.290+ if (content_type) and (not 'content-type' in self.headers):291+ self.headers['Content-Type'] = content_type292+293+ self.body = body294+295+296+297 def prepare_auth(self, auth):264- pass298+ if auth:299+ if isinstance(auth, tuple) and len(auth) == 2:300+ # special-case basic HTTP auth301+ auth = HTTPBasicAuth(*auth)302 303+ # Allow auth to make its changes.304+ r = auth(self)305+306+ # Update self to reflect the auth changes.307+ self.__dict__.update(r.__dict__)308+309 def prepare_cookies(self, cookies):310 #: CookieJar to attach to :class:`Request <Request>`.311 if isinstance(cookies, cookielib.CookieJar):312 self.cookies = cookies441 unmodified lines754 # Use .netrc auth if none was provided.755 if not self.auth and self.config.get('trust_env'):756 self.auth = get_netrc_auth(url)757 715- if self.auth:716- if isinstance(self.auth, tuple) and len(self.auth) == 2:717- # special-case basic HTTP auth718- self.auth = HTTPBasicAuth(*self.auth)758+ # if self.auth:759+ # if isinstance(self.auth, tuple) and len(self.auth) == 2:760+ # # special-case basic HTTP auth761+ # self.auth = HTTPBasicAuth(*self.auth)762 720- # Allow auth to make its changes.721- r = self.auth(self)763+ # # Allow auth to make its changes.764+ # r = self.auth(self)765 723- # Update self to reflect the auth changes.724- self.__dict__.update(r.__dict__)766+ # # Update self to reflect the auth changes.767+ # self.__dict__.update(r.__dict__)768 726- # Nottin' on you.727- body = None728- content_type = None769+ # # Nottin' on you.770+ # body = None771+ # content_type = None772 730- # Multi-part file uploads.731- if self.files:732- (body, content_type) = self._encode_files(self.files)733- else:734- if self.data:773+ # # Multi-part file uploads.774+ # if self.files:775+ # (body, content_type) = self._encode_files(self.files)776+ # else:777+ # if self.data:778 736- body = self._encode_params(self.data)737- if isinstance(self.data, str) or isinstance(self.data, builtin_str) or hasattr(self.data, 'read'):738- content_type = None739- else:740- content_type = 'application/x-www-form-urlencoded'779+ # body = self._encode_params(self.data)780+ # if isinstance(self.data, str) or isinstance(self.data, builtin_str) or hasattr(self.data, 'read'):781+ # content_type = None782+ # else:783+ # content_type = 'application/x-www-form-urlencoded'784 742- self.headers['Content-Length'] = '0'743- if hasattr(body, 'seek') and hasattr(body, 'tell'):744- body.seek(0, 2)745- self.headers['Content-Length'] = str(body.tell())746- body.seek(0, 0)747- elif body is not None:748- self.headers['Content-Length'] = str(len(body))785+ # self.headers['Content-Length'] = '0'786+ # if hasattr(body, 'seek') and hasattr(body, 'tell'):787+ # body.seek(0, 2)788+ # self.headers['Content-Length'] = str(body.tell())789+ # body.seek(0, 0)790+ # elif body is not None:791+ # self.headers['Content-Length'] = str(len(body))792 750- # Add content-type if it wasn't explicitly provided.751- if (content_type) and (not 'content-type' in self.headers):752- self.headers['Content-Type'] = content_type793+ # # Add content-type if it wasn't explicitly provided.794+ # if (content_type) and (not 'content-type' in self.headers):795+ # self.headers['Content-Type'] = content_type796 797 _p = urlparse(url)798 no_proxy = filter(lambda x: x.strip(), self.proxies.get('no', '').split(','))799 proxy = self.proxies.get(_p.scheme)368 unmodified linesmodified:test_iteration.py+5 added-1 removed
16 unmodified lines17 s = requests.Session()18 r = requests.Request(method='GET', url='http://github.com/')19 r = r.prepare()20 21- s.send(r)21+ r = s.send(r)22+ print r23 24 25+26+27+28 if __name__ == '__main__':29 unittest.main()