2017-03-30 00:44:18 +02:00
|
|
|
const querystring = require('querystring');
|
|
|
|
|
|
|
|
const lbryio = {};
|
|
|
|
|
2017-03-31 01:00:33 +02:00
|
|
|
const CONNECTION_STRING = 'https://apidev.lbry.tech/';
|
2017-03-30 00:44:18 +02:00
|
|
|
|
|
|
|
const mocks = {
|
2017-04-02 12:08:25 +02:00
|
|
|
'reward_type.get': ({name}) => {
|
2017-03-30 00:44:18 +02:00
|
|
|
return {
|
|
|
|
name: 'link_github',
|
|
|
|
title: 'Link your GitHub account',
|
|
|
|
description: 'Link LBRY to your GitHub account',
|
|
|
|
value: 50,
|
|
|
|
claimed: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
'reward_type.list': () => {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
name: 'link_github',
|
|
|
|
title: 'Link your GitHub account',
|
|
|
|
description: 'Link LBRY to your GitHub account',
|
|
|
|
value: 50,
|
|
|
|
claimed: false,
|
|
|
|
},
|
|
|
|
];
|
2017-04-02 12:07:04 +02:00
|
|
|
},
|
2017-03-30 00:44:18 +02:00
|
|
|
};
|
|
|
|
|
2017-04-02 12:08:25 +02:00
|
|
|
lbryio.call = function(resource, action, params={}, method='get') {
|
2017-03-30 00:44:18 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
/* temp code for mocks */
|
|
|
|
if (`${resource}.${action}` in mocks) {
|
|
|
|
resolve(mocks[`${resource}.${action}`](params));
|
|
|
|
return;
|
|
|
|
}
|
2017-03-30 06:48:18 +02:00
|
|
|
|
2017-03-30 00:44:18 +02:00
|
|
|
/* end temp */
|
|
|
|
|
|
|
|
console.log('about to create xhr object');
|
|
|
|
const xhr = new XMLHttpRequest;
|
2017-04-01 08:54:56 +02:00
|
|
|
|
2017-03-30 00:44:18 +02:00
|
|
|
xhr.addEventListener('error', function (error) {
|
|
|
|
console.log('received XHR error:', error);
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
console.log('about to add timeout listener');
|
|
|
|
xhr.addEventListener('timeout', function() {
|
|
|
|
console.log('XHR timed out');
|
|
|
|
|
|
|
|
reject(new Error('XMLHttpRequest connection timed out'));
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('about to create load listener');
|
|
|
|
xhr.addEventListener('load', function() {
|
|
|
|
console.log('loaded');
|
|
|
|
const response = JSON.parse(xhr.responseText);
|
|
|
|
|
2017-03-30 20:05:37 +02:00
|
|
|
if (!response.success) {
|
2017-03-30 00:44:18 +02:00
|
|
|
if (reject) {
|
|
|
|
reject(new Error(response.error));
|
|
|
|
} else {
|
|
|
|
document.dispatchEvent(new CustomEvent('unhandledError', {
|
|
|
|
detail: {
|
|
|
|
connectionString: connectionString,
|
2017-03-30 20:05:37 +02:00
|
|
|
method: action,
|
2017-03-30 00:44:18 +02:00
|
|
|
params: params,
|
|
|
|
message: response.error.message,
|
2017-03-30 20:05:37 +02:00
|
|
|
... response.error.data ? {data: response.error.data} : {},
|
2017-03-30 00:44:18 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-01 08:52:45 +02:00
|
|
|
console.info(`${resource}.${action} response data:`, response);
|
2017-03-31 01:00:33 +02:00
|
|
|
resolve(response.data);
|
2017-03-30 00:44:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('about to call xhr.open');
|
2017-03-30 20:05:37 +02:00
|
|
|
|
2017-04-02 11:55:24 +02:00
|
|
|
// For social media auth:
|
|
|
|
//const accessToken = localStorage.getItem('accessToken');
|
|
|
|
//const fullParams = {...params, ... accessToken ? {access_token: accessToken} : {}};
|
|
|
|
|
|
|
|
// Temp app ID based auth:
|
|
|
|
const fullParams = {app_id: localStorage.getItem('appId'), ...params};
|
|
|
|
|
2017-03-31 01:00:33 +02:00
|
|
|
if (method == 'get') {
|
2017-04-02 11:55:24 +02:00
|
|
|
console.info('GET ', CONNECTION_STRING + resource + '/' + action, ' | params:', fullParams);
|
|
|
|
xhr.open('get', CONNECTION_STRING + resource + '/' + action + '?' + querystring.stringify(fullParams), true);
|
2017-03-31 01:00:33 +02:00
|
|
|
xhr.send();
|
|
|
|
} else if (method == 'post') {
|
2017-04-02 11:55:24 +02:00
|
|
|
console.info('POST ', CONNECTION_STRING + resource + '/' + action, '| params: ', fullParams);
|
2017-03-31 01:00:33 +02:00
|
|
|
xhr.open('post', CONNECTION_STRING + resource + '/' + action, true);
|
2017-03-30 20:05:37 +02:00
|
|
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
2017-04-02 11:55:24 +02:00
|
|
|
xhr.send(querystring.stringify(fullParams));
|
2017-03-30 20:05:37 +02:00
|
|
|
}
|
2017-03-30 00:44:18 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default lbryio;
|