Skip to content
Myr

api.py

This file

Path

requests/api.py

151 lines · 4.5 KiB · Python

Last changed by

cleanupKenneth Reitzbefore adoption35fc3b4

History of this file

Take it

1# -*- coding: utf-8 -*-23"""4requests.api5~~~~~~~~~~~~67This module impliments the Requests API.89:copyright: (c) 2011 by Kenneth Reitz.10:license: ISC, see LICENSE for more details.1112"""1314import config15from .models import Request, Response, AuthObject16from .status_codes import codes17from .hooks import dispatch_hook18from .utils import cookiejar_from_dict, header_expand1920from urlparse import urlparse2122__all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')2324def request(method, url,25    params=None, data=None, headers=None, cookies=None, files=None, auth=None,26    timeout=None, allow_redirects=False, proxies=None, hooks=None):2728    """Constructs and sends a :class:`Request <Request>`.29    Returns :class:`Response <Response>` object.3031    :param method: method for the new :class:`Request` object.32    :param url: URL for the new :class:`Request` object.33    :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.34    :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.35    :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.36    :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.37    :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.38    :param auth: (optional) AuthObject to enable Basic HTTP Auth.39    :param timeout: (optional) Float describing the timeout of the request.40    :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.41    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.42    """4344    method = str(method).upper()4546    if cookies is None:47        cookies = {}4849    cookies = cookiejar_from_dict(cookies)5051    # Expand header values52    if headers:53        for k, v in headers.items() or {}:54            headers[k] = header_expand(v)5556    args = dict(57        method = method,58        url = url,59        data = data,60        params = params,61        headers = headers,62        cookiejar = cookies,63        files = files,64        auth = auth,65        timeout = timeout or config.settings.timeout,66        allow_redirects = allow_redirects,67        proxies = proxies or config.settings.proxies,68    )6970    # Arguments manipulation hook.71    args = dispatch_hook('args', hooks, args)7273    r = Request(**args)7475    # Pre-request hook.76    r = dispatch_hook('pre_request', hooks, r)7778    # Send the HTTP Request.79    r.send()8081    # Post-request hook.82    r = dispatch_hook('post_request', hooks, r)8384    # Response manipulation hook.85    r.response = dispatch_hook('response', hooks, r.response)8687    return r.response888990def get(url, **kwargs):9192    """Sends a GET request. Returns :class:`Response` object.9394    :param url: URL for the new :class:`Request` object.95    :param **kwargs: Optional arguments that ``request`` takes.96    """9798    return request('get', url, **kwargs)99100101def head(url, **kwargs):102    """Sends a HEAD request. Returns :class:`Response` object.103104    :param url: URL for the new :class:`Request` object.105    :param **kwargs: Optional arguments that ``request`` takes.106    """107108    return request('head', url, **kwargs)109110111def post(url, data='', **kwargs):112    """Sends a POST request. Returns :class:`Response` object.113114    :param url: URL for the new :class:`Request` object.115    :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.116    :param **kwargs: Optional arguments that ``request`` takes.117    """118119    return request('post', url, data=data, **kwargs)120121122def put(url, data='', **kwargs):123    """Sends a PUT request. Returns :class:`Response` object.124125    :param url: URL for the new :class:`Request` object.126    :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.127    :param **kwargs: Optional arguments that ``request`` takes.128    """129130    return request('put', url, data=data, **kwargs)131132133def patch(url, data='', **kwargs):134    """Sends a PATCH request. Returns :class:`Response` object.135136    :param url: URL for the new :class:`Request` object.137    :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.138    :param **kwargs: Optional arguments that ``request`` takes.139    """140141    return request('patch', url, **kwargs)142143144def delete(url, **kwargs):145    """Sends a DELETE request. Returns :class:`Response` object.146147    :param url: URL for the new :class:`Request` object.148    :param **kwargs: Optional arguments that ``request`` takes.149    """150151    return request('delete', url, **kwargs)