api.py
This file
Path
requests/api.py
143 lines · 4.4 KiB · Python
Last changed by
explicitKenneth Reitzbefore adoption6730818
History of this fileTake it
1# -*- coding: utf-8 -*-23"""4requests.api5~~~~~~~~~~~~67This module implements the Requests API.89:copyright: (c) 2011 by Kenneth Reitz.10:license: ISC, see LICENSE for more details.1112"""1314from .sessions import session151617def request(method, url,18 params=None,19 data=None,20 headers=None,21 cookies=None,22 files=None,23 auth=None,24 timeout=None,25 allow_redirects=False,26 proxies=None,27 hooks=None,28 return_response=True,29 prefetch=False,30 config=None):31 """Constructs and sends a :class:`Request <Request>`.32 Returns :class:`Response <Response>` object.3334 :param method: method for the new :class:`Request` object.35 :param url: URL for the new :class:`Request` object.36 :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.37 :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.38 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.39 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.40 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.41 :param auth: (optional) Auth typle to enable Basic/Digest/Custom HTTP Auth.42 :param timeout: (optional) Float describing the timeout of the request.43 :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.44 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.45 :param return_response: (optional) If False, an un-sent Request object will returned.46 :param config: (optional) A configuration dictionary.47 """4849 s = session()50 return s.request(51 method=method,52 url=url,53 params=params,54 data=data,55 headers=headers,56 cookies=cookies,57 files=files,58 auth=auth,59 timeout=timeout,60 allow_redirects=allow_redirects,61 proxies=proxies,62 hooks=hooks,63 return_response=return_response,64 config=config,65 prefetch=prefetch66 )67686970def get(url, **kwargs):71 """Sends a GET request. Returns :class:`Response` object.7273 :param url: URL for the new :class:`Request` object.74 :param **kwargs: Optional arguments that ``request`` takes.75 """7677 kwargs.setdefault('allow_redirects', True)78 return request('get', url, **kwargs)798081def options(url, **kwargs):82 """Sends a OPTIONS request. Returns :class:`Response` object.8384 :param url: URL for the new :class:`Request` object.85 :param **kwargs: Optional arguments that ``request`` takes.86 """8788 kwargs.setdefault('allow_redirects', True)89 return request('options', url, **kwargs)909192def head(url, **kwargs):93 """Sends a HEAD request. Returns :class:`Response` object.9495 :param url: URL for the new :class:`Request` object.96 :param **kwargs: Optional arguments that ``request`` takes.97 """9899 kwargs.setdefault('allow_redirects', True)100 return request('head', url, **kwargs)101102103def post(url, data=None, **kwargs):104 """Sends a POST request. Returns :class:`Response` object.105106 :param url: URL for the new :class:`Request` object.107 :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.108 :param **kwargs: Optional arguments that ``request`` takes.109 """110111 return request('post', url, data=data, **kwargs)112113114def put(url, data=None, **kwargs):115 """Sends a PUT request. Returns :class:`Response` object.116117 :param url: URL for the new :class:`Request` object.118 :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.119 :param **kwargs: Optional arguments that ``request`` takes.120 """121122 return request('put', url, data=data, **kwargs)123124125def patch(url, data=None, **kwargs):126 """Sends a PATCH request. Returns :class:`Response` object.127128 :param url: URL for the new :class:`Request` object.129 :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.130 :param **kwargs: Optional arguments that ``request`` takes.131 """132133 return request('patch', url, data=data, **kwargs)134135136def delete(url, **kwargs):137 """Sends a DELETE request. Returns :class:`Response` object.138139 :param url: URL for the new :class:`Request` object.140 :param **kwargs: Optional arguments that ``request`` takes.141 """142143 return request('delete', url, **kwargs)