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, 2, 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 -def request(url, method='GET', body=None, headers=None, pool_instance=None, 52 follow_redirect=False, filters=None, key_file=None, cert_file=None):
53 """ Quick shortcut method to pass a request 54 55 :param url: str, url string 56 :param method: str, by default GET. http verbs 57 :param body: the body, could be a string, an iterator or a file-like object 58 :param headers: dict or list of tupple, http headers 59 :pool intance: instance inherited from `restkit.pool.PoolInterface`. 60 It allows you to share and reuse connections connections. 61 :param follow_redirect: boolean, by default is false. If true, 62 if the HTTP status is 301, 302 or 303 the client will follow 63 the location. 64 :param filters: list, list of http filters. see the doc of http filters 65 for more info 66 :param key_file: the key fle to use with ssl 67 :param cert_file: the cert file to use with ssl 68 69 """ 70 # detect credentials from url 71 u = urlparse.urlparse(url) 72 if u.username is not None: 73 password = u.password or "" 74 filters = filters or [] 75 url = urlparse.urlunparse((u.scheme, u.netloc.split("@")[-1], 76 u.path, u.params, u.query, u.fragment)) 77 filters.append(BasicAuth(u.username, password)) 78 79 http_client = HttpConnection(follow_redirect=follow_redirect, 80 filters=filters, key_file=key_file, cert_file=cert_file, 81 pool_instance=pool_instance) 82 return http_client.request(url, method=method, body=body, 83 headers=headers)
84