Node.js中Box View API的实现给出了202

时间:2014-02-21 15:32:31

标签: node.js ember.js box-api box-view-api

我正在构建新的Box View API的节点实现,每次上传文档并检索会话时,我都会得到202。但是,如果我打一个卷发电话,我就不会得到202.还有其他人遇到过这个问题吗?

这是我的Ember实施:

export default Ember.View.extend({
  document: null,
  documentID: null,
  session: null,
  sessionID: null,

  getDocument: function() {
    var self = this;

    return Ember.$.ajax({ 
      url: 'http://localhost:3000/doc', 
      type: 'POST',
      contentType: 'application/json',
      dataType: 'json',
      data: JSON.stringify({ "docURL": this.textField.value })
    }).then(function(response){
      self.set('document', response);
      self.set('documentID', response.document_id);
    });
  },

  getSession: function() {
    var self = this;

    return Ember.$.ajax({
      url: 'http://localhost:3000/sess/',
      type: 'POST',
      contentType: 'application/json',
      data: JSON.stringify({ "docID": this.get('documentID') })
    }).
    then(function(response) {
      self.set('session', response);
      self.set('sessionID', response.session_id);
    });
  }.observes('documentID'),

  actions: {
    upload: function() {
      this.getDocument();
    }
  }
});

这是我的节点实现:

var https = require('https');
var requestCount = 0;

exports.doc = function(req, res) {
  var docURL = req.body.docURL;
  var httpReq;
  var opts = {
    hostname: 'view-api.box.com',
    path: '/1/documents',
    method: 'POST',
    headers: { 'Content-Type': 'application/json', 'Authorization': 'Token <my token>' }
  };

  res.header('Access-Control-Allow-Origin', '*');

  httpReq = https.request(opts, function(preq, pres) {
    var output = '';

    preq.on('data', function(chunk) {
      output += chunk;
    });

    preq.on('end', function() {
      output = JSON.parse(output);

      output.document_id = output.id;
      delete output.id;

      res.json(output);
    });
  });

  httpReq.write(JSON.stringify({ "url": docURL }));
  httpReq.end();
};

exports.sess = getSession;

function getSession(req, res) {
  var docID = req.body.docID;
  var httpReq;
  var opts = {
    hostname: 'view-api.box.com',
    path: '/1/sessions',
    method: 'POST',
    headers: { 'Content-Type': 'application/json', 'Authorization': 'Token <my token>' }
  };

  res.header('Access-Control-Allow-Origin', '*');

  httpReq = https.request(opts, function(preq, pres) {
    var output = '';

    if(preq.statusCode === 202) {
      setTimeout(function() {
        console.log('Retrying Request :: Count(' + requestCount + ')');
        if (requestCount >= 3) {
          res.json({ 'error': "Retry Again.", 'time': preq.headers['retry-after'] });
          return;
        }

        getSession(req, res);
        requestCount += 1;
      }, 2000);
      return;
    }

    preq.on('data', function(chunk) {
      output += chunk;
    });

    preq.on('end', function() {
      console.log('Successful Request!');
      requestCount = 0;
      output = JSON.parse(output);

      output.session_id = output.id;
      delete output.id;

      res.json(output);
    });
  });

  httpReq.write(JSON.stringify({ "document_id": docID, "duration": 60 }));
  httpReq.end();
}

但现在我收到了这个错误。是否有可以帮助我删除上传文档的用户界面?

{
  "message": "You have exceeded your document upload rate-limit.",
  "type": "error",
  "request_id": "49f8b480b304496987b8cf21f5850c90"
}

1 个答案:

答案 0 :(得分:1)

对于会话,您可以使用正确的方法进行重试。

您看到的速率限制实际上是由于View API测试版的2个文档速率限制。有关详细信息,请参阅FAQ

您可以使用webhooks在文档完成转换时收到通知(允许您上传另一个文档),这样您就不必轮询/documents端点的状态。