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, 1) 
 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 -def set_logging(level, handler=None):
35 """ 36 Set level of logging, and choose where to display/save logs 37 (file or standard output). 38 """ 39 if not handler: 40 handler = logging.StreamHandler() 41 42 loglevel = LOG_LEVELS.get(level, logging.INFO) 43 logger = logging.getLogger('restkit') 44 logger.setLevel(loglevel) 45 format = r"%(asctime)s [%(process)d] [%(levelname)s] %(message)s" 46 datefmt = r"%Y-%m-%d %H:%M:%S" 47 48 handler.setFormatter(logging.Formatter(format, datefmt)) 49 logger.addHandler(handler)
50 51
52 -def request(url, method='GET', body=None, headers=None, pool_instance=None, 53 follow_redirect=False, filters=None, key_file=None, cert_file=None):
54 """ Quick shortcut method to pass a request 55 56 :param url: str, url string 57 :param method: str, by default GET. http verbs 58 :param body: the body, could be a string, an iterator or a file-like object 59 :param headers: dict or list of tupple, http headers 60 :pool intance: instance inherited from `restkit.pool.PoolInterface`. 61 It allows you to share and reuse connections connections. 62 :param follow_redirect: boolean, by default is false. If true, 63 if the HTTP status is 301, 302 or 303 the client will follow 64 the location. 65 :param filters: list, list of http filters. see the doc of http filters 66 for more info 67 :param key_file: the key fle to use with ssl 68 :param cert_file: the cert file to use with ssl 69 70 """ 71 # detect credentials from url 72 u = urlparse.urlparse(url) 73 if u.username is not None: 74 password = u.password or "" 75 filters = filters or [] 76 url = urlparse.urlunparse((u.scheme, u.netloc.split("@")[-1], 77 u.path, u.params, u.query, u.fragment)) 78 filters.append(BasicAuth(u.username, password)) 79 80 http_client = HttpConnection(follow_redirect=follow_redirect, 81 filters=filters, key_file=key_file, cert_file=cert_file, 82 pool_instance=pool_instance) 83 return http_client.request(url, method=method, body=body, 84 headers=headers)
85