{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c0",
   "metadata": {},
   "source": [
    "# Cloud storage\n",
    "See which cloud drives (Box, Dropbox, Google Drive, OneDrive) are connected to your account. Listing a drive's files calls the provider, so it runs only with `RUN_PAID_CALLS = True` on a connected drive."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1",
   "metadata": {},
   "source": [
    "## Config\n",
    "Add your settings to the repo `.env` (local) or Colab **Secrets** — this cell reads them with `get(...)`:\n",
    "`INDOX_API_KEY` (your `ak_…` key) and `INDOX_BASE_URL` (`https://indox.org`; the SDK adds `/api/v1/…` itself). A missing key stops here with its name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2",
   "metadata": {},
   "outputs": [],
   "source": "import json, logging, re, sys, tempfile\nfrom pathlib import Path\n\nREPO = Path.cwd().parents[1]\nsys.path[:0] = [str(REPO), str(REPO / \"languages\" / \"python\")]\ntry:\n    from google.colab import userdata\n    get = userdata.get\nexcept ImportError:\n    from tools.env import require_env as get\n\nfrom indox_client import Indox\n\nlogging.basicConfig(format=\"→ %(message)s\", force=True)\nlogging.getLogger(\"urllib3.connectionpool\").setLevel(logging.DEBUG)\n\n\ndef _mask(record):\n    record.msg = re.sub(r\"/(public|sign)/[^/ ]+\", r\"/\\1/***\", record.getMessage())\n    record.args = ()\n    return True\n\n\nlogging.getLogger(\"urllib3.connectionpool\").addFilter(_mask)\n\n\ndef show(data, limit=2500):\n    print(json.dumps(data, indent=2, default=str)[:limit])\n\n\nclient = Indox(api_key=get(\"INDOX_API_KEY\"), base_url=get(\"INDOX_BASE_URL\"))\nstart_credits = client.billing.credits_balance()[\"credit_amount\"]\nSAMPLES = REPO / \"colab\"\nout = Path(tempfile.mkdtemp())\nRUN_PAID_CALLS = False"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "integrations = client.user.integrations()\n",
    "show(integrations)\n",
    "assert {\"box\", \"dropbox\", \"google_drive\", \"onedrive\"} <= {i[\"provider\"] for i in integrations}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "statuses = {p: client.storage.status(p) for p in (\"box\", \"dropbox\", \"google\", \"onedrive\")}\n",
    "show(statuses)\n",
    "assert all(isinstance(s[\"connected\"], bool) for s in statuses.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "connected = [p for p, s in statuses.items() if s[\"connected\"]]\n",
    "if RUN_PAID_CALLS and connected:\n",
    "    contents = client.storage.contents(connected[0])\n",
    "    show(contents)\n",
    "    assert isinstance(contents, (dict, list))\n",
    "else:\n",
    "    print(\"skipped: storage.contents calls the provider (RUN_PAID_CALLS = False; connected:\", connected, \")\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6",
   "metadata": {},
   "source": [
    "## Usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "now = client.billing.credits_balance()[\"credit_amount\"]\n",
    "print(f\"credits used by this notebook: {start_credits - now}  (balance {now})\")\n",
    "show(client.billing.usage_by_feature())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
