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", "open": "0.0.5",
"rand-token": "0.4.0", "rand-token": "0.4.0",
"rcedit": "1.1.1", "rcedit": "1.1.1",
"request": "2.88.0",
"request-promise": "4.2.2",
"rimraf": "2.6.2", "rimraf": "2.6.2",
"sanitize-filename": "1.6.1", "sanitize-filename": "1.6.1",
"sax": "^1.2.4", "sax": "^1.2.4",

View File

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

View File

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