Example #1
0
 var bacon = (function() {
   var request = rest.post( url, { data: req.body } );
   return Bacon.fromEventTarget(request, 'complete');
 }());
Example #2
0
    this.retry(5000); // try again after 5 sec
  } else {
    sys.puts(result);
  }
});

rest.get('http://twaud.io/api/v1/users/danwrong.json').on('complete', function(data) {
  sys.puts(data[0].message); // auto convert to object
});

rest.get('http://twaud.io/api/v1/users/danwrong.xml').on('complete', function(data) {
  sys.puts(data[0].sounds[0].sound[0].message); // auto convert to object
});

rest.post('http://*****:*****@service.com/action', {
  data: { id: 334 },
}).on('complete', function(data, response) {
  if (response.statusCode == 201) {
    // you can get at the raw response like this...
  }
});

// multipart request sending a 321567 byte long file using https
rest.post('https://twaud.io/api/v1/upload.json', {
  multipart: true,
  username: '******',
  password: '******',
  data: {
    'sound[message]': 'hello from restler!',
    'sound[file]': rest.file('doug-e-fresh_the-show.mp3', null, 321567, null, 'audio/mpeg')
  }
Example #3
0
var ebayApiPostXmlRequest = function ebayApiPostXmlRequest(options, callback) {
  if (! options.serviceName) return callback(new Error("Missing serviceName"));
  if (! options.opType) return callback(new Error("Missing opType"));
  
  
  // @see note in buildXmlInput() re: nested elements
  options.params = options.params || {};
  
  options.reqOptions = options.reqOptions || {};
  options.sandbox = options.sandbox || false;

  // options.parser = options.parser || ...;   // @todo
  
  // converts XML to JSON by default, but can also return raw XML
  options.rawXml = options.rawXml || false;

  
  // app/auth params go into headers (see defaultParams())
  options.reqOptions.headers = options.reqOptions.headers || {};
  _.defaults(options.reqOptions.headers, defaultParams(options));
  // console.dir(options);

  var url = buildRequestUrl(options.serviceName, {}, {}, options.sandbox);
  // console.log('URL:', url);
  
  options.reqOptions.data = buildXmlInput(options.opType, options.params);
  // console.log(options.reqOptions.data);
  
  var request = restler.post(url, options.reqOptions);
  
  request.on('complete', function(result, response) {
    if (result instanceof Error) {
      var error = result;
      error.message = "Completed with error: " + error.message;
      return callback(error);
    }
    else if (response.statusCode !== 200) {
      return callback(new Error(util.format("Bad response status code", response.statusCode, result.toString())));
    }
    
    // raw XML wanted?
    if (options.rawXml) {
      return callback(null, result);
    }

    async.waterfall([

      // convert xml to json
      function toJson(next) {
        var xml2js = require('xml2js'),
            parser = new xml2js.Parser();
        
        parser.parseString(result, function parseXmlCallback(error, data) {
          if (error) {
            error.message = "Error parsing XML: " + error.message;
            return next(error);
          }
          next(null, data);
        });
      },
      

      function parseData(data, next) {
        //// @todo parse the response
        // options.parser(data, next);
        
        next(null, data);
      }
      
    ],
    function(error, data){
      if (error) return callback(error);
      callback(null, data);
    });
    
  });

  // emitted when some errors have occurred
  // either this OR 'completed' should fire
  request.on('error', function(error, response) {
    error.message = "Request error: " + error.message;
    callback(error);
  });
};
Example #4
0
app.post('/viewing', fb.checkSession, fb.getUserDetails, util.fetchOrCreateViewing, function(req, res, next) {

    var fbActions = [],
        fbResponses = [];

    if (req.body.wantToSee) {
        req.viewing.wantToSee = req.body.wantToSee == 'true';

        if (req.viewing.wantToSee) {
            fbActions.push({
                method: 'POST',
                relative_url: req.session.fb.user_id + '/' + config.fbNamespace + ':want_to_watch',
                body: 'movie=http://www.watchlistapp.com/movie/' + req.body.movieId
            });
            fbResponses.push({ key: 'wantToSeeId', value: 'id' });
        } else if (req.viewing.wantToSeeId) {
            fbActions.push({ method: 'DELETE', relative_url: String(req.viewing.wantToSeeId) });
            fbResponses.push({ key: 'wantToSeeId', value: null });
        }
    }

    if (req.body.seen) {
        req.viewing.seen = req.body.seen == 'true';

        if (req.viewing.seen) {
            fbActions.push({
                method: 'POST',
                relative_url: req.session.fb.user_id + '/' + config.fbNamespace + ':watch',
                body: 'movie=http://www.watchlistapp.com/movie/' + req.body.movieId
            });
            fbResponses.push({ key: 'seenId', value: 'id' });
        } else if (req.viewing.seenId) {
            fbActions.push({ method: 'DELETE', relative_url: String(req.viewing.seenId) });
            fbResponses.push({ key: 'seenId', value: 'null'});
        }
    }

    if (req.body.like && req.viewing.seenId) {
        req.viewing.like = req.body.like == 'true';

        if (req.viewing.like) {
            fbActions.push({
                method: 'POST',
                relative_url: String(req.viewing.seenId),
                body: 'rating=1'
            });
            fbResponses.push();
        }
    }

    if (req.body.dislike && req.viewing.seenId) {
        req.viewing.dislike = req.body.dislike == 'true';

        if (req.viewing.dislike) {
            fbActions.push({
                method: 'POST',
                relative_url: String(req.viewing.seenId),
                body: 'rating=-1'
            });
            fbResponses.push();
        }
    }

    if (fbActions.length) {

        console.log("Posting to Facebook Open Graph...", req.session.fb.access_token, fbActions)

        rest.post("https://graph.facebook.com", {
            data: {
                access_token: req.session.fb.access_token,
                batch: JSON.stringify(fbActions)
            }
        }).on('complete', function(str) {

            var data = JSON.parse(str);

            _.each(data, function(batchResponse) {

                var body = JSON.parse(batchResponse.body),
                    takeAction = fbResponses.shift();

                if (body.error) {
                    req.fbError = body.error;
                }

                if (takeAction) {
                    req.viewing[takeAction.key] = body[takeAction.value] || null;
                }

                console.log("Facebook batch complete", body)
            })

            util.saveViewing(req, res, next);
        }).on('error', function(err) {
            console.log('Error with batch request to FB', err);
            util.saveViewing(req, res, next);
        });

    } else {
        util.saveViewing(req, res, next);
    }
});
Example #5
0
 setTimeout(function(){
     rest.post(config.protocol + '://' + hostname + ':' + port + '/queue/' + queue +
         '/pop?timeout=' + timeOut + '&max=' + maxMessages)/*.on('complete', function(data, response){
      //console.log('Pop received: ' + data);
      })*/;
 }, interval);