def _prepare_transport(self): update = False if not self._authclient: self._authclient = BasicAuthClient() update = True if not self._authclient.hasRealm("OpenERP User"): self._authclient.addLogin("OpenERP User", self._session.auth_proxy.user, self._session.auth_proxy.passwd) update = True if not self._authclient.hasRealm("OpenERP Admin") \ and self._session.auth_proxy.superpass: self._authclient.addLogin("OpenERP Admin", 'root', self._session.auth_proxy.superpass) if update: self._transport.setAuthClient(self._authclient)
class RpcJsonConnection(TCPConnection): """ JSON-based RPC connection class Usually opened at port 8069 on the server. Unlike the XML-RPC connection classes, this doesn't need to keep proxy objects, or "gateways" to the server. Instead, it will simple issue HTTP requests for every RPC call needed, over a persistent Transport instance. With the same TCP connection, same authentication (negotiation), it can work over different url paths (end-objects). """ name = "RPC-JSON" codename = 'http' _TransportClass = JSONAuthTransport def __init__(self, session): super(RpcJsonConnection, self).__init__(session) self._transport = None self._authclient = None self._req_counter = 1 def _prepare_transport(self): update = False if not self._authclient: self._authclient = BasicAuthClient() update = True if not self._authclient.hasRealm("OpenERP User"): self._authclient.addLogin("OpenERP User", self._session.auth_proxy.user, self._session.auth_proxy.passwd) update = True if not self._authclient.hasRealm("OpenERP Admin") \ and self._session.auth_proxy.superpass: self._authclient.addLogin("OpenERP Admin", 'root', self._session.auth_proxy.superpass) if update: self._transport.setAuthClient(self._authclient) def establish(self, kwargs, do_init=False): if 'host' in kwargs: self.host = kwargs['host'] if 'port' in kwargs: self.port = kwargs['port'] send_gzip = True self._transport = self._TransportClass(send_gzip=send_gzip) uri = '%s:%s' % (self.host, self.port) self._prepare_transport() self._transport.make_connection(uri) if do_init: self._establish_int() return True def _do_request(self, path, method, args, kwargs=None): """ Request, including the marshalling of parameters Also decode possible Exceptions to the appropriate RpcException classes @param path a list of path components """ try: url = '/json/' + '/'.join(map(str, path)) req_id = str(self._req_counter) self._req_counter += 1 # self._log.debug("path: %s", url) req_struct = { "version": "1.1", "id": req_id, "method": str(method), } if not kwargs: req_struct["params"] = list(args) else: req_struct['params'] = kwargs.copy() if args: req_struct['params'].update(dict(enumerate(args))) req_body = json.dumps(req_struct, cls=json_helpers.JsonEncoder2) # makes it little more readable: req_body += "\n" del req_struct host = '%s:%s' % (self.host, self.port) res = self._transport.request(host, url, req_body) if res.get('version') not in ('1.0', '1.1', '1.2'): raise errors.RpcProtocolException("Invalid JSON version: %s" % \ res.get('version', '<unknown>')) if res.get('id') != req_id: raise errors.RpcProtocolException("Protocol Out of order: %r != %r" %\ (res.get('id'), req_id)) if res.get('error'): raise RpcJServerException(res['error']) except socket.error, err: if err.errno in errors.ENONET: raise errors.RpcNetworkException(err.strerror, err.errno) self._log.error("socket error: %s" % err) self._log.debug("call %s/%s(%r)", '/'.join(path), method, args) raise errors.RpcProtocolException(err) except httplib.InvalidURL, err: raise errors.RpcNoProtocolException(err.args[0])
class RpcJsonConnection(TCPConnection): """ JSON-based RPC connection class Usually opened at port 8069 on the server. Unlike the XML-RPC connection classes, this doesn't need to keep proxy objects, or "gateways" to the server. Instead, it will simple issue HTTP requests for every RPC call needed, over a persistent Transport instance. With the same TCP connection, same authentication (negotiation), it can work over different url paths (end-objects). """ name = "RPC-JSON" codename = 'http' _TransportClass = JSONAuthTransport def __init__(self, session): super(RpcJsonConnection, self).__init__(session) self._transport = None self._authclient = None self._req_counter = 1 def _prepare_transport(self): update = False if not self._authclient: self._authclient = BasicAuthClient() update = True if not self._authclient.hasRealm("OpenERP User"): self._authclient.addLogin("OpenERP User", self._session.auth_proxy.user, self._session.auth_proxy.passwd) update = True if not self._authclient.hasRealm("OpenERP Admin") \ and self._session.auth_proxy.superpass: self._authclient.addLogin("OpenERP Admin", 'root', self._session.auth_proxy.superpass) if update: self._transport.setAuthClient(self._authclient) def establish(self, kwargs, do_init=False): if 'host' in kwargs: self.host = kwargs['host'] if 'port' in kwargs: self.port = kwargs['port'] send_gzip = True self._transport = self._TransportClass(send_gzip=send_gzip) uri = '%s:%s' % (self.host, self.port) self._prepare_transport() self._transport.make_connection(uri) if do_init: self._establish_int() return True def _do_request(self, path, method, args, kwargs=None): """ Request, including the marshalling of parameters Also decode possible Exceptions to the appropriate RpcException classes @param path a list of path components """ try: url = '/json/' + '/'.join(map(str, path)) req_id = str(self._req_counter) self._req_counter += 1 # self._log.debug("path: %s", url) req_struct = { "version": "1.1", "id": req_id, "method": str(method), } if not kwargs: req_struct["params"] = list(args) else: req_struct['params'] = kwargs.copy() if args: req_struct['params'].update(dict(enumerate(args))) req_body = json.dumps(req_struct, cls=json_helpers.JsonEncoder2) # makes it little more readable: req_body += "\n" del req_struct host = '%s:%s' % (self.host, self.port) res = self._transport.request(host, url, req_body) if res.get('version') not in ('1.0', '1.1', '1.2'): raise errors.RpcProtocolException("Invalid JSON version: %s" % \ res.get('version', '<unknown>')) if res.get('id') != req_id: raise errors.RpcProtocolException("Protocol Out of order: %r != %r" %\ (res.get('id'), req_id)) if res.get('error'): raise RpcJServerException(res['error']) except socket.error, err: if err.errno in errors.ENONET: raise errors.RpcNetworkException(err.strerror, err.errno) self._log.error("socket error: %s" % err) self._log.debug("call %s/%s(%r)", '/'.join(path), method, args) raise errors.RpcProtocolException( err ) except httplib.InvalidURL, err: raise errors.RpcNoProtocolException(err.args[0])