Last active
May 26, 2020 10:49
-
-
Save williamtoader/9904714f948244cdd5b89f7de80c88b2 to your computer and use it in GitHub Desktop.
nodejs lbry connection script
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
const lbry = require("./node_lbry_2020.js"); | |
let promise = lbry.requestPromise("example_method", { | |
param: "value" | |
}) | |
promise.then( | |
claims => console.log(claims), | |
error => console.error(error) | |
) |
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
const http = require('http'); | |
const lbry ={} | |
const _request = (method_name, params) => { | |
let requestPromise = new Promise((resolve, reject) => { | |
let post_data = { | |
method: method_name | |
} | |
if (params) { | |
post_data.params = params | |
} | |
let post_data_string = JSON.stringify(post_data) | |
const options = { | |
hostname: '127.0.0.1', | |
port: '5279', | |
path: '/lbryapi', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(post_data_string) | |
} | |
} | |
const req = http.request(options, (res) => { | |
console.log(`statusCode: ${res.statusCode}`) | |
res.on('data', (data) => { | |
resolve(data.toString()) | |
}) | |
}) | |
req.on('error', (error) => { | |
reject(error) | |
}) | |
req.write(post_data_string); | |
req.end() | |
}) | |
return requestPromise | |
} | |
lbry.requestPromise = _request | |
module.exports = lbry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment