0

我目前正在构建新 Box View API 的节点实现,每次上传文档并检索会话时都会收到 202。但是,如果我打一个 curl 电话,我不会收到 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();
}

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

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

1 回答 1

1

您对会话进行重试后有正确的方法。

您看到的速率限制实际上是由于 View API 测试版的 2 文档速率限制。有关更多信息,请参阅常见问题解答

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

于 2014-02-21T22:44:59.957 回答