axios working
This commit is contained in:
1 parent
a90c8256ac
commit
28216a08d0
6 files changed
+229
-27918
No files matched your search
@@ -1,7 +1,7 @@
|
||||
exports.newNetworkModulesHttpNetworkClient = function newNetworkModulesHttpNetworkClient() {
|
||||
|
||||
let thisObject = {
|
||||
callerRole: undefined,
|
||||
callerRole: undefined,
|
||||
host: undefined,
|
||||
port: undefined,
|
||||
p2pNetworkNode: undefined,
|
||||
@@ -38,31 +38,151 @@ exports.newNetworkModulesHttpNetworkClient = function newNetworkModulesHttpNetwo
|
||||
thisObject.port = JSON.parse(thisObject.p2pNetworkNode.node.config).webPort
|
||||
}
|
||||
|
||||
function sendMessage(message) {
|
||||
return new Promise(sendHttpMessage)
|
||||
async function sendMessage(message) {
|
||||
|
||||
async function sendHttpMessage(resolve, reject) {
|
||||
try {
|
||||
let signature = web3.eth.accounts.sign(JSON.stringify(message), SA.secrets.map.get(thisObject.p2pNetworkClientCodeName).privateKey)
|
||||
await axios()
|
||||
async function axios() {
|
||||
/*
|
||||
This function helps a caller to use await syntax while the called
|
||||
function uses callbacks, specifically for retrieving files.
|
||||
*/
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
|
||||
let body = {
|
||||
messageId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(),
|
||||
messageType: 'Request',
|
||||
signature: JSON.stringify(signature),
|
||||
payload: JSON.stringify(message)
|
||||
}
|
||||
const fetch = SA.nodeModules.nodeFetch
|
||||
const response = await fetch('https://' + thisObject.host + ':' + thisObject.port + '/' + 'New-Signal', {
|
||||
method: 'post',
|
||||
body: JSON.stringify(body),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
const data = await response.json();
|
||||
resolve(data)
|
||||
} catch (err) {
|
||||
console.log('[ERROR] Http Network Client -> err.message = ' + err.message)
|
||||
reject(err.description)
|
||||
const axios = require('axios')
|
||||
console.log('call axios')
|
||||
axios
|
||||
.post('http://localhost:31248/New-Signal', {
|
||||
messageId: 'La concha de tu madre.'
|
||||
})
|
||||
.then(res => {
|
||||
console.log(`statusCode: ${res.status}`)
|
||||
console.log('ATENTIONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN !!!!!!!!!!!!!!!! ' + res.data)
|
||||
resolve()
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error)
|
||||
reject()
|
||||
})
|
||||
})
|
||||
|
||||
return promise
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
let signature = web3.eth.accounts.sign(JSON.stringify(message), SA.secrets.map.get(thisObject.p2pNetworkClientCodeName).privateKey)
|
||||
|
||||
let body = {
|
||||
messageId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(),
|
||||
messageType: 'Request',
|
||||
signature: JSON.stringify(signature),
|
||||
payload: JSON.stringify(message)
|
||||
}
|
||||
|
||||
const data = JSON.stringify(body)
|
||||
const http = SA.nodeModules.http
|
||||
|
||||
const options = {
|
||||
hostname: thisObject.host,
|
||||
port: thisObject.port,
|
||||
path: '/New-Signal',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': data.length
|
||||
}
|
||||
}
|
||||
|
||||
const req = http.request(options, res => {
|
||||
console.log(`statusCode: ${res.statusCode}`)
|
||||
|
||||
res.on('data', d => {
|
||||
process.stdout.write(d)
|
||||
})
|
||||
})
|
||||
|
||||
req.on('error', error => {
|
||||
console.error(error)
|
||||
})
|
||||
|
||||
req.write(data)
|
||||
req.end()
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
return new Promise(sendHttpMessage)
|
||||
|
||||
async function sendHttpMessage(resolve, reject) {
|
||||
try {
|
||||
let signature = web3.eth.accounts.sign(JSON.stringify(message), SA.secrets.map.get(thisObject.p2pNetworkClientCodeName).privateKey)
|
||||
|
||||
let body = {
|
||||
messageId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(),
|
||||
messageType: 'Request',
|
||||
signature: JSON.stringify(signature),
|
||||
payload: JSON.stringify(message)
|
||||
}
|
||||
|
||||
const data = JSON.stringify(body)
|
||||
const http = SA.nodeModules.http
|
||||
|
||||
const options = {
|
||||
hostname: thisObject.host,
|
||||
port: thisObject.port,
|
||||
path: '/New-Signal',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': data.length
|
||||
}
|
||||
}
|
||||
|
||||
const req = http.request(options, res => {
|
||||
console.log(`statusCode: ${res.statusCode}`)
|
||||
|
||||
res.on('data', d => {
|
||||
process.stdout.write(d)
|
||||
})
|
||||
})
|
||||
|
||||
req.on('error', error => {
|
||||
console.error(error)
|
||||
})
|
||||
|
||||
req.write(data)
|
||||
req.end()
|
||||
|
||||
return
|
||||
|
||||
/*
|
||||
const fetch = SA.nodeModules.nodeFetch
|
||||
const url = 'http://' + thisObject.host + ':' + thisObject.port + '/' + 'New-Signal'
|
||||
const options = {
|
||||
method: 'post',
|
||||
body: JSON.stringify(body),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
}
|
||||
const response = fetch(url)
|
||||
.then(returnData)
|
||||
.catch(onError)
|
||||
|
||||
function onError(errorMessage) {
|
||||
console.log('[ERROR] Error sending http request to P2P Network Node via httpInterdace: ' + errorMessage)
|
||||
}
|
||||
|
||||
async function returnData() {
|
||||
const data = await response.json();
|
||||
resolve(data)
|
||||
}
|
||||
*/
|
||||
/*
|
||||
} catch (err) {
|
||||
console.log('[ERROR] Http Network Client -> err.message = ' + err.message)
|
||||
reject(err.description)
|
||||
}
|
||||
|
||||
|
||||
} */
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user