Created
October 15, 2016 00:08
-
-
Save rodrigosetti/d42c09383604004e49fb15ccb80daf0e to your computer and use it in GitHub Desktop.
Sherpa Node Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var request = require("request"); | |
var async = require("async"); | |
module.exports = { | |
get : function(sherpaHost, dbName, tableName, ycaProxy, ycaRole, key, callback) { | |
var url = "http://" + sherpaHost + "/YDHTWebService/V1/get/" + dbName + "." + tableName + "/" + key; | |
request({ | |
url: url, | |
method: 'GET', | |
proxy: "http://" + ycaProxy, | |
headers: { | |
'Yahoo-App-Auth': 'a=' + ycaRole | |
}, | |
json: true | |
}, | |
function (error, response, data) { | |
if (error) { | |
console.error(error); | |
} else if (response.statusCode !== 200) { | |
console.error("Received", response.statusCode, response.statusMessage, "status"); | |
console.error(data); | |
} else if (!data || !data.ydht) { | |
console.log("Malformed response data", data); | |
} else if (data.ydht.status.code !== 200) { | |
console.error("Received", data.ydht.status.code, data.ydht.status.message, "status (from payload)"); | |
console.error(data); | |
} else { | |
callback(data.ydht.fields); | |
} | |
}); | |
}, | |
set : function (sherpaHost, dbName, tableName, ycaProxy, ycaRole, key, fields) { | |
var url = "http://" + sherpaHost + "/YDHTWebService/V1/set/" + dbName + "." + tableName + "/" + key; | |
request({ | |
url: url, | |
method: 'POST', | |
proxy: "http://" + ycaProxy, | |
headers: { | |
'Yahoo-App-Auth': 'a=' + ycaRole | |
}, | |
json: { ydht: { fields: fields } } | |
}, | |
function (error, response, data) { | |
if (error) { | |
console.error(error); | |
} else if (response.statusCode !== 200) { | |
console.error("Received", response.statusCode, response.statusMessage, "status"); | |
console.error(data); | |
} else if (!data || !data.ydht) { | |
console.log("Malformed response data", data); | |
} else if (data.ydht.status.code !== 200) { | |
console.error("Received", data.ydht.status.code, data.ydht.status.message, "status (from payload)"); | |
console.error(data); | |
} | |
}); | |
}, | |
delete: function (sherpaHost, dbName, tableName, ycaProxy, ycaRole, key) { | |
var url = "http://" + sherpaHost + "/YDHTWebService/V1/delete/" + dbName + "." + tableName + "/" + key; | |
request({ | |
url: url, | |
method: 'DELETE', | |
proxy: "http://" + ycaProxy, | |
headers: { | |
'Yahoo-App-Auth': 'a=' + ycaRole | |
}, | |
json: true | |
}, | |
function (error, response, data) { | |
if (error) { | |
console.error(error); | |
} else if (response.statusCode !== 200) { | |
console.error("Received", response.statusCode, response.statusMessage, "status"); | |
console.error(data); | |
} else if (!data || !data.ydht) { | |
console.log("Malformed response data", data); | |
} else if (data.ydht.status.code !== 200) { | |
console.error("Received", data.ydht.status.code, data.ydht.status.message, "status (from payload)"); | |
console.error(data); | |
} | |
}); | |
}, | |
hashScan : function (sherpaHost, dbName, tableName, ycaProxy, ycaRole, recordCallBack, doneCallback) { | |
var url = "http://" + sherpaHost + "/YDHTWebService/V1/hash_scan/" + dbName + "." + tableName; | |
var scanCompleted = false; | |
var startHashKey = "0x00000000"; | |
var endHashKey = "0xFFFFFFFF"; | |
async.until(function () { return scanCompleted; }, | |
function (callback) { | |
console.log("scanning from", startHashKey); | |
request({ | |
url: url, | |
method: 'POST', | |
proxy: "http://" + ycaProxy, | |
headers: { | |
'X-DHT-Start-Hash-Key': startHashKey, | |
'Yahoo-App-Auth': 'a=' + ycaRole | |
}, | |
json: { | |
ydht: { | |
continuation: { | |
start_hash_key: startHashKey, | |
end_hash_key: endHashKey | |
}, | |
record_limit: 100, | |
byte_limit: 1000000 | |
} | |
} | |
}, | |
function (error, response, data) { | |
if (error) { | |
console.error(error); | |
callback(error); | |
} else if (response.statusCode !== 200) { | |
console.error("Received", response.statusCode, response.statusMessage, "status"); | |
console.error(data); | |
callback(response.statusCode); | |
} else if (!data || !data.ydht || !data.ydht.records) { | |
console.log("Malformed response data", data); | |
callback(data); | |
} else if (data.ydht.status.code !== 200) { | |
console.error("Received", data.ydht.status.code, data.ydht.status.message, "status (from payload)"); | |
console.error(data); | |
callback(data.ydht.status.code); | |
} else { | |
var recordKeys = Object.keys(data.ydht.records); | |
recordKeys.forEach(function (key) { | |
recordCallBack(key, data.ydht.records[key]); | |
}); | |
scanCompleted = data.ydht.continuation.scan_completed; | |
if (!scanCompleted) { | |
startHashKey = data.ydht.continuation.start_hash_key; | |
endHashKey = data.ydht.continuation.end_hash_key || endHashKey; | |
} | |
callback(); | |
} | |
}); | |
}, | |
doneCallback); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment