bricks/bricks/jsoncall.js
2024-02-01 17:54:04 +08:00

197 lines
4.6 KiB
JavaScript
Executable File

var bricks = window.bricks || {};
function url_params(data) {
return Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&');
}
bricks.HttpText = class {
constructor(headers){
/*
var _headers = {
"Accept":"text/html",
}
_headers = {
"Accept": "application/json",
};
*/
if (!headers)
headers = {};
this.headers = headers || {
"Accept":"text/html",
};
extend(this.headers, headers);
this.params = {
"_webbricks_":1
}
}
url_parse(url){
var a = url.split('?');
if (a.length == 1) return url;
url = a[0];
var a = a[1].split('&');
for (var i=0;i<a.length;i++){
var b;
b = a[i].split('=');
this.params[b[0]] = b[1];
}
return url;
}
async get_result_data(resp){
return await resp.text();
}
add_own_params(params){
if (! params)
params = {};
var session = bricks.app.get_session();
if (params instanceof FormData){
for ( const [key, value] of Object.entries(this.params)){
params.append(key, value);
}
if(session){
params.append('session', session);
}
return params;
}
params = Object.assign(this.params, params);
if (session){
extend(params,{session:session});
}
return params;
}
add_own_headers(headers){
if (! headers){
headers = {};
}
return Object.assign(this.headers, headers);
}
async httpcall(url, {method='GET', headers=null, params=null}={}){
url = this.url_parse(url);
var data = this.add_own_params(params);
var header = this.add_own_headers(headers);
var _params = {
"method":method,
}
// _params.headers = headers;
if (method == 'GET' || method == 'HEAD') {
let pstr = url_params(data);
url = url + '?' + pstr;
} else {
if (data instanceof FormData){
_params.body = data;
} else {
_params.body = JSON.stringify(data);
}
}
const fetchResult = await fetch(url, _params);
var result=null;
result = await this.get_result_data(fetchResult);
if (fetchResult.ok){
var ck = objget(fetchResult.headers, 'Set-Cookie');
if (ck){
var session = ck.split(';')[0];
bricks.app.save_session(session);
}
return result;
}
if (fetchResult.status == 401 && bricks.app.login_url){
return await this.withLoginInfo(url, _params);
}
const resp_error = {
"type":"Error",
"message":result.message || 'Something went wrong',
"data":result.data || '',
"code":result.code || ''
};
const error = new Error();
error.info = resp_error;
return error;
}
async withLoginInfo(url, params){
var get_login_info = function(e){
console.log('login info:', e.target.getValue());
return e.target.getValue();
}
var w = await widgetBuild({
"id":"login_form",
"widgettype":"urlwidget",
"options":{
"url":bricks.app.login_url
}
});
var login_info = await new Promise((resolve, reject, w) => {
w.bind('submit', (event) => {
resolve(event.target.getValue());
event.target.dismiss();
});
w.bind('discard', (event) => {
resolve(null);
event.target.dismiss()
});
});
if (login_info){
this.set_authorization_header(params, lgin_info);
const fetchResult = await fetch(url, params);
var result=null;
result = await this.get_result_data(fetchResult);
if (fetchResult.ok){
return result;
}
if (fetchResult.status == 401){
return await this.withLoginInfo(url, params);
}
}
const resp_error = {
"type":"Error",
"message":result.message || 'Something went wrong',
"data":result.data || '',
"code":result.code || ''
};
const error = new Error();
error.info = resp_error;
return error;
}
set_authorization_header(params, lgin_info){
var auth = 'password' + '::' + login_info.user + '::' + login_info.password;
var rsa = bricks.app.rsa;
var code = rsa.encrypt(auth);
self.header.authorization = btoa(code)
}
async get(url, {headers=null, params=null}={}){
return await this.httpcall(url, {
method:'GET',
headers:headers,
params:params
});
}
async post(url, {headers=null, params=null}={}){
return await this.httpcall(url, {
method:'POST',
headers:headers,
params:params
});
}
}
bricks.HttpJson = class extends bricks.HttpText {
constructor(headers){
if (!headers)
headers = {};
super(headers);
this.headers = {
"Accept": "application/json",
}
extend(this.headers, headers);
}
async get_result_data(resp) {
return await resp.json()
}
}
bricks.hc = new bricks.HttpText();
bricks.tget = bricks.hc.get.bind(bricks.hc);
bricks.tpost = bricks.hc.post.bind(bricks.hc);
bricks.jc = new bricks.HttpJson();
bricks.jcall = bricks.jc.httpcall.bind(bricks.jc);
bricks.jget = bricks.jc.get.bind(bricks.jc);
bricks.jpost = bricks.jc.post.bind(bricks.jc);