{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'config'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-10-308c1cb0467c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mrequests\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'config'"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import os\n",
    "import config\n",
    "import json\n",
    "\n",
    "class LookerAPI():\n",
    "\n",
    "    def __init__(self, config):\n",
    "\n",
    "        self.client_id = config.looker_client_id\n",
    "        self.client_secret = config.looker_client_secret\n",
    "        self.url =  config.looker_url\n",
    "\n",
    "        self.auth_token = self.get_access_token(self.client_id, self.client_secret, self.url)\n",
    "\n",
    "    def get_access_token(self, client_id, client_secret, url):\n",
    "\n",
    "        '''\n",
    "        This funtion logs into the Looker API with proper creds and returns the authorization header (with token)\n",
    "\n",
    "        '''\n",
    "\n",
    "        credentials = {\n",
    "            'client_id': client_id,\n",
    "            'client_secret': client_secret\n",
    "        }\n",
    "\n",
    "        url ='{}/login'.format(url)\n",
    "\n",
    "        auth = requests.post(url, params=credentials)\n",
    "\n",
    "        if auth.status_code == 200:\n",
    "\n",
    "            token = auth.json()['access_token']\n",
    "\n",
    "            return {'Authorization' : 'token {}'.format(token)}\n",
    "\n",
    "        else:\n",
    "            print('Authorization Failed')\n",
    "\n",
    "\n",
    "    def run_look(self, look_id):\n",
    "\n",
    "        '''\n",
    "        Runs a Look and returns output in JSON format\n",
    "\n",
    "        Args:\n",
    "            look_id (int): id of Look you want to run\n",
    "\n",
    "        Returns:\n",
    "            list: JSON represntation of output\n",
    "\n",
    "        '''\n",
    "\n",
    "        url = '{url}/looks/{look_id}/run/json'.format(url=self.url, look_id=look_id)\n",
    "\n",
    "        response = requests.get(\n",
    "            url,\n",
    "            headers=self.auth_token\n",
    "        )\n",
    "\n",
    "\n",
    "        if response.status_code == 200:\n",
    "            return response.json()\n",
    "\n",
    "        return 'Did not receive status code: 200'\n",
    "\n",
    "    def get_user(self, user_id):\n",
    "\n",
    "        '''\n",
    "        Gets info about a given user\n",
    "\n",
    "        Args:\n",
    "            user_id (int): id of user\n",
    "\n",
    "        Returns:\n",
    "            dict: Json representation of output\n",
    "        '''\n",
    "\n",
    "        url = '{url}/users/{user_id}'.format(url=self.url, user_id=user_id)\n",
    "\n",
    "        response = requests.get(\n",
    "            url,\n",
    "            headers=self.auth_token\n",
    "        )\n",
    "\n",
    "        if response.status_code == 200:\n",
    "            return response.json()\n",
    "\n",
    "        return 'Did not receive status code: 200'\n",
    "    \n",
    "    def get_all_users(self):\n",
    "        \n",
    "        '''\n",
    "        Returns JSON of all Looker Users\n",
    "        '''\n",
    "        \n",
    "        url = '{url}/users'.format(url=self.url)\n",
    "        \n",
    "        response = requests.get(\n",
    "            url,\n",
    "            headers=self.auth_token\n",
    "        )\n",
    "        \n",
    "        if response.status_code == 200:\n",
    "            return response.json()\n",
    "        \n",
    "        else:\n",
    "            return 'Did not receive status code: 200'\n",
    "\n",
    "\n",
    "    def disable_user(self, user_id):\n",
    "\n",
    "        '''\n",
    "        Disables a user\n",
    "\n",
    "        Args:\n",
    "            user_id(int): user id you want to disable\n",
    "        Returns:\n",
    "            N/A\n",
    "        '''\n",
    "\n",
    "        url = '{url}/users/{user_id}'.format(url=self.url, user_id=user_id)\n",
    "\n",
    "        body = json.dumps({'is_disabled': True})\n",
    "\n",
    "        response = requests.patch(\n",
    "            url,\n",
    "            data=body,\n",
    "            headers=self.auth_token\n",
    "        )\n",
    "\n",
    "        if response.status_code == 200:\n",
    "            print('User ID {} successfully disabled'.format(user_id))\n",
    "\n",
    "        else:\n",
    "            return 'Did not receive status code: 200'\n",
    " \n",
    "    def get_all_groups(self):\n",
    "        \n",
    "        '''\n",
    "        Returns JSON of all Looker Groups\n",
    "        '''\n",
    "    \n",
    "        url = '{url}/groups'.format(url=self.url)\n",
    "        \n",
    "        response = requests.get(\n",
    "            url,\n",
    "            headers=self.auth_token\n",
    "        )\n",
    "        \n",
    "        if response.status_code == 200:\n",
    "            return response.json()\n",
    "        \n",
    "        else: \n",
    "            return  'Did not receive status code: 200'\n",
    "        \n",
    "    def add_user_to_group(self, user_id, group_id):\n",
    "        \n",
    "        '''\n",
    "        Adds a specified user to a specified group\n",
    "        \n",
    "        Args:\n",
    "            user_id(int)\n",
    "            group_id(int)\n",
    "        Returns:\n",
    "            JSON\n",
    "        '''\n",
    "        \n",
    "        url = '{url}/groups/{group_id}/users'.format(url=self.url, group_id=group_id)\n",
    "\n",
    "        body = json.dumps({'user_id': user_id})\n",
    "\n",
    "        response = requests.post(\n",
    "            url,\n",
    "            data=body,\n",
    "            headers=self.auth_token\n",
    "        )\n",
    "        \n",
    "        return response.json() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'config'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-11-e8dd0edca720>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mlooker\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/ds/orchard-analytics/core-looker-api/looker.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mrequests\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'config'"
     ]
    }
   ],
   "source": [
    "import looker\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_access_token(client_id, client_secret, url):\n",
    "\n",
    "        '''\n",
    "        This funtion logs into the Looker API with proper creds and returns the authorization header (with token)\n",
    "\n",
    "        '''\n",
    "\n",
    "        credentials = {\n",
    "            'client_id': client_id,\n",
    "            'client_secret': client_secret\n",
    "        }\n",
    "\n",
    "        url ='{}/login'.format(url)\n",
    "\n",
    "        auth = requests.post(url, params=credentials)\n",
    "\n",
    "        if auth.status_code == 200:\n",
    "\n",
    "            token = auth.json()['access_token']\n",
    "\n",
    "            return {'Authorization' : 'token {}'.format(token)}\n",
    "        else:\n",
    "            return auth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Authorization': 'token 35tr7XW3WNh2QQ4g9mhpwSZ2WX4B5yp5vK4djGS6'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_access_token('Vf36pn2Q2g96gRp754gm','V732VFX9WZ9XP3BTRMmMdkTq','https://theorchard.looker.com:19999/api/3.1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "auth_token = get_access_token('xD4mJj7BPwBxmCw3fD9G','94jfKfVrGwSTDvyJTYzPDDfj','https://theorchardoem.looker.com:19999/api/3.1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [404]>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "requests.get('https://theorchardoem.looker.com:19999/api/3.1/dashboards/601', headers=auth_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
