Created
December 20, 2013 14:46
-
-
Save dominictobias/8055724 to your computer and use it in GitHub Desktop.
Send a query to phabricator using node.js
This file contains hidden or 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
var ApiQuery = function() { | |
this.http = require('http'); | |
this.q = require('q'); | |
this.Connect = require('./conduit.connect'); | |
this.initialize(); | |
}; | |
ApiQuery.prototype = { | |
initialize: function() { | |
this.connect = new this.Connect(); | |
}, | |
send: function(api, params) { | |
params = params || {}; | |
var deferred = this.q.defer(); | |
console.log('Received send:', api, params); | |
this.connect.toFirst().then(function(attrs) { | |
console.log('Succesfully authed'); | |
params.__conduit__ = { | |
connectionID: attrs.connectionID, | |
sessionKey: attrs.sessionKey | |
}; | |
var formData = 'params='+JSON.stringify(params)+'&output=json&__conduit__=1', | |
postOptions = { | |
host: attrs.host.host, | |
port: 80, | |
path: '/api/' + api, | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(formData) | |
} | |
}, | |
postReq = this.http.request(postOptions, function(res) { | |
console.log('Send response:', res); | |
res.setEncoding('utf8'); | |
res.on('error', function(e) { | |
console.log('Error sending post:', e); | |
}); | |
res.on('data', function(data) { | |
console.log('Received send data:', data); | |
deferred.resolve(data); | |
}); | |
}); | |
postReq.end(formData, 'utf8'); | |
}); | |
return deferred.promise; | |
} | |
}; | |
module.exports = ApiQuery; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: formData doesn't use something like
qs.stringify
on the whole object as Phabricator strangely expects only the stuff in params to be in quotes.