Package restkit
[hide private]
[frames] | no frames]

Source Code for Package restkit

 1  # -*- coding: utf-8 - 
 2  # 
 3  # This file is part of restkit released under the MIT license.  
 4  # See the NOTICE for more information. 
 5   
 6   
 7   
 8  version_info = (3, 0, 0) 
 9  __version__ =  ".".join(map(str, version_info)) 
10   
11  try: 
12      from .errors import ResourceNotFound, Unauthorized, RequestFailed,\ 
13  RedirectLimit, RequestError, InvalidUrl, ResponseError, ProxyError, ResourceError 
14      from .client import Client, ClientResponse, MAX_FOLLOW_REDIRECTS 
15      from .resource import Resource 
16      from .manager import Manager  
17      from .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   
33 -def set_logging(level, handler=None):
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
51 -def request(url, 52 method='GET', 53 body=None, 54 headers=None, 55 **kwargs):
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 # detect credentials from url 74 u = urlparse.urlparse(url) 75 if u.username is not None: 76 password = u.password or "" 77 filters = kwargs.get('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 kwargs['filters'] = filters 83 84 http_client = Client(**kwargs) 85 return http_client.request(url, method=method, body=body, 86 headers=headers)
87