lbry-desktop/src/renderer/lbryio.js

156 lines
4 KiB
JavaScript
Raw Normal View History

import { ipcRenderer } from 'electron';
2018-04-18 06:03:01 +02:00
import { Lbry } from 'lbry-redux';
import querystring from 'querystring';
2017-04-09 17:06:23 +02:00
const Lbryio = {
2017-06-08 02:56:52 +02:00
enabled: true,
authenticationPromise: null,
2017-04-17 16:01:33 +02:00
};
2017-06-06 06:21:55 +02:00
const CONNECTION_STRING = process.env.LBRY_APP_API_URL
? process.env.LBRY_APP_API_URL.replace(/\/*$/, '/') // exactly one slash at the end
: 'https://api.lbry.io/';
Lbryio.call = (resource, action, params = {}, method = 'get') => {
if (!Lbryio.enabled) {
console.log(__('Internal API disabled'));
return Promise.reject(new Error(__('LBRY internal API is disabled')));
}
2017-06-08 02:56:52 +02:00
if (!(method === 'get' || method === 'post')) {
return Promise.reject(new Error(__('Invalid method')));
}
2017-06-08 02:56:52 +02:00
2017-11-10 15:55:30 +01:00
function checkAndParse(response) {
if (response.status >= 200 && response.status < 300) {
2017-11-10 15:55:30 +01:00
return response.json();
}
2017-12-13 22:36:30 +01:00
return response.json().then(json => {
let error;
if (json.error) {
error = new Error(json.error);
} else {
error = new Error('Unknown API error signature');
2017-12-13 22:36:30 +01:00
}
error.response = response; // This is primarily a hack used in actions/user.js
2017-12-13 22:36:30 +01:00
return Promise.reject(error);
});
}
2017-06-08 02:56:52 +02:00
function makeRequest(url, options) {
2017-11-10 15:55:30 +01:00
return fetch(url, options).then(checkAndParse);
}
return Lbryio.getAuthToken().then(token => {
const fullParams = { auth_token: token, ...params };
const qs = querystring.stringify(fullParams);
let url = `${CONNECTION_STRING}${resource}/${action}?${qs}`;
let options = {
method: 'GET',
};
if (method === 'post') {
options = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: qs,
};
url = `${CONNECTION_STRING}${resource}/${action}`;
}
2017-06-08 02:56:52 +02:00
return makeRequest(url, options).then(response => response.data);
2017-06-08 02:56:52 +02:00
});
};
Lbryio.authToken = null;
Lbryio.getAuthToken = () =>
new Promise(resolve => {
if (Lbryio.authToken) {
resolve(Lbryio.authToken);
} else {
ipcRenderer.once('auth-token-response', (event, token) => {
Lbryio.authToken = token;
return resolve(token);
});
ipcRenderer.send('get-auth-token');
}
});
2017-04-18 21:45:15 +02:00
Lbryio.setAuthToken = token => {
Lbryio.authToken = token ? token.toString().trim() : null;
ipcRenderer.send('set-auth-token', token);
2017-06-08 02:56:52 +02:00
};
Lbryio.getCurrentUser = () => Lbryio.call('user', 'me');
2017-04-09 17:06:23 +02:00
Lbryio.authenticate = () => {
if (!Lbryio.enabled) {
return new Promise(resolve => {
2017-06-01 01:29:10 +02:00
resolve({
id: 1,
language: 'en',
primary_email: 'disabled@lbry.io',
2017-06-08 02:56:52 +02:00
has_verified_email: true,
is_identity_verified: true,
2017-06-08 02:56:52 +02:00
is_reward_approved: false,
});
});
2017-06-01 01:29:10 +02:00
}
if (Lbryio.authenticationPromise === null) {
Lbryio.authenticationPromise = new Promise((resolve, reject) => {
Lbryio.getAuthToken()
.then(token => {
2017-06-21 14:42:22 +02:00
if (!token || token.length > 60) {
return false;
2017-06-08 02:56:52 +02:00
}
// check that token works
return Lbryio.getCurrentUser()
2017-12-13 22:36:30 +01:00
.then(() => true)
.catch(() => false);
2017-06-08 02:56:52 +02:00
})
2017-06-21 14:42:22 +02:00
.then(isTokenValid => {
if (isTokenValid) {
return reject;
}
return Lbry.status()
2017-12-13 22:36:30 +01:00
.then(status =>
Lbryio.call(
'user',
'new',
2017-08-24 21:07:16 +02:00
{
auth_token: '',
language: 'en',
2017-08-24 21:07:16 +02:00
app_id: status.installation_id,
},
'post'
2017-12-13 22:36:30 +01:00
)
)
.then(response => {
if (!response.auth_token) {
throw new Error(__('auth_token is missing from response'));
}
return Lbryio.setAuthToken(response.auth_token);
});
})
.then(Lbryio.getCurrentUser)
.then(resolve, reject);
2017-06-08 02:56:52 +02:00
});
}
return Lbryio.authenticationPromise;
2017-06-06 06:21:55 +02:00
};
2017-04-09 17:06:23 +02:00
Lbryio.getStripeToken = () =>
CONNECTION_STRING.startsWith('http://localhost:')
? 'pk_test_NoL1JWL7i1ipfhVId5KfDZgo'
: 'pk_live_e8M4dRNnCCbmpZzduEUZBgJO';
export default Lbryio;