1
2
3
4
5
6
7
8 version_info = (2, 1, 4)
9 __version__ = ".".join(map(str, version_info))
10
11 try:
12 from restkit.errors import ResourceNotFound, Unauthorized, RequestFailed,\
13 RedirectLimit, RequestError, InvalidUrl, ResponseError, ProxyError, ResourceError
14 from restkit.client import HttpConnection, HttpResponse, MAX_FOLLOW_REDIRECTS
15 from restkit.resource import Resource
16 from restkit.pool.simple import SimplePool
17 from restkit.filters import BasicAuth, SimpleProxy, OAuthFilter
18 except ImportError:
19 import traceback
20 traceback.print_exc()
21
22 import urlparse
23 import logging
24
25 LOG_LEVELS = {
26 "critical": logging.CRITICAL,
27 "error": logging.ERROR,
28 "warning": logging.WARNING,
29 "info": logging.INFO,
30 "debug": logging.DEBUG
31 }
32
34 """
35 Set level of logging, and choose where to display/save logs
36 (file or standard output).
37 """
38 if not handler:
39 handler = logging.StreamHandler()
40
41 loglevel = LOG_LEVELS.get(level, logging.INFO)
42 logger = logging.getLogger('restkit')
43 logger.setLevel(loglevel)
44 format = r"%(asctime)s [%(process)d] [%(levelname)s] %(message)s"
45 datefmt = r"%Y-%m-%d %H:%M:%S"
46
47 handler.setFormatter(logging.Formatter(format, datefmt))
48 logger.addHandler(handler)
49
50 from restkit.util.sock import _GLOBAL_DEFAULT_TIMEOUT
51
52 -def request(url, method='GET', body=None, headers=None,
53 timeout=_GLOBAL_DEFAULT_TIMEOUT, filters=None, follow_redirect=False,
54 force_follow_redirect=False, max_follow_redirect=MAX_FOLLOW_REDIRECTS,
55 pool_instance=None, response_class=None, **ssl_args):
56 """ Quick shortcut method to pass a request
57
58 :param url: str, url string
59 :param method: str, by default GET. http verbs
60 :param body: the body, could be a string, an iterator or a file-like object
61 :param headers: dict or list of tupple, http headers
62 :pool intance: instance inherited from `restkit.pool.PoolInterface`.
63 It allows you to share and reuse connections connections.
64 :param follow_redirect: boolean, by default is false. If true,
65 if the HTTP status is 301, 302 or 303 the client will follow
66 the location.
67 :param filters: list, list of http filters. see the doc of http filters
68 for more info
69 :param ssl_args: ssl arguments. See http://docs.python.org/library/ssl.html
70 for more information.
71
72 """
73
74 u = urlparse.urlparse(url)
75 if u.username is not None:
76 password = u.password or ""
77 filters = filters or []
78 url = urlparse.urlunparse((u.scheme, u.netloc.split("@")[-1],
79 u.path, u.params, u.query, u.fragment))
80 filters.append(BasicAuth(u.username, password))
81
82 http_client = HttpConnection(
83 timeout=timeout,
84 filters=filters,
85 follow_redirect=follow_redirect,
86 force_follow_redirect=force_follow_redirect,
87 max_follow_redirect=max_follow_redirect,
88 pool_instance=pool_instance,
89 response_class=response_class,
90 **ssl_args)
91 return http_client.request(url, method=method, body=body,
92 headers=headers)
93