<commit_before>
var ICA = {
  api: "api/v1"
};

ICA.login = function () {
  return new Promise(function (resolve, reject) {
    var oauthWindow = window.open("oauth2login", "Log in via MintKit", "location=0,status=0,width=600,height=700");

    if (oauthWindow) {
      var timer = setInterval(function () {
        if (oauthWindow.closed) {
          clearInterval(timer);

          if (new Date().getTime() - window.sessionStorage.getItem("_ica_oauth2_timestamp") < 1000) {
            resolve();
          } else {
            reject(new Error("Error synchronizing/failed to log in"));
          }
        }
      }, 1000);
    } else {
      console.error("Failed to open popup window");
    }
  });
};

Object.defineProperty(ICA, "accountId", {
  get: function () {
    return window.sessionStorage.getItem("_ica_account_id");
  }
});

Object.defineProperty(ICA, "accountSession", {
  get: function () {
    return window.sessionStorage.getItem("_ica_account_session");
  }
});

ICA.request = function (method, url, headers, data, responseType = "json", returnXHR = false) {
  return new Promise(function (resolve, reject) {
    var x = new XMLHttpRequest();
    x.open(method, url, true);
    if (responseType && responseType != "x") x.responseType = responseType;
    if (x.upload) x.upload.onprogress = function (e) {
      if (e.lengthComputable) {
        var percentComplete = (e.loaded / e.total) * 100;
        console.log("Requesting: {0}%".format(percentComplete));
      }
    };
    x.onprogress = function (e) {
      if (e.lengthComputable) {
        var percentComplete = (e.loaded / e.total) * 100;
        console.log("Responding: {0}%".format(percentComplete));
      }
    };
    x.onreadystatechange = function () {
      if (x.readyState == 4) {
        if (returnXHR) {
          resolve(x);
          return;
        }
        if (x.status == 200) {
          switch (x.responseType) {
          case "blob":
            resolve(new Blob(
              [x.response],
              {
                type: x.getResponseHeader("Content-Type")
                  || "text/plain"
              }));
            break;
          case "json":
            resolve(x.response);
            break;
          default:
            if (x.getResponseHeader("Content-Type") == "application/json") {
              resolve(JSON.parse(x.responseText));
              break;
            }
            resolve(x.responseText);
          }
        } else {
          reject(new Error("Something else other than 200 was returned"));
        }
      }
    };
    if (this.accountSession) {
      x.setRequestHeader("Authorization", "Bearer " + this.accountSession);
    }
    if (headers) for (var header in headers) {
      x.setRequestHeader(header, headers[header]);
    }
    x.send(data);
  }.bind(this));
};

ICA.requestAPI = function (method, path, data) {
  return ICA.request(
    method,
    this.api + path,
    {
      "Content-Type": "application/json"
    },
    JSON.stringify(data),
    "json",
    true
  )
    .then(function (x) {
      var data = x.response;
      if (typeof data == "object") {
        if (data.error) {
          console.warn("Request caught error:", data);
          return Promise.reject(new Error(data.error));
        }
        if (data.warn) {
          console.warn("Request caught warning:", data);
        }
      }
      return data;
    });
};

ICA.uploadFile = function (file) {
  return ICA.uploadFileChunked(file);
  // return ICA.request(
  //   "post",
  //   this.api + "/files/",
  //   {
  //     "Content-Type": file.type
  //   },
  //   blob
  // )
  //   .then(function (fileId) {
  //     console.log("ICA: File uploaded");
  //     return fileId;
  //   });
};

