using http service for sync setup as well, removed request(-promise) dependency

This commit is contained in:
azivner 2018-12-17 22:12:26 +01:00
parent a1f939e3a0
commit 7c3bbfd45e
3 changed files with 28 additions and 35 deletions

View file

@ -50,8 +50,6 @@
"open": "0.0.5",
"rand-token": "0.4.0",
"rcedit": "1.1.1",
"request": "2.88.0",
"request-promise": "4.2.2",
"rimraf": "2.6.2",
"sanitize-filename": "1.6.1",
"sax": "^1.2.4",

View file

@ -14,6 +14,17 @@ function exec(opts) {
return new Promise(async (resolve, reject) => {
try {
const headers = {
Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
'Content-Type': 'application/json'
};
if (opts.auth) {
const token = new Buffer(opts.auth.user + ":" + opts.auth.pass).toString('base64');
headers['Authorization'] = `Basic ${token}`;
}
const request = client.request({
method: opts.method,
// url is used by electron net module
@ -24,10 +35,7 @@ function exec(opts) {
port: parsedUrl.port,
path: parsedUrl.path,
timeout: opts.timeout,
headers: {
Cookie: (opts.cookieJar && opts.cookieJar.header) || "",
'Content-Type': 'application/json'
}
headers
});
request.on('response', response => {
@ -48,7 +56,7 @@ function exec(opts) {
catch (e) {
log.error("Failed to deserialize sync response: " + responseStr);
reject(generateError(e));
reject(generateError(e, opts));
}
});
});
@ -56,7 +64,7 @@ function exec(opts) {
request.end(opts.body ? JSON.stringify(opts.body) : undefined);
}
catch (e) {
reject(generateError(e));
reject(generateError(e, opts));
}
})
}
@ -77,8 +85,8 @@ function getClient(opts) {
}
}
function generateError(e) {
return new Error(`Request to ${method} ${syncServerHost}${requestPath} failed, error: ${e.message}`);
function generateError(e, opts) {
return new Error(`Request to ${opts.method} ${opts.url} failed, error: ${e.message}`);
}
module.exports = {

View file

@ -1,10 +1,10 @@
const rp = require('request-promise');
const syncService = require('./sync');
const log = require('./log');
const sqlInit = require('./sql_init');
const repository = require('./repository');
const optionService = require('./options');
const syncOptions = require('./sync_options');
const request = require('./request');
async function hasSyncServerSchemaAndSeed() {
const response = await requestToSyncServer('GET', '/api/setup/status');
@ -37,23 +37,13 @@ async function sendSeedToSyncServer() {
}
async function requestToSyncServer(method, path, body = null) {
const rpOpts = {
uri: await syncOptions.getSyncServerHost() + path,
method: method,
json: true
};
if (body) {
rpOpts.body = body;
}
const syncProxy = await syncOptions.getSyncProxy();
if (syncProxy) {
rpOpts.proxy = syncProxy;
}
return await rp(rpOpts);
return await request.exec({
method,
url: await syncOptions.getSyncServerHost() + path,
body,
proxy: await syncOptions.getSyncProxy(),
timeout: await syncOptions.getSyncTimeout()
});
}
async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, password) {
@ -68,19 +58,16 @@ async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, pass
log.info("Getting document options from sync server.");
// response is expected to contain documentId and documentSecret options
const options = await rp.get({
uri: syncServerHost + '/api/setup/sync-seed',
const options = await request.exec({
method: 'get',
url: syncServerHost + '/api/setup/sync-seed',
auth: {
'user': username,
'pass': password
},
json: true
proxy: syncProxy
});
if (syncProxy) {
options.proxy = syncProxy;
}
await sqlInit.createDatabaseForSync(options, syncServerHost, syncProxy);
triggerSync();