Skip to content
Browse files

Merge pull request #1 from tjmehta/master

Added ability to add fields to the request sent to TransloadIt.
  • Loading branch information...
2 parents 726a6ad + 9a5c322 commit 4646b308c0ac05bba52450bbd242b89b9cf5de54 @gmwils committed Mar 8, 2012
Showing with 18 additions and 7 deletions.
  1. +18 −7 lib/client.js
View
25 lib/client.js
@@ -8,12 +8,12 @@ var fs = require('fs');
var API_HOST = 'api2.transloadit.com';
var API_PATH = '/assemblies';
-/**
+/**
* Class Client
*
* @param {authKey} string Transloadit API key
* @param {authSecret} string Transloadit API secret
- *
+ *
* @return {Client} object
*/
@@ -31,7 +31,7 @@ var Client = module.exports = function(authKey, authSecret) {
};
var methods = function() {
- /**
+ /**
* Public methods
*/
this.addStream = function(name, file, type, size, stream) {
@@ -52,7 +52,13 @@ var methods = function() {
this.addStream(file_name, file_name, _mime_type, stat.size, stream);
};
- this.send = function(params, success_cb, failure_cb) {
+ this.send = function(params, fields, success_cb, failure_cb) {
+ if (_.isFunction(fields)){
+ failure_cb = success_cb;
+ success_cb = fields;
+ fields = {};
+ }
+
var expires_date = new Date().add({ days: 1 });
if (!params.auth) { params.auth = {}; }
@@ -66,12 +72,17 @@ var methods = function() {
var signature = hmac.digest('hex');
req.setParam("params", json_params);
+ for(var key in fields){
+ if (_.isObject(fields[key]) || _.isArray(fields[key]))
+ req.setParam(key, json.stringify(fields[key]));
+ else
+ req.setParam(key, fields[key]);
+ }
req.setParam("signature", signature);
-
_.each(this.streams, function(value, key) {
req.addStream(key, value.file, value.type, value.length, value.stream);
});
-
+
req.send(function(err, res) {
var body = '';
res.on('data', function (chunk) {
@@ -81,7 +92,7 @@ var methods = function() {
res.on('end', function() {
var result = JSON.parse(body);
// console.log('Final data: ' + JSON.stringify(result));
-
+
if (result.ok) {
if (success_cb) {
success_cb(result);

0 comments on commit 4646b30

Please sign in to comment.
Something went wrong with that request. Please try again.