Skip to content

Commit

Permalink
feat: update notebooks to get credits transactions and balance
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed Dec 21, 2023
1 parent 8f41de0 commit 4dac0a7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"tags": []
},
"source": [
"# Naas Credits - Export current transaction"
"# Naas Credits - Export current transactions to Excel"
]
},
{
Expand All @@ -30,7 +30,7 @@
"tags": []
},
"source": [
"**Tags:** #naascredits #export #transaction #current #python #notebook"
"**Tags:** #naascredits #export #transaction #current #excel #naas"
]
},
{
Expand Down Expand Up @@ -63,7 +63,7 @@
"tags": []
},
"source": [
"**Description:** This notebook will export the current transaction from Naas Credits. It will allow to have a better understanding of the current transaction and to analyze it."
"**Description:** This notebook will export your current transactions (transactions done this month) from Naas Credits API and save it to an Excel file."
]
},
{
Expand All @@ -74,7 +74,8 @@
"tags": []
},
"source": [
"**References:**\n- [Naas Credits Documentation](https://docs.naascredits.com/)\n- [Naas Credits API Reference](https://api.naascredits.com/reference)"
"**References:**\n",
"- [Naas Credits driver](https://github.com/jupyter-naas/drivers/blob/main/naas_drivers/tools/naas_credits.py)"
]
},
{
Expand Down Expand Up @@ -107,30 +108,31 @@
"papermill": {},
"tags": []
},
"source": "import requests\nimport json",
"outputs": []
"outputs": [],
"source": [
"from naas_drivers import naascredits\n",
"from datetime import datetime\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "367bb25c-0161-47b9-9514-611399f0d510",
"metadata": {
"papermill": {},
"tags": []
},
"id": "35b0528a-1021-4cfc-8363-03ecbb0dd08a",
"metadata": {},
"source": [
"### Setup variables\n- **api_key**: API key provided by Naas Credits. [How to get an API key?](https://docs.naascredits.com/getting-started/api-key)\n- **transaction_id**: ID of the transaction to be exported"
"### Setup variables\n",
"- `file_path`: Excel file path to be saved in your local env."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "349d0cbe-c9fb-48e6-be73-ad5c0911839a",
"metadata": {
"papermill": {},
"tags": []
},
"source": "api_key = \"YOUR_API_KEY\"\ntransaction_id = \"YOUR_TRANSACTION_ID\"",
"outputs": []
"id": "d0ac086d-1dba-4bbf-a692-f7bf0832f24e",
"metadata": {},
"outputs": [],
"source": [
"file_path = f\"{datetime.now().isoformat()}_currents_transactions.xlsx\""
]
},
{
"cell_type": "markdown",
Expand All @@ -151,30 +153,52 @@
"tags": []
},
"source": [
"### Export current transaction"
"### Get current transaction"
]
},
{
"cell_type": "markdown",
"id": "3f55d120-d3cc-441e-b928-69081078eb87",
"cell_type": "code",
"execution_count": null,
"id": "06a55817-cb91-4f9f-ae7e-25595be18237",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"This function will export the current transaction from Naas Credits."
"data = naascredits.connect().transactions.get_currents()"
]
},
{
"cell_type": "markdown",
"id": "30a1a7a6-5121-4c76-859e-00e0ea6cb364",
"metadata": {
"execution": {
"iopub.execute_input": "2023-12-21T08:53:33.705308Z",
"iopub.status.busy": "2023-12-21T08:53:33.705055Z",
"iopub.status.idle": "2023-12-21T08:53:33.708305Z",
"shell.execute_reply": "2023-12-21T08:53:33.707644Z",
"shell.execute_reply.started": "2023-12-21T08:53:33.705284Z"
},
"tags": []
},
"source": [
"### Convert data to DataFrame"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06a55817-cb91-4f9f-ae7e-25595be18237",
"id": "eb2a040e-dd00-4afa-933b-9ea2497d83dc",
"metadata": {
"papermill": {},
"tags": []
},
"source": "def export_transaction(api_key, transaction_id):\n url = \"https://api.naascredits.com/v1/transactions/\" + transaction_id\n headers = {\"Authorization\": \"Bearer \" + api_key}\n response = requests.get(url, headers=headers)\n if response.status_code == 200:\n return json.loads(response.content.decode(\"utf-8\"))\n else:\n return None",
"outputs": []
"outputs": [],
"source": [
"df = pd.DataFrame(data)\n",
"print(\"Current balance:\", df.CREDIT.sum())"
]
},
{
"cell_type": "markdown",
Expand All @@ -195,19 +219,25 @@
"tags": []
},
"source": [
"### Display result"
"### Save data to Excel file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb2a040e-dd00-4afa-933b-9ea2497d83dc",
"id": "bc039287-6129-4f0b-8966-80bb8985b989",
"metadata": {
"papermill": {},
"tags": []
},
"source": "transaction = export_transaction(api_key, transaction_id)\nif transaction is not None:\n print(json.dumps(transaction, indent=4))\nelse:\n print(\"Error while exporting transaction\")",
"outputs": []
"outputs": [],
"source": [
"df.to_excel(\n",
" file_path,\n",
" sheet_name=\"Export\",\n",
" index=False,\n",
")\n",
"print(f\"Excel file successfully saved: {file_path}\")"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -249,4 +279,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"tags": []
},
"source": [
"# Naas - Credits Get Balance\n",
"# Naas Credits - Get Balance\n",
"<a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/Naas/Naas_Credits_Get_Balance.ipynb\" target=\"_parent\"><img src=\"https://naasai-public.s3.eu-west-3.amazonaws.com/Open_in_Naas_Lab.svg\"/></a><br><br><a href=\"https://bit.ly/3JyWIk6\">Give Feedback</a> | <a href=\"https://github.com/jupyter-naas/awesome-notebooks/issues/new?assignees=&labels=bug&template=bug_report.md&title=Naas+-+Credits+Get+Balance:+Error+short+description\">Bug report</a>"
]
},
Expand Down Expand Up @@ -60,7 +60,7 @@
"tags": []
},
"source": [
"**Last update:** 2023-04-12 (Created: 2021-11-17)"
"**Last update:** 2023-12-21 (Created: 2021-11-17)"
]
},
{
Expand Down Expand Up @@ -130,7 +130,7 @@
"tags": []
},
"source": [
"### Function"
"### Get balance"
]
},
{
Expand All @@ -143,7 +143,8 @@
},
"outputs": [],
"source": [
"balance = naascredits.connect().get_balance()"
"balance = naascredits.connect().get_balance()\n",
"print(balance)"
]
},
{
Expand Down Expand Up @@ -205,7 +206,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.9.6"
},
"naas": {
"notebook_id": "b8e8498be8e2a6e4ed47256dd6b987fc934f372db1ce9b0ba198bd19514296ca",
Expand All @@ -227,4 +228,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}

0 comments on commit 4dac0a7

Please sign in to comment.