-
-
Save BrajeshKhare/6ef48105ddc78d6dfee6793aed36c637 to your computer and use it in GitHub Desktop.
Odoo Python Connector (JSON RPC)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib2,random, json | |
odoo_url = 'SERVER_URL' | |
class OdooConnector: | |
context = {} | |
http = False | |
cookies = '' | |
def __init__(self): | |
self.http = httplib2.Http() | |
def get_session_info(self): | |
url = '/web/session/get_session_info' | |
requestData = self._create_params({}) | |
result = self.requestJSON(url, requestData) | |
res = result.get('result') | |
return res | |
def authenticate(self, username, password, db): | |
url = '/web/session/authenticate' | |
params = { | |
"db": db, | |
"login": username, | |
"password": password, | |
"context": {} | |
} | |
requestData = self._create_params(params) | |
result = self.requestJSON(url, requestData) | |
user = result.get('result') | |
self.context = user.get('user_context') | |
return user | |
def write(self,model, data, record_id): | |
url = "/web/dataset/call_kw" | |
args = [(record_id), data] | |
params = { | |
"model": model, | |
"method": "write", | |
"args": args, | |
"kwargs": {}, | |
"context": self.context | |
} | |
requestData = self._create_params(params) | |
return self.requestJSON(url, requestData) | |
def requestJSON(self, url, data): | |
headers = { | |
'Content-Type': 'application/json; charset=UTF-8', | |
'Connection': 'keep-alive' | |
} | |
if self.cookies: | |
headers['Cookie'] = self.cookies | |
res, content = self.http.request(odoo_url + url, 'POST', headers=headers, body=json.dumps(data)) | |
self.cookies = res.get('set-cookie', '') | |
return json.loads(content) | |
def _create_params(self, params): | |
data = { | |
"jsonrpc": "2.0", | |
"method": "call", | |
"params": params or {}, | |
"id": random.randint(1000, 6000) | |
} | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment