Browse code
Added headers to JSONRPCProxy
Timo Vanwynsberghe authored on 16/02/2012 19:53:03
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -43,6 +43,7 @@ collections.Mapping.register(UserDict.DictMixin) |
43 | 43 |
|
44 | 44 |
from hashlib import sha1 |
45 | 45 |
import jsonrpc.jsonutil |
46 |
+from jsonrpc import __version__ |
|
46 | 47 |
from jsonrpc.common import Response, Request |
47 | 48 |
|
48 | 49 |
__all__ = ['JSONRPCProxy', 'ProxyEvents'] |
... | ... |
@@ -90,6 +91,17 @@ class ProxyEvents(object): |
90 | 91 |
'''allow a subclass to access the response data before it is returned to the user''' |
91 | 92 |
return data |
92 | 93 |
|
94 |
+ |
|
95 |
+class JSONRPCProcessor(urllib2.BaseHandler): |
|
96 |
+ def __init__(self): |
|
97 |
+ self.handler_order = 100 |
|
98 |
+ |
|
99 |
+ def http_request(self, request): |
|
100 |
+ request.add_header('content-type', 'application/json') |
|
101 |
+ request.add_header('user-agent', 'jsonrpc/'+__version__) |
|
102 |
+ return request |
|
103 |
+ |
|
104 |
+ |
|
93 | 105 |
class JSONRPCProxy(object): |
94 | 106 |
'''A class implementing a JSON-RPC Proxy. |
95 | 107 |
|
... | ... |
@@ -139,7 +151,7 @@ class JSONRPCProxy(object): |
139 | 151 |
|
140 | 152 |
cj = cookielib.CookieJar() |
141 | 153 |
self._opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) |
142 |
- |
|
154 |
+ self._opener.add_handler(JSONRPCProcessor()) |
|
143 | 155 |
|
144 | 156 |
|
145 | 157 |
def _set_opener(self, opener): |