separated lbry api calls away from controller routes
This commit is contained in:
parent
c2941a6619
commit
11f31b2e12
3 changed files with 89 additions and 104 deletions
|
@ -7,15 +7,41 @@ module.exports = {
|
|||
axios.post('http://localhost:5279/lbryapi', {
|
||||
"method": "publish",
|
||||
"params": publishParams
|
||||
})
|
||||
.then(function (response) {
|
||||
}).then(function (response) {
|
||||
console.log(">> 'publish' success");
|
||||
resolve(response.data);
|
||||
}).catch(function(error){
|
||||
console.log(">> 'publish' error");
|
||||
reject(error);
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
getClaim: function(uri){ // note: move to lbryApi
|
||||
var deferred = new Promise(function(resolve, reject){
|
||||
console.log(">> making get request to lbry daemon");
|
||||
axios.post('http://localhost:5279/lbryapi', {
|
||||
"method": "get",
|
||||
"params": { "uri": uri }
|
||||
}).then(function (getResponse) {
|
||||
console.log(">> 'get claim' success...");
|
||||
//check to make sure the daemon didn't just time out (or otherwise send an error that appears to be a success?)
|
||||
// if (getResponse.data.result.error){
|
||||
// reject(getResponse.data.result.error);
|
||||
// }
|
||||
console.log(">> response data:", getResponse.data);
|
||||
// resolve the promise with the download path for the claim we got
|
||||
/*
|
||||
note: put in a check to make sure we do not resolve until the download is actually complete (total_bytes should match written_bytes)
|
||||
*/
|
||||
resolve(getResponse.data);
|
||||
}).catch(function(getUriError){
|
||||
console.log(">> 'get' error.");
|
||||
// reject the promise with an error message
|
||||
reject(getUriError);
|
||||
return;
|
||||
});
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
getClaimsList: function(claimName){
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
var path = require('path');
|
||||
var axios = require('axios');
|
||||
var lbryApi = require('./lbryApi');
|
||||
|
||||
function filterForFreePublicClaims(claimsListArray){
|
||||
if (!claimsListArray) {
|
||||
|
@ -34,101 +35,14 @@ function orderTopClaims(claimsListArray){
|
|||
return claimsListArray;
|
||||
}
|
||||
|
||||
function getClaimWithUri(uri, resolve, reject){
|
||||
console.log(">> making get request to lbry daemon");
|
||||
axios.post('http://localhost:5279/lbryapi', {
|
||||
"method": "get",
|
||||
"params": { "uri": uri }
|
||||
}
|
||||
).then(function (getUriResponse) {
|
||||
console.log(">> 'get claim' success...");
|
||||
//check to make sure the daemon didn't just time out
|
||||
if (getUriResponse.data.result.error){
|
||||
reject(getUriResponse.data.result.error);
|
||||
}
|
||||
console.log(">> response data:", getUriResponse.data);
|
||||
console.log(">> dl path =", getUriResponse.data.result.download_path)
|
||||
// resolve the promise with the download path for the claim we got
|
||||
/*
|
||||
note: put in a check to make sure we do not resolve until the download is actually complete
|
||||
*/
|
||||
resolve(getUriResponse.data.result.download_path);
|
||||
}).catch(function(getUriError){
|
||||
console.log(">> 'get' error.");
|
||||
// reject the promise with an error message
|
||||
reject(getUriError);
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
function findAllClaims(name, resolve, reject){
|
||||
// to do: abstract claim_list function to here
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getClaimBasedOnNameOnly: function(claimName){
|
||||
var deferred = new Promise(function (resolve, reject){
|
||||
// make a call to the daemon to get the claims list
|
||||
axios.post('http://localhost:5279/lbryapi', {
|
||||
"method": "claim_list",
|
||||
"params": { "name": claimName }
|
||||
})
|
||||
.then(function (response) {
|
||||
console.log(">> 'claim_list' success");
|
||||
var claimsList = response.data.result.claims;
|
||||
console.log(">> Number of claims:", claimsList.length)
|
||||
// return early if no claims were found
|
||||
if (claimsList.length === 0){
|
||||
reject("NO_CLAIMS");
|
||||
console.log("exiting due to lack of claims");
|
||||
return;
|
||||
}
|
||||
// filter the claims to return only free, public claims
|
||||
var freePublicClaims = filterForFreePublicClaims(claimsList);
|
||||
// return early if no free, public claims were found
|
||||
if (!freePublicClaims || (freePublicClaims.length === 0)){
|
||||
reject("NO_FREE_PUBLIC_CLAIMS");
|
||||
console.log("exiting due to lack of free or public claims");
|
||||
return;
|
||||
}
|
||||
// order the claims
|
||||
var orderedPublicClaims = orderTopClaims(freePublicClaims);
|
||||
// create the uri for the first (selected) claim
|
||||
console.log(">> ordered free public claims");
|
||||
var freePublicClaimUri = orderedPublicClaims[0].name + "#" + orderedPublicClaims[0].claim_id;
|
||||
console.log(">> your free public claim URI:", freePublicClaimUri);
|
||||
// fetch the image to display
|
||||
getClaimWithUri(freePublicClaimUri, resolve, reject);
|
||||
})
|
||||
.catch(function(error){
|
||||
console.log(">> 'claim_list' error.");
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
getClaimBasedOnUri: function(uri){
|
||||
/*
|
||||
to do: need to pass the URI through a test (use 'resolve') to see if it is free and public. Right now it is jumping straight to 'get'ing and serving the asset.
|
||||
*/
|
||||
var deferred = new Promise(function (resolve, reject){
|
||||
console.log(">> get claim based on URI:", uri);
|
||||
// fetch the image to display
|
||||
getClaimWithUri(uri, resolve, reject);
|
||||
});
|
||||
return deferred;
|
||||
|
||||
},
|
||||
getAllFreePublicClaims: function(claimName, res){ // note: work in progress
|
||||
function getAllFreePublicClaims(claimName){ // note: work in progress
|
||||
var deferred = new Promise(function(resolve, reject){
|
||||
console.log(">> get all claims data for", claimName)
|
||||
// make a call to the daemon to get the claims list
|
||||
axios.post('http://localhost:5279/lbryapi', {
|
||||
method: "claim_list",
|
||||
params: { name: claimName }
|
||||
}).then(function (response) {
|
||||
lbryApi.getClaimsList(claimName)
|
||||
.then(function(data){
|
||||
console.log(">> 'claim_list' success");
|
||||
var claimsList = response.data.result.claims;
|
||||
var claimsList = data.result.claims;
|
||||
console.log(">> Number of claims:", claimsList.length)
|
||||
// return early if no claims were found
|
||||
if (claimsList.length === 0){
|
||||
|
@ -154,5 +68,50 @@ module.exports = {
|
|||
});
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getClaimBasedOnNameOnly: function(claimName){
|
||||
var deferred = new Promise(function (resolve, reject){
|
||||
console.log(">> get all free claims for", claimName);
|
||||
// promise to get all free public claims
|
||||
getAllFreePublicClaims(claimName)
|
||||
.then(function(freePublicClaimList){
|
||||
var freePublicClaimUri = freePublicClaimList[0].name + "#" + freePublicClaimList[0].claim_id;
|
||||
console.log(">> your free public claim URI:", freePublicClaimUri);
|
||||
// promise to get the chosen uri
|
||||
lbryApi.getClaim(freePublicClaimUri)
|
||||
.then(function(data){
|
||||
console.log(">> dl path =", data.result.download_path)
|
||||
resolve(data.result.download_path)
|
||||
}).catch(function(error){
|
||||
reject(error)
|
||||
});
|
||||
}).catch(function(error){
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
|
||||
getClaimBasedOnUri: function(uri){
|
||||
/*
|
||||
to do: need to pass the URI through a test to see if it is free and public. Right now it is jumping straight to 'get'ing and serving the asset.
|
||||
*/
|
||||
var deferred = new Promise(function (resolve, reject){
|
||||
console.log(">> get claim based on URI:", uri);
|
||||
// promise to get the chosen uri
|
||||
lbryApi.getClaim(uri)
|
||||
.then(function(data){
|
||||
console.log(">> dl path =", data.result.download_path)
|
||||
resolve(data.result.download_path)
|
||||
}).catch(function(error){
|
||||
reject(error)
|
||||
});
|
||||
});
|
||||
return deferred;
|
||||
},
|
||||
getAllClaims: function(claimName){ // note: work in progress
|
||||
return getAllFreePublicClaims(claimName);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId)
|
|||
console.log(">> GET request on /" + req.params.name + " (all)");
|
||||
ua(googleAnalyticsId, {https: true}).event("Show Routes", "/name/all", req.params.name + "/all").send();
|
||||
// create promise
|
||||
lbryHelpers.getAllFreePublicClaims(req.params.name)
|
||||
lbryHelpers.getAllClaims(req.params.name)
|
||||
.then(function(orderedFreePublicClaims){
|
||||
console.log("/:name/all success.");
|
||||
res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
|
||||
|
|
Loading…
Reference in a new issue