ICA.uploadFileChunked = function (file) {
  return ICA.request(
    "post",
    this.api + "/files/",
    {
      "X-Upload-Content-Type": file.type,
      "X-Upload-Content-Length": file.size
    },
    null,
    null,
    true
  )
    .then(function (x) {
      if (x.status !== 200) return Promise.reject(new Error("ICA: Error with file upload request"));
      if (!x.response) return Promise.reject(new Error("ICA: Failed to upload file"));
      var fileId = x.response;

      function putFile(url, byteStart = 0, byteLength = 5 * 1024 * 1024) {
        var byteEnd = Math.min(file.size, byteStart + byteLength);
        return ICA.request(
          "put",<commit_msg>Update file upload script<commit_after>
var ICA = {
  api: "api/v1"
};

ICA.login = function () {
  return new Promise(function (resolve, reject) {
    var oauthWindow = window.open("oauth2login", "Log in via MintKit", "location=0,status=0,width=600,height=700");

    if (oauthWindow) {
      var timer = setInterval(function () {
        if (oauthWindow.closed) {
          clearInterval(timer);

          if (new Date().getTime() - window.sessionStorage.getItem("_ica_oauth2_timestamp") < 1000) {
            resolve();
          } else {
            reject(new Error("Error synchronizing/failed to log in"));
          }
        }
      }, 1000);
    } else {
      console.error("Failed to open popup window");
    }
  });
};

Object.defineProperty(ICA, "accountId", {
  get: function () {
    return window.sessionStorage.getItem("_ica_account_id");
  }
});

Object.defineProperty(ICA, "accountSession", {
  get: function () {
    return window.sessionStorage.getItem("_ica_account_session");
  }
});

ICA.request = function (method, url, headers, data, responseType = "json", returnXHR = false) {
  return new Promise(function (resolve, reject) {
    var x = new XMLHttpRequest();
    x.open(method, url, true);
    if (responseType && responseType != "x") x.responseType = responseType;
    if (x.upload) x.upload.onprogress = function (e) {
      if (e.lengthComputable) {
        var percentComplete = (e.loaded / e.total) * 100;
        console.log("Requesting: {0}%".format(percentComplete));
      }
    };
    x.onprogress = function (e) {
      if (e.lengthComputable) {
        var percentComplete = (e.loaded / e.total) * 100;
        console.log("Responding: {0}%".format(percentComplete));
      }
    };
    x.onreadystatechange = function () {
      if (x.readyState == 4) {
        if (returnXHR) {
          resolve(x);
          return;
        }
        if (x.status == 200) {
          switch (x.responseType) {
          case "blob":
            resolve(new Blob(
              [x.response],
              {
                type: x.getResponseHeader("Content-Type")
                  || "text/plain"
              }));
            break;
          case "json":
            resolve(x.response);
            break;
          default:
            if (x.getResponseHeader("Content-Type") == "application/json") {
              resolve(JSON.parse(x.responseText));
              break;
            }
            resolve(x.responseText);
          }
        } else {
          reject(new Error("Something else other than 200 was returned"));
        }
      }
    };
    if (this.accountSession) {
      x.setRequestHeader("Authorization", "Bearer " + this.accountSession);
    }
    if (headers) for (var header in headers) {
      x.setRequestHeader(header, headers[header]);
    }
    x.send(data);
  }.bind(this));
};

ICA.requestAPI = function (method, path, data) {
  return ICA.request(
    method,
    this.api + path,
    {
      "Content-Type": "application/json"
    },
    JSON.stringify(data),
    "json",
    true
  )
    .then(function (x) {
      var data = x.response;
      if (typeof data == "object" && data.error) {
        console.warn("Request caught error:", data);
        return Promise.reject(new Error(data.error));
      }
      return data;
    });
};

ICA.uploadFile = function (file) {
  return ICA.uploadFileChunked(file);
  // return ICA.request(
  //   "post",
  //   this.api + "/files/",
  //   {
  //     "Content-Type": file.type
  //   },
  //   blob
  // )
  //   .then(function (fileId) {
  //     console.log("ICA: File uploaded");
  //     return fileId;
  //   });
};

ICA.uploadFileChunked = function (file) {
  return ICA.request(
    "post",
    this.api + "/files/",
    {
      "X-Upload-Content-Type": file.type,
      "X-Upload-Content-Length": file.size
    },
    null,
    "json",
    true
  )
    .then(function (x) {
      var data = x.response;
      if (typeof data != "number") {
        if (typeof data == "object" && data.error) {
          console.warn("Request caught error:", data);
          return Promise.reject(new Error(data.error));
        }
        return Promise.reject(new Error("Error starting file upload"));
      }
      var fileId = data;

      function putFile(url, byteStart = 0, byteLength = 5 * 1024 * 1024) {
        var byteEnd = Math.min(file.size, byteStart + byteLength);
        return ICA.request(
          "put",
{'fraction_non_alphanumeric': 0.14976228209191758, 'fraction_numerical': 0.010301109350237718, 'mean_word_length': 2.0687738798193815, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4842754', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2707, 'n_words': 580}