Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions odoolib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,17 @@ def __init__(self, error):
self.error = error
def __str__(self):
return repr(self.error)

def json_rpc(url, fct_name, params):
data = {
"jsonrpc": "2.0",
"method": fct_name,
"params": params,
"id": random.randint(0, 1000000000),
}
result_req = requests.post(url, data=json.dumps(data), headers={
"Content-Type":"application/json",
})
result = result_req.json()
if result.get("error", None):
raise JsonRPCException(result["error"])
return result.get("result", False)


class JsonRPCConnector(Connector):
"""
A type of connector that uses the JsonRPC protocol.
"""
PROTOCOL = 'jsonrpc'
session_reuse = False

__logger = _getChildLogger(_logger, 'connector.jsonrpc')
_session = None

def __init__(self, hostname, port=8069):
"""
Expand All @@ -137,15 +125,34 @@ def __init__(self, hostname, port=8069):
self.url = 'http://%s:%d/jsonrpc' % (hostname, port)

def send(self, service_name, method, *args):
return json_rpc(self.url, "call", {"service": service_name, "method": method, "args": args})

class JsonRPCSConnector(Connector):
return self._json_rpc(self.url, "call", {"service": service_name, "method": method, "args": args})

def _json_rpc(self, url, fct_name, params):
data = {
"jsonrpc": "2.0",
"method": fct_name,
"params": params,
"id": random.randint(0, 1000000000),
}
if self.session_reuse and not self._session:
self._session = requests.Session()
post_cli = self._session.post if self.session_reuse else requests.post

result_req = post_cli(url, data=json.dumps(data), headers={
"Content-Type":"application/json",
})
result = result_req.json()
if result.get("error", None):
raise JsonRPCException(result["error"])
return result.get("result", False)


class JsonRPCSConnector(JsonRPCConnector):
"""
A type of connector that uses the JsonRPC protocol.
"""
PROTOCOL = 'jsonrpcs'

__logger = _getChildLogger(_logger, 'connector.jsonrpc')
session_reuse = True

def __init__(self, hostname, port=8069):
"""
Expand All @@ -155,8 +162,6 @@ def __init__(self, hostname, port=8069):
"""
self.url = 'https://%s:%d/jsonrpc' % (hostname, port)

def send(self, service_name, method, *args):
return json_rpc(self.url, "call", {"service": service_name, "method": method, "args": args})

class Service(object):
"""
Expand Down