Add caching wrapper for settings_get API method
This commit is contained in:
parent
cf1107050d
commit
9b4ebbab0f
2 changed files with 42 additions and 4 deletions
|
@ -648,6 +648,23 @@ lbry.resolve = function(params={}) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Adds caching.
|
||||||
|
lbry.settings_get = function(params={}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (params.allow_cached) {
|
||||||
|
const cached = getSession('settings');
|
||||||
|
if (cached) {
|
||||||
|
return resolve(cached);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lbry.call('settings_get', {}, (settings) => {
|
||||||
|
setSession('settings', settings);
|
||||||
|
resolve(settings);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// lbry.get = function(params={}) {
|
// lbry.get = function(params={}) {
|
||||||
// return function(params={}) {
|
// return function(params={}) {
|
||||||
// return new Promise((resolve, reject) => {
|
// return new Promise((resolve, reject) => {
|
||||||
|
|
|
@ -10,7 +10,8 @@ const lbryio = {
|
||||||
enabled: false
|
enabled: false
|
||||||
};
|
};
|
||||||
|
|
||||||
const CONNECTION_STRING = 'http://localhost:8080/';
|
const CONNECTION_STRING = 'https://api.lbry.io/';
|
||||||
|
const EXCHANGE_RATE_TIMEOUT = 20 * 60 * 1000;
|
||||||
|
|
||||||
const mocks = {
|
const mocks = {
|
||||||
'reward_type.get': ({name}) => {
|
'reward_type.get': ({name}) => {
|
||||||
|
@ -24,12 +25,32 @@ const mocks = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
lbryio.call = function(resource, action, params={}, method='get') {
|
|
||||||
|
lbryio.getExchangeRates = function() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!lbryio.enabled && (resource != 'discover' || action != 'list')) {
|
const cached = getSession('exchangeRateCache');
|
||||||
reject(new Error("LBRY internal API is disabled"))
|
if (!cached || Date.now() - cached.time > EXCHANGE_RATE_TIMEOUT) {
|
||||||
|
lbryio.call('lbc', 'exchange_rate', {}, 'get', true).then(({lbc_usd, lbc_btc, btc_usd}) => {
|
||||||
|
const rates = {lbc_usd, lbc_btc, btc_usd};
|
||||||
|
setSession('exchangeRateCache', {
|
||||||
|
rates: rates,
|
||||||
|
time: Date.now(),
|
||||||
|
});
|
||||||
|
resolve(rates);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
resolve(cached.rates);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
lbryio.call = function(resource, action, params={}, method='get', evenIfDisabled=false) { // evenIfDisabled is for development, when we may have some calls working and some not
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!lbryio.enabled && !evenIfDisabled && (resource != 'discover' || action != 'list')) {
|
||||||
|
reject(new Error("LBRY interal API is disabled"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/* temp code for mocks */
|
/* temp code for mocks */
|
||||||
if (`${resource}.${action}` in mocks) {
|
if (`${resource}.${action}` in mocks) {
|
||||||
resolve(mocks[`${resource}.${action}`](params));
|
resolve(mocks[`${resource}.${action}`](params));
|
||||||
|
|
Loading…
Add table
Reference in a new issue