Package restkit
[hide private]

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  from restkit.version import version_info, __version__ 
  7   
  8  try: 
  9      from restkit.conn import Connection 
 10      from restkit.errors import ResourceNotFound, Unauthorized, RequestFailed,\ 
 11  RedirectLimit, RequestError, InvalidUrl, ResponseError, ProxyError, \ 
 12  ResourceError, ResourceGone 
 13      from restkit.client import Client, MAX_FOLLOW_REDIRECTS 
 14      from restkit.wrappers import Request, Response, ClientResponse 
 15      from restkit.resource import Resource 
 16      from restkit.filters import BasicAuth, OAuthFilter 
 17  except ImportError: 
 18      import traceback 
 19      traceback.print_exc() 
 20   
 21  import urlparse 
 22  import logging 
 23   
 24  LOG_LEVELS = { 
 25      "critical": logging.CRITICAL, 
 26      "error": logging.ERROR, 
 27      "warning": logging.WARNING, 
 28      "info": logging.INFO, 
 29      "debug": logging.DEBUG 
 30  } 
 31   
32 -def set_logging(level, handler=None):
33 """ 34 Set level of logging, and choose where to display/save logs 35 (file or standard output). 36 """ 37 if not handler: 38 handler = logging.StreamHandler() 39 40 loglevel = LOG_LEVELS.get(level, logging.INFO) 41 logger = logging.getLogger('restkit') 42 logger.setLevel(loglevel) 43 format = r"%(asctime)s [%(process)d] [%(levelname)s] %(message)s" 44 datefmt = r"%Y-%m-%d %H:%M:%S" 45 46 handler.setFormatter(logging.Formatter(format, datefmt)) 47 logger.addHandler(handler)
48 49
50 -def request(url, 51 method='GET', 52 body=None, 53 headers=None, 54 **kwargs):
55 """ Quick shortcut method to pass a request 56 57 :param url: str, url string 58 :param method: str, by default GET. http verbs 59 :param body: the body, could be a string, an iterator or a file-like object 60 :param headers: dict or list of tupple, http headers 61 62 Client parameters 63 ~~~~~~~~~~~~~~~~~ 64 65 :param follow_redirect: follow redirection, by default False 66 :param max_ollow_redirect: number of redirections available 67 :filters: http filters to pass 68 :param decompress: allows the client to decompress the response 69 body 70 :param max_status_line_garbage: defines the maximum number of ignorable 71 lines before we expect a HTTP response's status line. With 72 HTTP/1.1 persistent connections, the problem arises that broken 73 scripts could return a wrong Content-Length (there are more 74 bytes sent than specified). Unfortunately, in some cases, this 75 cannot be detected after the bad response, but only before the 76 next one. So the client is abble to skip bad lines using this 77 limit. 0 disable garbage collection, None means unlimited number 78 of tries. 79 :param max_header_count: determines the maximum HTTP header count 80 allowed. by default no limit. 81 :param manager: the manager to use. By default we use the global 82 one. 83 :parama response_class: the response class to use 84 :param timeout: the default timeout of the connection 85 (SO_TIMEOUT) 86 87 :param max_tries: the number of tries before we give up a 88 connection 89 :param wait_tries: number of time we wait between each tries. 90 :param ssl_args: ssl named arguments, 91 See http://docs.python.org/library/ssl.html informations 92 """ 93 94 # detect credentials from url 95 u = urlparse.urlparse(url) 96 if u.username is not None: 97 password = u.password or "" 98 filters = kwargs.get('filters') or [] 99 url = urlparse.urlunparse((u.scheme, u.netloc.split("@")[-1], 100 u.path, u.params, u.query, u.fragment)) 101 filters.append(BasicAuth(u.username, password)) 102 103 kwargs['filters'] = filters 104 105 http_client = Client(**kwargs) 106 return http_client.request(url, method=method, body=body, 107 headers=headers)
108