{ "nbformat": 4, "nbformat_minor": 2, "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3.9.6 64-bit" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6" }, "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" }, "colab": { "name": "yf_REST_calls.ipynb", "provenance": [], "collapsed_sections": [] } }, "cells": [ { "cell_type": "markdown", "source": [ "\n", "\n", "## Import modules and set Yellowfin Base URL\n", "\n" ], "metadata": { "id": "nF23j-M4rLMY" } }, { "cell_type": "code", "execution_count": 2, "source": [ "import requests \n", "import time\n", "import json\n", "\n", "YF_BASE_URL = \"\" # Could be something like: \"http://localhost:9696\"" ], "outputs": [], "metadata": { "id": "e4Us05rxrLMb" } }, { "cell_type": "markdown", "source": [ "## Make Sure YF Instance is up and running!" ], "metadata": { "id": "TO6DLJ3yrLMd" } }, { "cell_type": "code", "execution_count": 3, "source": [ "r = requests.get(url = f\"{YF_BASE_URL}/logonCheck.i4\", verify=False)\n", "assert r.status_code == 401, \"Hmm, I can't seem to access the instance. Is it running?\"\n", "print(f\"{YF_BASE_URL} is up and running! 💪\")" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "http://localhost:9696/api is up and running! 💪\n" ] } ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 630 }, "id": "ABc1EVp9rLMd", "outputId": "25a3d4ae-77bb-4a36-9462-43b9bcc67cb6" } }, { "cell_type": "markdown", "source": [ "## Step 1 - Obtain Refresh Token" ], "metadata": { "id": "xKcVKTFGrLMe" } }, { "cell_type": "code", "execution_count": null, "source": [ "millis = int(round(time.time() * 1000))\n", "\n", "headers = {\n", " \"Authorization\": f\"YELLOWFIN ts={millis}, nonce=random\",\n", " \"Content-Type\": \"application/json\",\n", " \"Accept\": \"application/vnd.yellowfin.api-v1+json\",\n", "}\n", "\n", "payload = json.dumps(\n", " {\n", " \"userName\": \"admin@yellowfin.com.au\", # Change to ad admin account in your yellowfin instance\n", " \"password\": \"test\",\n", " \"clientOrgRef\": \"YFDefault\",\n", " }\n", ")\n", "\n", "REFRESH_TOKEN_URL = f\"{YF_BASE_URL}/api/refresh-tokens\"\n", "\n", "r = requests.request(\"POST\", url=REFRESH_TOKEN_URL, headers=headers, data=payload)\n", "\n", "r_dict = json.loads(r.text)\n", "refresh_token = r_dict[\"securityToken\"]\n", "print(\"Refresh Token Obtained:\")\n", "print(refresh_token + \"\\n\")" ], "outputs": [], "metadata": { "id": "-KXrC_O4rLMf" } }, { "cell_type": "markdown", "source": [ "## Step 2 - Use Refresh Token to Obtain Access Token" ], "metadata": { "id": "2ywtgQahrLMf" } }, { "cell_type": "code", "execution_count": null, "source": [ "ACCESS_TOKEN_URL = f'{YF_BASE_URL}/api/access-tokens'\n", "\n", "millis = int(round(time.time() * 1000))\n", "headers = {\n", " \"Accept\": \"application/vnd.yellowfin.api-v1+json\",\n", " \"Authorization\": f\"YELLOWFIN ts={millis}, nonce=random, token={refresh_token}\",\n", " \"Content-Type\": \"application/json\",\n", "}\n", "\n", "r = requests.post(url = ACCESS_TOKEN_URL, headers=headers) \n", "r_dict= json.loads(r.text)\n", "access_token = r_dict['securityToken']\n", "print(\"Access Token Obtained:\")\n", "print(access_token+\"\\n\") " ], "outputs": [], "metadata": { "id": "QpAe1bEhrLMg" } }, { "cell_type": "markdown", "source": [ "## Upload Licence" ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "UPLOAD_LICENSE_URL = f'{YF_BASE_URL}/api/rpc/licence-management/upload-licence'\n", "\n", "payload={}\n", "\n", "with open(\".lic\", \"rb\") as license_file:\n", "\n", " files = [\n", " (\n", " \"newLicence\",\n", " (\n", " \".lic\",\n", " license_file,\n", " \"application/octet-stream\",\n", " ),\n", " )\n", " ]\n", "\n", " millis = int(round(time.time() * 1000))\n", " headers = {\n", " 'Accept': 'application/vnd.yellowfin.api-v1+json',\n", " 'Authorization': f'YELLOWFIN ts={millis}, nonce=random, token={access_token}',\n", " }\n", "\n", " r = requests.request(\"POST\", url=UPLOAD_LICENSE_URL, headers=headers, data=payload, files=files)\n", " assert r.status_code == 200, f\"That didn't work, I got this instead: {r.text}\"\n", " print(\"License uploaded sucessfully ✅\")" ], "outputs": [], "metadata": {} } ] }