概述
1.RPC本质
RPC的语义是远程过程调用,在一般的印象中,就是将一个服务调用封装在一个本地方法中,让调用者像使用本地方法一样调用服务,对其屏蔽实现细节。而具体的实现是通过调用方和服务方的一套约定,基于TCP长连接进行数据交互达成。
作用:
1.屏蔽远程调用跟本地调用的区别,让我们感觉就是调用项目内的方法。
2.隐藏底层网络通信的复杂性,让我们更专注于业务逻辑。
2.dtcloud自带API及参数讲解演示
自带api注册文件地址:/addons/web/static/src/core/orm_service.js
API:
1.call
参数: model(模型) method(模型方法) args(方法参数) kwargs(可变参数)
例子:orm.call(model, “read”, [ids, fields], { context: ctx })
2.create
参数: model(模型) state(创建内容对应py create方法vals参数) ctx(context)
例子:orm.create(“partner”, { color: “red” })
3.read
参数: model(模型) ids(数据集ids) fields(读取字段) ctx(context)
例子:orm.read(“res.users”, [3], [“id”, “name”], { earth: “isfucked” })
4.readGroup
参数: model(模型) domain(筛选条件) fields(读取字段) groupby(分组字段) options(参数,只接收"lazy", “offset”, “orderby”, “limit”) ctx(context)
例子:orm.readGroup(“sale.order”,[[“user_id”, “=”, 2]],[“amount_total:sum”],[“date_order”],{ offset: 1 });
5.search
参数: model(模型) domain(筛选条件) options(参数,只接受"offset", “limit”, “order”) ctx(context)
例子:orm.search(“res.users”, [[“name”, “=”, ‘张三’]], {limit: 1})
6.searchRead
参数: model(模型) domain(筛选条件) options(参数,只接收"offset", “limit”, “order”) ctx(context)
例子:orm.searchRead(“sale.order”, [[“user_id”, “=”, 2]], [“amount_total”])
7.unlink
参数: model(模型) ids(数据集ids) ctx(context)
例子:orm.unlink(“ir.attachment”, [1,2]);
8.web_search_read
参数: model(模型) domain(筛选条件) options(参数,只接收"offset", “limit”, “order”) ctx(context)
例子:orm.searchRead(“sale.order”, [[“user_id”, “=”, 2]], [“amount_total”])
9.write
参数: model(模型) ids(数据集ids) data(修改参数) ctx(context)
例子:orm.write(“partner”, [43, 14], { active: false });
3.dtcloud错误机制如何进行定制
1.通过浏览器事件异常监控捕获unhandledrejection JS异常和代码中未捕获的promise异常。
2.通过handleError匹配相应错误并执行相应的操作。
4.如何样在其他页面使用RPC
如何样在其他地方使用RPC(源码)
rpc封装源码
class dtcloudCorsRpc {
constructor() {
this.request_counts = 0;
this.host = undefined;
this.db = undefined;
this.account_code = undefined;
}
setHost(host, db, account_code) {
this.host = host;
this.db = db;
this.account_code = account_code;
}
rpcSendRequest(url, json_data) {
function set_cookie(name, value, ttl) {
ttl = ttl || 24 * 60 * 60 * 365;
document.cookie = [
name + '=' + value,
'path=/',
'max-age=' + ttl,
'expires=' + new Date(new Date().getTime() + ttl * 1000).toGMTString()
].join(';');
}
return new Promise((resolve, reject) => {
// create a new asynchronous request
let request = new XMLHttpRequest();
// open the request
request.open("POST", this.host + url, true);
// Define the content type : JSON-RPC -> application/json
request.setRequestHeader(
"Content-Type",
"application/json; charset=UTF-8"
);
// set the response type
request.responseType = "json";
// set the mime type
if (request.overrideMimeType)
request.overrideMimeType("application/json");
// append session_id
request.withCredentials = false;
request.onload = () => {
if (request.status === 200) {
let data = request.response;
if (data.error) {
reject(data.error);
} else if (!('result' in data)) {
reject(Error("no result in data received"));
} else if (typeof data.result === 'object' && 'records' in data.result) {
resolve(data.result.records);
} else {
resolve(data.result);
}
} else {
reject(Error("Fail to get " + url));
}
};
request.send(JSON.stringify(json_data));
this.request_counts += 1;
});
}
login(db, login, password) {
return new Promise((resolve, reject) => {
// construct the data
let json_data = {
jsonrpc: "2.0",
method: "call",
params: { db: db, login: login, password: password },
id: "r" + this.request_counts
};
// send the request
this.rpcSendRequest("/web/session/authenticate", json_data).then(
rst => {
if (rst.uid !== false) {
console.log('login success');
// this.session_id = rst.session_id;
resolve(rst);
} else {
reject(Error("Fail to login in database"));
}
},
error => {
reject(error);
}
);
});
}
dtcloudRpcAuth(code, route) {
return new Promise((resolve, reject) => {
if (!code || code === "") {
reject(Error("the code param is invalidate"));
}
let xhr = new XMLHttpRequest();
xhr.responseType = "json";
let url = this.host + route + "?code=" + code + "&db=" + this.db + "&account_code=" + this.account_code;
xhr.open("GET", url, true);
xhr.onload = rst => {
if (xhr.status === 200) {
// this.session_id = xhr.response.session_id;
resolve(xhr.response);
} else {
reject(Error("Fail to login in database"));
}
};
xhr.send(null);
});
}
//
searchRead(model, params) {
let iParams = params || {};
iParams.model = model;
let json_data = {
jsonrpc: "2.0",
method: "call",
params: iParams,
id: "r" + this.request_counts
};
return this.rpcSendRequest("/web/dataset/search_read", json_data);
}
call(model, method, args, params) {
let _self = this;
let json_data = {
jsonrpc: "2.0",
params: {
model: model,
method: method,
args: args || [],
kwargs: params || {}
},
id: "r" + _self.request_counts
};
return this.rpcSendRequest("/web/dataset/call_kw", json_data);
}
// rpc.create('repair_manage_for_xian.lines', {name: 'crax'}).then((data)=>{})
create(model, datas) {
return this.call(model, "create", datas);
}
update(model, id, data) {
return this.call(model, "write", [id, data]);
}
del(model, ids) {
return this.call(model, "unlink", [ids]);
}
}
let rpc = new dtcloudCorsRpc();
中亿丰数字——何双新
最后
以上就是鳗鱼吐司为你收集整理的WEB RPC及其API的全部内容,希望文章能够帮你解决WEB RPC及其API所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复