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 = (1, 1, 2) 
 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 
15      from restkit.resource import Resource 
16      from restkit.pool import ConnectionPool 
17      from restkit.filters import BasicAuth, SimpleProxy 
18      from restkit.oauth2.filter import OAuthFilter 
19  except ImportError: 
20      import traceback 
21      traceback.print_exc() 
22   
23  import urlparse 
24  import logging     
25   
26  LOG_LEVELS = { 
27      "critical": logging.CRITICAL, 
28      "error": logging.ERROR, 
29      "warning": logging.WARNING, 
30      "info": logging.INFO, 
31      "debug": logging.DEBUG 
32  } 
33   
34 -class NullHandler(logging.Handler):
35 - def emit(self, record):
36 pass
37 38 h = NullHandler() 39 logging.getLogger('restkit').addHandler(h) 40
41 -def set_logging(level, handler=None):
42 """ 43 Set level of logging, and choose where to display/save logs 44 (file or standard output). 45 """ 46 if not handler: 47 handler = logging.StreamHandler() 48 49 loglevel = LOG_LEVELS.get(level, logging.INFO) 50 logger = logging.getLogger('restkit') 51 logger.setLevel(loglevel) 52 format = r"%(asctime)s [%(process)d] [%(levelname)s] %(message)s" 53 datefmt = r"%Y-%m-%d %H:%M:%S" 54 55 handler.setFormatter(logging.Formatter(format, datefmt)) 56 logger.addHandler(handler)
57 58
59 -def request(url, method='GET', body=None, headers=None, pool_instance=None, 60 follow_redirect=False, filters=None, key_file=None, cert_file=None):
61 """ Quick shortcut method to pass a request 62 63 :param url: str, url string 64 :param method: str, by default GET. http verbs 65 :param body: the body, could be a string, an iterator or a file-like object 66 :param headers: dict or list of tupple, http headers 67 :pool intance: instance inherited from `restkit.pool.PoolInterface`. 68 It allows you to share and reuse connections connections. 69 :param follow_redirect: boolean, by default is false. If true, 70 if the HTTP status is 301, 302 or 303 the client will follow 71 the location. 72 :param filters: list, list of http filters. see the doc of http filters 73 for more info 74 :param key_file: the key fle to use with ssl 75 :param cert_file: the cert file to use with ssl 76 77 """ 78 # detect credentials from url 79 u = urlparse.urlparse(url) 80 if u.username is not None: 81 password = u.password or "" 82 filters = filters or [] 83 url = urlparse.urlunparse((u.scheme, u.netloc.split("@")[-1], 84 u.path, u.params, u.query, u.fragment)) 85 filters.append(BasicAuth(u.username, password)) 86 87 http_client = HttpConnection(follow_redirect=follow_redirect, 88 filters=filters, key_file=key_file, cert_file=cert_file, 89 pool_instance=pool_instance) 90 return http_client.request(url, method=method, body=body, 91 headers=headers)
92