1
2
3
4
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
33
36
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
44 for i, f in enumerate(self.filters):
45 if obj == f: del self.filters[i]
46
47 - def apply(self, kind, *args):
48 for f in self.filters:
49 try:
50 func = getattr(f, kind)
51 func(*args)
52 except AttributeError:
53 continue
54