Revamp API wrapper code
- Refactoring throughout JSON-RPC, lbrynet and Lighthouse logic - Move JSON-RPC stuff into its own module - Add ability to directly call API methods on the lbry and lighthouse modules, e.g. lbry.file_list({name: 'what'}) - New-style API calls use promises instead of callbacks. - Converted some lbrynet calls and all Lighthouse calls to use the new style
This commit is contained in:
parent
d633c2790c
commit
1567a2de0a
7 changed files with 146 additions and 136 deletions
2
lbryum
2
lbryum
|
@ -1 +1 @@
|
||||||
Subproject commit 07882aa766acf296a494bd641d88397a27bea83a
|
Subproject commit 65703001c7f199492c0aa5ef4dc976c8fde0fad5
|
|
@ -8,9 +8,8 @@ Web UI version numbers should always match the corresponding version of LBRY App
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
### Added
|
### Added
|
||||||
*
|
* You can now make API calls directly on the lbry module, e.g. lbry.peer_list()
|
||||||
*
|
* New-style API calls return promises instead of using callbacks
|
||||||
*
|
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
*
|
*
|
||||||
|
|
69
ui/js/jsonrpc.js
Normal file
69
ui/js/jsonrpc.js
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
const jsonrpc = {};
|
||||||
|
|
||||||
|
jsonrpc.call = function (connectionString, method, params, callback, errorCallback, connectFailedCallback, timeout) {
|
||||||
|
var xhr = new XMLHttpRequest;
|
||||||
|
if (typeof connectFailedCallback !== 'undefined') {
|
||||||
|
if (timeout) {
|
||||||
|
xhr.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
xhr.addEventListener('error', function (e) {
|
||||||
|
connectFailedCallback(e);
|
||||||
|
});
|
||||||
|
xhr.addEventListener('timeout', function() {
|
||||||
|
connectFailedCallback(new Error('XMLHttpRequest connection timed out'));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
xhr.addEventListener('load', function() {
|
||||||
|
var response = JSON.parse(xhr.responseText);
|
||||||
|
|
||||||
|
if (response.error) {
|
||||||
|
if (errorCallback) {
|
||||||
|
errorCallback(response.error);
|
||||||
|
} else {
|
||||||
|
var errorEvent = new CustomEvent('unhandledError', {
|
||||||
|
detail: {
|
||||||
|
connectionString: connectionString,
|
||||||
|
method: method,
|
||||||
|
params: params,
|
||||||
|
code: response.error.code,
|
||||||
|
message: response.error.message,
|
||||||
|
data: response.error.data
|
||||||
|
}
|
||||||
|
});
|
||||||
|
document.dispatchEvent(errorEvent)
|
||||||
|
}
|
||||||
|
} else if (callback) {
|
||||||
|
callback(response.result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (connectFailedCallback) {
|
||||||
|
xhr.addEventListener('error', function (event) {
|
||||||
|
connectFailedCallback(event);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
xhr.addEventListener('error', function (event) {
|
||||||
|
var errorEvent = new CustomEvent('unhandledError', {
|
||||||
|
detail: {
|
||||||
|
connectionString: connectionString,
|
||||||
|
method: method,
|
||||||
|
params: params,
|
||||||
|
code: xhr.status,
|
||||||
|
message: 'Connection to API server failed'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
document.dispatchEvent(errorEvent);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
xhr.open('POST', connectionString, true);
|
||||||
|
xhr.send(JSON.stringify({
|
||||||
|
'jsonrpc': '2.0',
|
||||||
|
'method': method,
|
||||||
|
'params': params,
|
||||||
|
'id': 0
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
export default jsonrpc;
|
|
@ -1,9 +1,10 @@
|
||||||
import lighthouse from './lighthouse.js';
|
import lighthouse from './lighthouse.js';
|
||||||
|
import jsonrpc from './jsonrpc.js';
|
||||||
|
|
||||||
const {remote} = require('electron');
|
const {remote} = require('electron');
|
||||||
const menu = remote.require('./menu/main-menu');
|
const menu = remote.require('./menu/main-menu');
|
||||||
|
|
||||||
var lbry = {
|
let lbry = {
|
||||||
isConnected: false,
|
isConnected: false,
|
||||||
rootPath: '.',
|
rootPath: '.',
|
||||||
daemonConnectionString: 'http://localhost:5279/lbryapi',
|
daemonConnectionString: 'http://localhost:5279/lbryapi',
|
||||||
|
@ -22,74 +23,8 @@ var lbry = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
lbry.jsonrpc_call = function (connectionString, method, params, callback, errorCallback, connectFailedCallback, timeout) {
|
|
||||||
var xhr = new XMLHttpRequest;
|
|
||||||
if (typeof connectFailedCallback !== 'undefined') {
|
|
||||||
if (timeout) {
|
|
||||||
xhr.timeout = timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
xhr.addEventListener('error', function (e) {
|
|
||||||
connectFailedCallback(e);
|
|
||||||
});
|
|
||||||
xhr.addEventListener('timeout', function() {
|
|
||||||
connectFailedCallback(new Error('XMLHttpRequest connection timed out'));
|
|
||||||
})
|
|
||||||
}
|
|
||||||
xhr.addEventListener('load', function() {
|
|
||||||
var response = JSON.parse(xhr.responseText);
|
|
||||||
|
|
||||||
if (response.error) {
|
|
||||||
if (errorCallback) {
|
|
||||||
errorCallback(response.error);
|
|
||||||
} else {
|
|
||||||
var errorEvent = new CustomEvent('unhandledError', {
|
|
||||||
detail: {
|
|
||||||
connectionString: connectionString,
|
|
||||||
method: method,
|
|
||||||
params: params,
|
|
||||||
code: response.error.code,
|
|
||||||
message: response.error.message,
|
|
||||||
data: response.error.data
|
|
||||||
}
|
|
||||||
});
|
|
||||||
document.dispatchEvent(errorEvent)
|
|
||||||
}
|
|
||||||
} else if (callback) {
|
|
||||||
callback(response.result);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (connectFailedCallback) {
|
|
||||||
xhr.addEventListener('error', function (event) {
|
|
||||||
connectFailedCallback(event);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
xhr.addEventListener('error', function (event) {
|
|
||||||
var errorEvent = new CustomEvent('unhandledError', {
|
|
||||||
detail: {
|
|
||||||
connectionString: connectionString,
|
|
||||||
method: method,
|
|
||||||
params: params,
|
|
||||||
code: xhr.status,
|
|
||||||
message: 'Connection to API server failed'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
document.dispatchEvent(errorEvent);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
xhr.open('POST', connectionString, true);
|
|
||||||
xhr.send(JSON.stringify({
|
|
||||||
'jsonrpc': '2.0',
|
|
||||||
'method': method,
|
|
||||||
'params': params,
|
|
||||||
'id': 0
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
lbry.call = function (method, params, callback, errorCallback, connectFailedCallback) {
|
lbry.call = function (method, params, callback, errorCallback, connectFailedCallback) {
|
||||||
lbry.jsonrpc_call(lbry.daemonConnectionString, method, [params], callback, errorCallback, connectFailedCallback);
|
jsonrpc.call(lbry.daemonConnectionString, method, [params], callback, errorCallback, connectFailedCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -244,12 +179,10 @@ lbry.getCostInfoForName = function(name, callback, errorCallback) {
|
||||||
}, errorCallback);
|
}, errorCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
lighthouse.getSizeForName(name, (size) => {
|
lighthouse.get_size_for_name(name).then((size) => {
|
||||||
getCostWithData(name, size, callback, errorCallback);
|
getCostWithData(name, size, callback, errorCallback);
|
||||||
}, () => {
|
}, () => {
|
||||||
getCostNoData(name, callback, errorCallback);
|
getCostNoData(name, callback, errorCallback);
|
||||||
}, () => {
|
|
||||||
getCostNoData(name, callback, errorCallback);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -329,7 +262,7 @@ lbry.getFileInfoWhenListed = function(name, callback, timeoutCallback, tryNum=0)
|
||||||
|
|
||||||
// Calls callback with file info when it appears in the lbrynet file manager.
|
// Calls callback with file info when it appears in the lbrynet file manager.
|
||||||
// If timeoutCallback is provided, it will be called if the file fails to appear.
|
// If timeoutCallback is provided, it will be called if the file fails to appear.
|
||||||
lbry.getFileStatus(name, (fileInfo) => {
|
lbry.file_list({name: name}).then(([fileInfo]) => {
|
||||||
if (fileInfo) {
|
if (fileInfo) {
|
||||||
callback(fileInfo);
|
callback(fileInfo);
|
||||||
} else {
|
} else {
|
||||||
|
@ -560,4 +493,18 @@ lbry.showMenuIfNeeded = function() {
|
||||||
sessionStorage.setItem('menuShown', chosenMenu);
|
sessionStorage.setItem('menuShown', chosenMenu);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
lbry = new Proxy(lbry, {
|
||||||
|
get: function(target, name) {
|
||||||
|
if (name in target) {
|
||||||
|
return target[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
return function(params={}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
jsonrpc.call(lbry.connectionString, name, [params], resolve, reject, reject);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default lbry;
|
export default lbry;
|
||||||
|
|
|
@ -1,67 +1,64 @@
|
||||||
import lbry from './lbry.js';
|
import lbry from './lbry.js';
|
||||||
|
import jsonrpc from './jsonrpc.js';
|
||||||
|
|
||||||
var lighthouse = {
|
const queryTimeout = 5000;
|
||||||
_query_timeout: 5000,
|
const maxQueryTries = 5;
|
||||||
_max_query_tries: 5,
|
const defaultServers = [
|
||||||
|
'http://lighthouse4.lbry.io:50005',
|
||||||
|
'http://lighthouse5.lbry.io:50005',
|
||||||
|
'http://lighthouse6.lbry.io:50005',
|
||||||
|
];
|
||||||
|
const path = '/';
|
||||||
|
|
||||||
servers: [
|
let server = null;
|
||||||
'http://lighthouse4.lbry.io:50005',
|
let connectTryNum = 0;
|
||||||
'http://lighthouse5.lbry.io:50005',
|
|
||||||
'http://lighthouse6.lbry.io:50005',
|
|
||||||
],
|
|
||||||
path: '/',
|
|
||||||
|
|
||||||
call: function(method, params, callback, errorCallback, connectFailedCallback, timeout) {
|
function getServers() {
|
||||||
const handleConnectFailed = function(tryNum=0) {
|
return lbry.getClientSetting('useCustomLighthouseServers')
|
||||||
if (tryNum > lighthouse._max_query_tries) {
|
? lbry.getClientSetting('customLighthouseServers')
|
||||||
if (connectFailedCallback) {
|
: defaultServers;
|
||||||
connectFailedCallback();
|
}
|
||||||
} else {
|
|
||||||
throw new Error(`Could not connect to Lighthouse server. Last server attempted: ${lighthouse.server}`);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
lbry.call(method, params, callback, errorCallback, () => { handleConnectFailed(tryNum + 1) }, timeout);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the Lighthouse server if it hasn't been set yet, or if the current server is not in
|
function call(method, params, callback, errorCallback) {
|
||||||
// the current set of servers (most likely because of a settings change).
|
if (connectTryNum > maxQueryTries) {
|
||||||
if (typeof lighthouse.server === 'undefined' || lighthouse.getServers().indexOf(lighthouse.server) == -1) {
|
if (connectFailedCallback) {
|
||||||
lighthouse.chooseNewServer();
|
connectFailedCallback();
|
||||||
}
|
|
||||||
|
|
||||||
lbry.jsonrpc_call(this.server + this.path, method, params, callback, errorCallback,
|
|
||||||
() => { handleConnectFailed() }, timeout || lighthouse.query_timeout);
|
|
||||||
},
|
|
||||||
|
|
||||||
getServers: function() {
|
|
||||||
return lbry.getClientSetting('useCustomLighthouseServers')
|
|
||||||
? lbry.getClientSetting('customLighthouseServers')
|
|
||||||
: lighthouse.servers;
|
|
||||||
},
|
|
||||||
|
|
||||||
chooseNewServer: function() {
|
|
||||||
// Randomly choose a new Lighthouse server and switch to it. If a server is already set, this
|
|
||||||
// will choose a different one (used for switching servers after a failed query).
|
|
||||||
const servers = lighthouse.getServers();
|
|
||||||
let newServerChoices;
|
|
||||||
if (!lighthouse.server) {
|
|
||||||
newServerChoices = servers;
|
|
||||||
} else {
|
} else {
|
||||||
newServerChoices = lighthouse.getServers().slice();
|
throw new Error(`Could not connect to Lighthouse server. Last server attempted: ${server}`);
|
||||||
newServerChoices.splice(newServerChoices.indexOf(lighthouse.server), 1);
|
|
||||||
}
|
}
|
||||||
lighthouse.server = newServerChoices[Math.round(Math.random() * (newServerChoices.length - 1))];
|
|
||||||
},
|
|
||||||
|
|
||||||
search: function(query, callback, errorCallback, connectFailedCallback, timeout) {
|
|
||||||
lighthouse.call('search', [query], callback, errorCallback, connectFailedCallback, timeout);
|
|
||||||
},
|
|
||||||
|
|
||||||
getSizeForName: function(name, callback, errorCallback, connectFailedCallback, timeout) {
|
|
||||||
lighthouse.call('get_size_for_name', [name], callback, errorCallback, connectFailedCallback, timeout);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the Lighthouse server if it hasn't been set yet, if the current server is not in current
|
||||||
|
* set of servers (most likely because of a settings change), or we're re-trying after a failed
|
||||||
|
* query.
|
||||||
|
*/
|
||||||
|
if (!server || !getServers().includes(server) || connectTryNum > 0) {
|
||||||
|
// If there's a current server, filter it out so we get a new one
|
||||||
|
const newServerChoices = server ? getServers().filter((s) => s != server) : getServers();
|
||||||
|
server = newServerChoices[Math.round(Math.random() * (newServerChoices.length - 1))];
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonrpc.call(server + path, method, params, (response) => {
|
||||||
|
connectTryNum = 0;
|
||||||
|
callback(response);
|
||||||
|
}, (error) => {
|
||||||
|
connectTryNum = 0;
|
||||||
|
errorCallback(error);
|
||||||
|
}, () => {
|
||||||
|
connectTryNum++;
|
||||||
|
call(method, params, callback, errorCallback);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const lighthouse = new Proxy({}, {
|
||||||
|
get: function(target, name) {
|
||||||
|
return function(...params) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
call(name, params, resolve, reject);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export default lighthouse;
|
export default lighthouse;
|
||||||
|
|
|
@ -9,9 +9,7 @@ lbry.showMenuIfNeeded();
|
||||||
|
|
||||||
var init = function() {
|
var init = function() {
|
||||||
window.lbry = lbry;
|
window.lbry = lbry;
|
||||||
if (lbry.getClientSetting('debug')) {
|
window.lighthouse = lighthouse;
|
||||||
window.lighthouse = lighthouse;
|
|
||||||
}
|
|
||||||
|
|
||||||
var canvas = document.getElementById('canvas');
|
var canvas = document.getElementById('canvas');
|
||||||
if (window.sessionStorage.getItem('loaded') == 'y') {
|
if (window.sessionStorage.getItem('loaded') == 'y') {
|
||||||
|
|
|
@ -125,7 +125,7 @@ var DiscoverPage = React.createClass({
|
||||||
query: query,
|
query: query,
|
||||||
});
|
});
|
||||||
|
|
||||||
lighthouse.search(query, this.searchCallback);
|
lighthouse.search(query).then(this.searchCallback);
|
||||||
},
|
},
|
||||||
|
|
||||||
componentWillMount: function() {
|
componentWillMount: function() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue