forked from astronaut1712/angular2-odoo-jsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodoorpc.service.js
224 lines (224 loc) · 8.8 KB
/
odoorpc.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var core_1 = require("@angular/core");
var http_1 = require("@angular/http");
require("rxjs/add/operator/toPromise");
var Cookies = (function () {
function Cookies() {
this.session_id = null;
}
Cookies.prototype.delete_sessionId = function () {
this.session_id = null;
document.cookie = "session_id=; expires=Wed, 29 Jun 2016 00:00:00 UTC";
};
Cookies.prototype.get_sessionId = function () {
return document
.cookie.split("; ")
.filter(function (x) { return x.indexOf("session_id") === 0; })
.map(function (x) { return x.split("=")[1]; })
.pop() || this.session_id || "";
};
Cookies.prototype.set_sessionId = function (val) {
document.cookie = "session_id=" + val;
this.session_id = val;
};
return Cookies;
}());
var OdooRPCService = (function () {
function OdooRPCService(http) {
this.http = http;
this.uniq_id_counter = 0;
this.shouldManageSessionId = false; // try without first
this.context = JSON.parse(localStorage.getItem("user_context")) || { "lang": "en_US" };
this.cookies = new Cookies();
}
OdooRPCService.prototype.buildRequest = function (url, params) {
this.uniq_id_counter += 1;
if (this.shouldManageSessionId) {
params.session_id = this.cookies.get_sessionId();
}
var json_data = {
jsonrpc: "2.0",
method: "call",
params: params,
};
this.headers = new http_1.Headers({
"Content-Type": "application/json",
"X-Openerp-Session-Id": this.cookies.get_sessionId(),
"Authorization": "Basic " + btoa("" + this.http_auth)
});
return JSON.stringify({
jsonrpc: "2.0",
method: "call",
params: params,
});
};
OdooRPCService.prototype.handleOdooErrors = function (response) {
response = response.json();
if (!response.error) {
return response.result;
}
var error = response.error;
var errorObj = {
title: " ",
message: "",
fullTrace: error
};
if (error.code === 200 && error.message === "Odoo Server Error" && error.data.name === "werkzeug.exceptions.NotFound") {
errorObj.title = "page_not_found";
errorObj.message = "HTTP Error";
}
else if ((error.code === 100 && error.message === "Odoo Session Expired") ||
(error.code === 300 && error.message === "OpenERP WebClient Error" && error.data.debug.match("SessionExpiredException")) // v7
) {
errorObj.title = "session_expired";
this.cookies.delete_sessionId();
}
else if ((error.message === "Odoo Server Error" && /FATAL: database "(.+)" does not exist/.test(error.data.message))) {
errorObj.title = "database_not_found";
errorObj.message = error.data.message;
}
else if ((error.data.name === "openerp.exceptions.AccessError")) {
errorObj.title = "AccessError";
errorObj.message = error.data.message;
}
else {
var split = ("" + error.data.fault_code).split("\n")[0].split(" -- ");
if (split.length > 1) {
error.type = split.shift();
error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
}
if (error.code === 200 && error.type) {
errorObj.title = error.type;
errorObj.message = error.data.fault_code.replace(/\n/g, "<br />");
}
else {
errorObj.title = error.message;
errorObj.message = error.data.debug.replace(/\n/g, "<br />");
}
}
return Promise.reject(errorObj);
};
OdooRPCService.prototype.handleHttpErrors = function (error) {
return Promise.reject(error.message || error);
};
OdooRPCService.prototype.init = function (configs) {
this.odoo_server = configs.odoo_server;
this.http_auth = configs.http_auth || null;
};
OdooRPCService.prototype.setOdooServer = function (odoo_server) {
this.odoo_server = odoo_server;
};
OdooRPCService.prototype.setHttpAuth = function (http_auth) {
this.http_auth = http_auth;
};
OdooRPCService.prototype.sendRequest = function (url, params) {
var options = this.buildRequest(url, params);
return this.http.post(this.odoo_server + url, options, { headers: this.headers })
.toPromise()
.then(this.handleOdooErrors)
.catch(this.handleHttpErrors);
};
OdooRPCService.prototype.getServerInfo = function () {
return this.sendRequest("/web/webclient/version_info", {});
};
OdooRPCService.prototype.getSessionInfo = function () {
return this.sendRequest("/web/session/get_session_info", {});
};
OdooRPCService.prototype.login = function (db, login, password) {
var params = {
db: db,
login: login,
password: password
};
var $this = this;
return this.sendRequest("/web/session/authenticate", params).then(function (result) {
if (!result.uid) {
$this.cookies.delete_sessionId();
return Promise.reject({
title: "wrong_login",
message: "Username and password don't match",
fullTrace: result
});
}
$this.context = result.user_context;
localStorage.setItem("user_context", JSON.stringify($this.context));
$this.cookies.set_sessionId(result.session_id);
return result;
});
};
OdooRPCService.prototype.isLoggedIn = function (force) {
var _this = this;
if (force === void 0) { force = true; }
if (!force) {
return Promise.resolve(this.cookies.get_sessionId().length > 0);
}
return this.getSessionInfo().then(function (result) {
_this.cookies.set_sessionId(result.session_id);
return !!(result.uid);
});
};
OdooRPCService.prototype.logout = function (force) {
var _this = this;
if (force === void 0) { force = true; }
this.cookies.delete_sessionId();
if (force) {
return this.getSessionInfo().then(function (r) {
if (r.db)
return _this.login(r.db, "", "");
});
}
else {
return Promise.resolve();
}
};
OdooRPCService.prototype.getDbList = function () {
return this.sendRequest("/web/database/get_list", {});
};
OdooRPCService.prototype.searchRead = function (model, domain, fields) {
var params = {
model: model,
domain: domain,
fields: fields,
context: this.context
};
return this.sendRequest("/web/dataset/search_read", params);
};
OdooRPCService.prototype.updateContext = function (context) {
var _this = this;
localStorage.setItem("user_context", JSON.stringify(context));
var args = [[this.context.uid], context];
this.call("res.users", "write", args, {})
.then(function () { return _this.context = context; })
.catch(function (err) { return _this.context = context; });
};
OdooRPCService.prototype.getContext = function () {
return this.context;
};
OdooRPCService.prototype.call = function (model, method, args, kwargs) {
kwargs = kwargs || {};
kwargs.context = kwargs.context || {};
Object.assign(kwargs.context, this.context);
var params = {
model: model,
method: method,
args: args,
kwargs: kwargs,
};
return this.sendRequest("/web/dataset/call_kw", params);
};
OdooRPCService = __decorate([
core_1.Injectable(),
__param(0, core_1.Inject(http_1.Http))
], OdooRPCService);
return OdooRPCService;
}());
exports.OdooRPCService = OdooRPCService;