Package restkit :: Package filters
[hide private]
[frames] | no frames]

Source Code for Package restkit.filters

 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  filters - Http filters 
 8   
 9  Http filters are object used before sending the request to the server 
10  and after. The `HttpClient` instance is passed as argument. 
11   
12  An object with a method `on_request` is called just before the request.  
13  An object with a method `on_response` is called after fetching response headers. 
14   
15  ex:: 
16   
17      class MyFilter(object): 
18           
19          def on_request(self, http_client): 
20              "do something with/to http_client instance" 
21   
22          def on_response(self, http_client): 
23              "do something on http_client and get response infos" 
24               
25               
26  """ 
27   
28  from restkit.filters.basicauth import BasicAuth 
29  from restkit.filters.oauth2 import OAuthFilter 
30  from restkit.filters.simpleproxy import SimpleProxy 
31   
32 -class Filters(object):
33
34 - def __init__(self, filters=None):
35 self.filters = filters or []
36
37 - def add(self, obj):
38 if not hasattr(obj, "on_request") and not hasattr(obj, "on_response"): 39 raise TypeError("%s is not a filter object." % obj.__class__.__name__) 40 41 self.filters.append(obj)
42
43 - def remove(self, obj):
44 for i, f in enumerate(self.filters): 45 if obj == f: del self.filters[i]
46
47 - def apply(self, kind, client):
48 for f in self.filters: 49 try: 50 func = getattr(f, kind) 51 func(client) 52 except AttributeError: 53 continue
54