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