2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-02-19 16:41:10 +00:00
|
|
|
const Observable = require('frappejs/utils/observable');
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-19 16:41:10 +00:00
|
|
|
module.exports = class HTTPClient extends Observable {
|
2018-02-08 09:38:47 +00:00
|
|
|
constructor({ server, protocol = 'http' }) {
|
2018-02-19 16:41:10 +00:00
|
|
|
super();
|
|
|
|
|
2018-01-12 12:25:07 +00:00
|
|
|
this.server = server;
|
|
|
|
this.protocol = protocol;
|
|
|
|
|
2018-03-05 16:45:21 +00:00
|
|
|
// if the backend is http, then always client!
|
|
|
|
frappe.isServer = false;
|
|
|
|
|
2018-02-08 06:46:38 +00:00
|
|
|
this.initTypeMap();
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async insert(doctype, doc) {
|
|
|
|
doc.doctype = doctype;
|
2018-02-16 13:13:46 +00:00
|
|
|
let url = this.getURL('/api/resource', doctype);
|
2018-02-08 09:38:47 +00:00
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(doc)
|
2018-02-08 09:38:47 +00:00
|
|
|
})
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async get(doctype, name) {
|
2018-02-16 13:13:46 +00:00
|
|
|
let url = this.getURL('/api/resource', doctype, name);
|
2018-02-08 09:38:47 +00:00
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'GET',
|
2018-02-08 09:38:47 +00:00
|
|
|
headers: this.getHeaders()
|
|
|
|
})
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
async getAll({ doctype, fields, filters, start, limit, sort_by, order }) {
|
2018-02-16 13:13:46 +00:00
|
|
|
let url = this.getURL('/api/resource', doctype);
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-03-26 12:18:07 +00:00
|
|
|
url = url + "?" + frappe.getQueryString({
|
2018-01-12 12:25:07 +00:00
|
|
|
fields: JSON.stringify(fields),
|
|
|
|
filters: JSON.stringify(filters),
|
|
|
|
start: start,
|
|
|
|
limit: limit,
|
|
|
|
sort_by: sort_by,
|
|
|
|
order: order
|
|
|
|
});
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async update(doctype, doc) {
|
|
|
|
doc.doctype = doctype;
|
2018-02-16 13:13:46 +00:00
|
|
|
let url = this.getURL('/api/resource', doctype, doc.name);
|
2018-02-08 09:38:47 +00:00
|
|
|
|
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'PUT',
|
|
|
|
body: JSON.stringify(doc)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(doctype, name) {
|
2018-02-16 13:13:46 +00:00
|
|
|
let url = this.getURL('/api/resource', doctype, name);
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
return await this.fetch(url, {
|
2018-01-12 12:25:07 +00:00
|
|
|
method: 'DELETE',
|
|
|
|
});
|
2018-02-08 09:38:47 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
async deleteMany(doctype, names) {
|
2018-02-16 13:13:46 +00:00
|
|
|
let url = this.getURL('/api/resource', doctype);
|
2018-02-08 09:38:47 +00:00
|
|
|
|
|
|
|
return await this.fetch(url, {
|
|
|
|
method: 'DELETE',
|
|
|
|
body: JSON.stringify(names)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async exists(doctype, name) {
|
|
|
|
return (await this.getValue(doctype, name, 'name')) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getValue(doctype, name, fieldname) {
|
2018-02-16 13:13:46 +00:00
|
|
|
let url = this.getURL('/api/resource', doctype, name, fieldname);
|
2018-02-08 09:38:47 +00:00
|
|
|
|
|
|
|
return (await this.fetch(url, {
|
|
|
|
method: 'GET',
|
|
|
|
})).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetch(url, args) {
|
|
|
|
args.headers = this.getHeaders();
|
|
|
|
let response = await frappe.fetch(url, args);
|
2018-02-09 12:55:55 +00:00
|
|
|
let data = await response.json();
|
|
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw Error(data.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:38:47 +00:00
|
|
|
getURL(...parts) {
|
2018-02-19 16:41:10 +00:00
|
|
|
return this.protocol + '://' + this.server + parts.join('/');
|
2018-02-08 09:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getHeaders() {
|
|
|
|
return {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
2018-01-23 13:18:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 06:46:38 +00:00
|
|
|
initTypeMap() {
|
2018-02-27 16:29:14 +00:00
|
|
|
this.typeMap = {
|
2018-02-08 09:38:47 +00:00
|
|
|
'Currency': true
|
|
|
|
, 'Int': true
|
|
|
|
, 'Float': true
|
|
|
|
, 'Percent': true
|
|
|
|
, 'Check': true
|
|
|
|
, 'Small Text': true
|
|
|
|
, 'Long Text': true
|
|
|
|
, 'Code': true
|
|
|
|
, 'Text Editor': true
|
|
|
|
, 'Date': true
|
|
|
|
, 'Datetime': true
|
|
|
|
, 'Time': true
|
|
|
|
, 'Text': true
|
|
|
|
, 'Data': true
|
|
|
|
, 'Link': true
|
2018-03-27 04:20:42 +00:00
|
|
|
, 'DynamicLink': true
|
2018-02-08 09:38:47 +00:00
|
|
|
, 'Password': true
|
|
|
|
, 'Select': true
|
|
|
|
, 'Read Only': true
|
2018-03-29 18:51:24 +00:00
|
|
|
, 'File': true
|
2018-02-08 09:38:47 +00:00
|
|
|
, 'Attach': true
|
|
|
|
, 'Attach Image': true
|
|
|
|
, 'Signature': true
|
|
|
|
, 'Color': true
|
|
|
|
, 'Barcode': true
|
|
|
|
, 'Geolocation': true
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|