Bug 748215 - Switch YouSendIt to use v2 user info API. r=mconley.
authorDavid Bienvenu <dbienvenu@mozilla.com>
Thu, 26 Apr 2012 14:03:13 -0400
changeset 10022 def8ce3a54c275bf1df7df3f220311a209166ac8
parent 10021 edf201d1de4bf8bb5867a9c0c128177a9586081d
child 10023 aa2610f9224fb974a5d0d861a15c6245692f4903
push id7627
push usermconley@mozilla.com
push dateThu, 26 Apr 2012 18:04:30 +0000
treeherdercomm-central@def8ce3a54c2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley
bugs748215
Bug 748215 - Switch YouSendIt to use v2 user info API. r=mconley.
mail/components/cloudfile/nsYouSendIt.js
mail/test/mozmill/shared-modules/test-cloudfile-yousendit-helpers.js
--- a/mail/components/cloudfile/nsYouSendIt.js
+++ b/mail/components/cloudfile/nsYouSendIt.js
@@ -15,17 +15,17 @@ Cu.import("resource://gre/modules/Servic
 Cu.import("resource:///modules/gloda/log4moz.js");
 Cu.import("resource:///modules/cloudFileAccounts.js");
 
 var gServerUrl = "https://dpi.yousendit.com"; // Production url
 // test url var gServerUrl = "https://test2-api.yousendit.com";
 
 const kApiKey = "7spvjdt7m4kycr7jyhywrdn2";
 const kAuthPath = "/dpi/v1/auth";
-const kUserInfoPath = "/dpi/v1/user";
+const kUserInfoPath = "/dpi/v2/user";
 const kItemPath = "/dpi/v1/item/";
 const kItemSendPath = kItemPath + "send";
 const kItemCommitPath = kItemPath + "commit";
 
 function nsYouSendIt() {
   this.log = Log4Moz.getConfiguredLogger("YouSendIt");
 }
 
@@ -52,16 +52,17 @@ nsYouSendIt.prototype = {
   _userInfo: null,
   _file : null,
   _requestDate: null,
   _successCallback: null,
   _request: null,
   _maxFileSize : -1,
   _fileSpaceUsed : -1,
   _availableStorage : -1,
+  _totalStorage : -1,
   _lastErrorStatus : 0,
   _lastErrorText : "",
   _uploadingFile : null,
   _uploader : null,
   _urlsForFiles : {},
   _uploadInfo : {},
   _uploads: [],
 
@@ -226,23 +227,26 @@ nsYouSendIt.prototype = {
    * A private function for dealing with stale tokens.  Attempts to refresh
    * the token without prompting for the password.
    *
    * @param aSuccessCallback called if token refresh is successful.
    * @param aFailureCallback called if token refresh fails.
    */
   _handleStaleToken: function nsYouSendIt__handleStaleToken(aSuccessCallback,
                                                             aFailureCallback) {
+    this.log.info("Handling a stale token.");
     this._loggedIn = false;
+    this._cachedAuthToken = "";
     if (this.getPassword(this._userName, true) != "") {
       this.log.info("Attempting to reauth with saved password");
       // We had a stored password - let's try logging in with that now.
       this.logon(aSuccessCallback, aFailureCallback,
                  false);
     } else {
+      this.log.info("No saved password stored, so we can't refresh the token silently.");
       aFailureCallback();
     }
   },
 
   /**
    * A private function for retrieving profile information about a user.
    *
    * @param successCallback a callback fired if retrieving profile information
@@ -263,33 +267,42 @@ nsYouSendIt.prototype = {
         this.log.info("request status = " + req.status +
                       " response = " + req.responseText);
         let docResponse = JSON.parse(req.responseText);
         this.log.info("user info response parsed = " + docResponse);
         if (docResponse.errorStatus)
           this.log.info("error status = " + docResponse.errorStatus.code);
 
         if (docResponse.errorStatus && docResponse.errorStatus.code > 200) {
-          if (docResponse.errorStatus.code == 500) {
+          if (docResponse.errorStatus.code > 400) {
             // Our token has gone stale
+            this.log.info("Our token has gone stale - requesting a new one.");
 
             let retryGetUserInfo = function() {
               this._getUserInfo(successCallback, failureCallback);
             }.bind(this);
 
             this._handleStaleToken(retryGetUserInfo, failureCallback);
             return;
           }
 
           failureCallback();
           return;
         }
         this._userInfo = docResponse;
         let account = docResponse.account;
-        this._availableStorage = account.availableStorage;
+        let storage = docResponse.storage;
+        if (storage) {
+          this._fileSpaceUsed = storage.currentUsage;
+          this._availableStorage = storage.storageQuota - this._fileSpaceUsed;
+        }
+        else {
+          this._availableStorage = account.availableStorage;
+        }
+
         this._maxFileSize = account.maxFileSize;
         this.log.info("available storage = " + this._availableStorage + " max file size = " + this._maxFileSize);
         successCallback();
       }
       else {
         failureCallback();
       }
     }.bind(this);
--- a/mail/test/mozmill/shared-modules/test-cloudfile-yousendit-helpers.js
+++ b/mail/test/mozmill/shared-modules/test-cloudfile-yousendit-helpers.js
@@ -16,17 +16,17 @@ Cu.import('resource://mozmill/stdlib/htt
 Cu.import('resource://gre/modules/Services.jsm');
 Cu.import('resource:///modules/cloudFileAccounts.js');
 
 const kDefaultServerPort = 4444;
 const kServerRoot = "http://localhost:" + kDefaultServerPort;
 const kServerPath = "";
 const kServerURL = kServerRoot + kServerPath;
 const kAuthPath = "/dpi/v1/auth";
-const kUserInfoPath = "/dpi/v1/user";
+const kUserInfoPath = "/dpi/v2/user";
 const kFilePreparePath = "/dpi/v1/item/send";
 const kDefaultFileUploadPath = "/uploads";
 const kCommitPath = "/dpi/v1/item/commit";
 const kDeletePath = "/dpi/v1/item";
 
 const kDownloadURLPrefix = "http://www.example.com/downloads";
 
 const kAuthResult = {
@@ -65,16 +65,20 @@ const kDefaultUser = {
     dropboxUrl: null,
     knowledgeBase: "Yes",
     maxDownloadBWpermonth: "1073741824",
     maxFileDownloads: "100",
     maxFileSize: "104857600",
     premiumDelivery: null,
     verifyRecipientIdentity: "Included"
   },
+  storage: {
+    currentUsage: 1045,
+    storageQuota: 2147483648
+  },
   status: null,
   errorStatus: null,
 };
 
 const kDefaultFilePrepare = {
   itemId: "",
   uploadUrl: [
   ],