Package restkit :: Package client :: Module response
[hide private]
[frames] | no frames]

Source Code for Module restkit.client.response

 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.errors import AlreadyRead 
 7   
8 -class HttpResponse(object):
9 """ Http Response object returned by HttpConnction""" 10 11 charset = "utf8" 12 unicode_errors = 'strict' 13
14 - def __init__(self, response, final_url):
15 self.response = response 16 self.status = response.status 17 self.status_int = response.status_int 18 self.version = response.version 19 self.headerslist = response.headers 20 self.final_url = final_url 21 22 headers = {} 23 for key, value in response.headers: 24 headers[key.lower()] = value 25 self.headers = headers 26 self.closed = False
27
28 - def __getitem__(self, key):
29 try: 30 return getattr(self, key) 31 except AttributeError: 32 pass 33 return self.headers[key.lower()]
34
35 - def __contains__(self, key):
36 return (key.lower() in self.headers)
37
38 - def __iter__(self):
39 for item in list(self.headers.items()): 40 yield item
41
42 - def body_string(self, charset=None, unicode_errors="strict"):
43 """ return body string, by default in bytestring """ 44 if self.closed or self.response.body.closed: 45 raise AlreadyRead("The response have already been read") 46 body = self.response.body.read() 47 if charset is not None: 48 try: 49 body = body.decode(charset, unicode_errors) 50 except UnicodeDecodeError: 51 pass 52 self.close() 53 return body
54
55 - def body_stream(self):
56 """ return full body stream """ 57 if self.closed or self.response.body.closed: 58 raise AlreadyRead("The response have already been read") 59 return self.response.body
60
61 - def close(self):
62 """ release the socket """ 63 self.closed = True 64 self.response.body.close()
65