Package restkit :: Package contrib :: Module webob_api
[hide private]
[frames] | no frames]

Source Code for Module restkit.contrib.webob_api

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 - 
 3  # 
 4  # This file is part of restkit released under the MIT license.  
 5  # See the NOTICE for more information. 
 6   
 7  try: 
 8      from webob import Request as BaseRequest 
 9  except ImportError: 
10      raise ImportError('WebOb (http://pypi.python.org/pypi/WebOb) is required') 
11  from StringIO import StringIO 
12  from restkit.contrib.wsgi_proxy import Proxy 
13  import urlparse 
14  import urllib 
15   
16  __doc__ = '''Subclasses of webob.Request who use restkit to get a 
17  webob.Response via restkit.ext.wsgi_proxy.Proxy. 
18   
19  Example:: 
20   
21      >>> req = Request.blank('http://pypi.python.org/pypi/restkit') 
22      >>> resp = req.get_response() 
23      >>> print resp #doctest: +ELLIPSIS 
24      200 OK 
25      Date: ... 
26      Transfer-Encoding: chunked 
27      Content-Type: text/html; charset=utf-8 
28      Server: Apache/2... 
29      <BLANKLINE> 
30      <?xml version="1.0" encoding="UTF-8"?> 
31      ... 
32       
33   
34  ''' 
35   
36  PROXY = Proxy(allowed_methods=['GET', 'POST', 'HEAD', 'DELETE', 'PUT', 'PURGE']) 
37   
38 -class Method(property):
39 - def __init__(self, name):
40 self.name = name
41 - def __get__(self, instance, klass):
42 if not instance: 43 return self 44 instance.method = self.name.upper() 45 def req(*args, **kwargs): 46 return instance.get_response(*args, **kwargs)
47 return req
48 49
50 -class Request(BaseRequest):
51 get = Method('get') 52 post = Method('post') 53 put = Method('put') 54 head = Method('head') 55 delete = Method('delete')
56 - def get_response(self):
57 if self.content_length < 0: 58 self.content_length = 0 59 if self.method in ('DELETE', 'GET'): 60 self.body = '' 61 elif self.method == 'POST' and self.POST: 62 body = urllib.urlencode(self.POST.copy()) 63 stream = StringIO(body) 64 stream.seek(0) 65 self.body_file = stream 66 self.content_length = stream.len 67 if 'form' not in self.content_type: 68 self.content_type = 'application/x-www-form-urlencoded' 69 self.server_name = self.host 70 return BaseRequest.get_response(self, PROXY)
71 72 __call__ = get_response 73
74 - def set_url(self, url_or_path):
75 path = url_or_path.lstrip('/') 76 if '?' in path: 77 path, self.query_string = path.split('?', 1) 78 if path.startswith('http'): 79 url = path 80 else: 81 self.path_info = '/'+path 82 url = self.url 83 self.scheme, self.host, self.path_info = urlparse.urlparse(url)[0:3]
84