2014-02-21 62 views
0

我目前正在構建新的Box View API的節點實現,每當我上傳文檔並檢索會話時,我都會收到202。但是,如果我打了一個捲曲電話,我沒有收到202.有沒有其他人遇到這個問題?在Node.js中實現Box View API給出了202

這裏是我的灰燼實施:

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" 
} 
+0

當您在'retry-after'指示的時間量後重試請求時會發生什麼? –

+0

@JohnHoerr我能夠調整節點代碼重試(上面編輯)。不過,我現在正在收到上面提到的錯誤。 – alvincrespo

+0

@JohnHoerr現在我收到一個限速問題。有沒有可以訪問的用戶界面來刪除這些文檔? – alvincrespo

回答

1

對於會話,您有正確的方法進行重試。

您看到的速率限制實際上是由於View API測試版的2文檔速率限制。有關更多信息,請參閱FAQ

當文檔完成轉換(允許您上傳另一個文檔)時,您可以使用webhooks進行通知,因此您不必輪詢/documents端點的狀態。