2017-04-10 14:32:40 +02:00
import { getLocal , getSession , setSession , setLocal } from './utils.js' ;
2017-04-09 17:06:23 +02:00
import lbry from './lbry.js' ;
2017-03-30 00:44:18 +02:00
const querystring = require ( 'querystring' ) ;
2017-04-09 17:06:23 +02:00
const lbryio = {
_accessToken : getLocal ( 'accessToken' ) ,
_authenticationPromise : null ,
2017-04-15 00:23:42 +02:00
_user : null ,
2017-04-27 05:54:53 +02:00
enabled : false
2017-04-17 16:01:33 +02:00
} ;
2017-03-30 00:44:18 +02:00
2017-04-27 05:54:53 +02:00
// const CONNECTION_STRING = process.env.LBRY_APP_API_URL ? process.env.LBRY_APP_API_URL : 'https://api.lbry.io/';
const CONNECTION _STRING = 'https://api.lbry.io/' ;
2017-04-17 18:18:33 +02:00
const EXCHANGE _RATE _TIMEOUT = 20 * 60 * 1000 ;
2017-03-30 00:44:18 +02:00
2017-04-17 18:18:33 +02:00
lbryio . getExchangeRates = function ( ) {
2017-03-30 00:44:18 +02:00
return new Promise ( ( resolve , reject ) => {
2017-04-17 18:18:33 +02:00
const cached = getSession ( 'exchangeRateCache' ) ;
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 ) ;
}
} ) ;
}
2017-04-17 22:22:08 +02:00
lbryio . call = function ( resource , action , params = { } , method = 'get' , evenIfDisabled = false ) { // evenIfDisabled is just for development, when we may have some calls working and some not
2017-04-17 18:18:33 +02:00
return new Promise ( ( resolve , reject ) => {
if ( ! lbryio . enabled && ! evenIfDisabled && ( resource != 'discover' || action != 'list' ) ) {
2017-04-18 21:45:15 +02:00
reject ( new Error ( "LBRY internal API is disabled" ) )
2017-04-15 00:23:42 +02:00
return
}
2017-04-17 18:18:33 +02:00
2017-03-30 00:44:18 +02:00
const xhr = new XMLHttpRequest ;
2017-04-01 08:54:56 +02:00
2017-04-09 17:06:23 +02:00
xhr . addEventListener ( 'error' , function ( event ) {
reject ( new Error ( "Something went wrong making an internal API call." ) ) ;
2017-03-30 00:44:18 +02:00
} ) ;
xhr . addEventListener ( 'timeout' , function ( ) {
reject ( new Error ( 'XMLHttpRequest connection timed out' ) ) ;
} ) ;
xhr . addEventListener ( 'load' , function ( ) {
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 ) {
2017-04-18 21:45:15 +02:00
let error = new Error ( response . error ) ;
error . xhr = xhr ;
reject ( error ) ;
2017-03-30 00:44:18 +02:00
} 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-03-31 01:00:33 +02:00
resolve ( response . data ) ;
2017-03-30 00:44:18 +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:
2017-04-18 21:45:15 +02:00
const fullParams = { app _id : lbryio . getAccessToken ( ) , ... params } ;
2017-04-02 11:55:24 +02:00
2017-03-31 01:00:33 +02:00
if ( method == 'get' ) {
2017-04-02 11:55:24 +02:00
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' ) {
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
} ) ;
} ;
2017-04-18 21:45:15 +02:00
lbryio . getAccessToken = ( ) => {
return getLocal ( 'accessToken' ) ;
}
2017-04-09 17:06:23 +02:00
lbryio . setAccessToken = ( token ) => {
setLocal ( 'accessToken' , token )
}
2017-04-10 14:32:40 +02:00
lbryio . authenticate = function ( ) {
2017-04-15 00:23:42 +02:00
if ( ! lbryio . enabled ) {
return new Promise ( ( resolve , reject ) => {
resolve ( {
ID : 1 ,
HasVerifiedEmail : true
} )
} )
}
2017-04-09 17:06:23 +02:00
if ( lbryio . _authenticationPromise === null ) {
lbryio . _authenticationPromise = new Promise ( ( resolve , reject ) => {
2017-04-17 22:45:51 +02:00
lbry . status ( ) . then ( ( response ) => {
2017-04-09 17:06:23 +02:00
2017-04-17 22:45:51 +02:00
let installation _id = response . installation _id ;
2017-04-09 17:06:23 +02:00
function setCurrentUser ( ) {
lbryio . call ( 'user' , 'me' ) . then ( ( data ) => {
lbryio . user = data
resolve ( data )
} ) . catch ( function ( err ) {
lbryio . setAccessToken ( null ) ;
2017-04-10 14:32:40 +02:00
if ( ! getSession ( 'reloadedOnFailedAuth' ) ) {
setSession ( 'reloadedOnFailedAuth' , true )
window . location . reload ( ) ;
} else {
reject ( err ) ;
}
2017-04-09 17:06:23 +02:00
} )
}
2017-04-18 21:45:15 +02:00
if ( ! lbryio . getAccessToken ( ) ) {
2017-04-09 17:06:23 +02:00
lbryio . call ( 'user' , 'new' , {
language : 'en' ,
app _id : installation _id ,
} , 'post' ) . then ( function ( responseData ) {
if ( ! responseData . ID ) {
reject ( new Error ( "Received invalid authentication response." ) ) ;
}
lbryio . setAccessToken ( installation _id )
setCurrentUser ( )
} ) . catch ( function ( error ) {
/ *
until we have better error code format , assume all errors are duplicate application id
if we ' re wrong , this will be caught by later attempts to make a valid call
* /
lbryio . setAccessToken ( installation _id )
setCurrentUser ( )
} )
} else {
setCurrentUser ( )
}
} ) . catch ( reject ) ;
} ) ;
}
return lbryio . _authenticationPromise ;
}
2017-03-30 00:44:18 +02:00
export default lbryio ;