{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to use an API"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import packages "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Base url to get the data\n",
    "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=pincode&date=date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url=\"https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=110053&date=21-05-2021\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Headers\n",
    "You can your user-agent details from https://www.whatismybrowser.com/detect/what-is-my-user-agent\n",
    "\n",
    "Copy and Paste the data in User-Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers={\n",
    "    \"User-Agent\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "response=requests.get(base_url,headers=headers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Status code\n",
    "If status_code is 200 : Processed Successfully.\n",
    "\n",
    "If status_code is 404 : Error\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'sessions': [{'center_id': 577114,\n",
       "   'name': 'JPC Hospital 4th Floor A Wing',\n",
       "   'address': 'Shastri Park, Delhi-110053',\n",
       "   'state_name': 'Delhi',\n",
       "   'district_name': 'North East Delhi',\n",
       "   'block_name': 'Not Applicable',\n",
       "   'pincode': 110053,\n",
       "   'from': '09:00:00',\n",
       "   'to': '21:00:00',\n",
       "   'lat': 28,\n",
       "   'long': 77,\n",
       "   'fee_type': 'Free',\n",
       "   'session_id': 'd3780f66-0004-4a63-b016-45bc48419f67',\n",
       "   'date': '21-05-2021',\n",
       "   'available_capacity': 29,\n",
       "   'fee': '0',\n",
       "   'min_age_limit': 45,\n",
       "   'vaccine': 'COVISHIELD',\n",
       "   'slots': ['09:00AM-12:00PM',\n",
       "    '12:00PM-03:00PM',\n",
       "    '03:00PM-06:00PM',\n",
       "    '06:00PM-09:00PM']},\n",
       "  {'center_id': 577261,\n",
       "   'name': 'JPC Hospital 4th Floor B Wing',\n",
       "   'address': 'Shastri Park, Delhi-110053',\n",
       "   'state_name': 'Delhi',\n",
       "   'district_name': 'North East Delhi',\n",
       "   'block_name': 'Not Applicable',\n",
       "   'pincode': 110053,\n",
       "   'from': '09:00:00',\n",
       "   'to': '21:00:00',\n",
       "   'lat': 28,\n",
       "   'long': 77,\n",
       "   'fee_type': 'Free',\n",
       "   'session_id': '093614e1-8fc7-4221-8ef7-5c8ccac502ff',\n",
       "   'date': '21-05-2021',\n",
       "   'available_capacity': 78,\n",
       "   'fee': '0',\n",
       "   'min_age_limit': 45,\n",
       "   'vaccine': 'COVISHIELD',\n",
       "   'slots': ['09:00AM-12:00PM',\n",
       "    '12:00PM-03:00PM',\n",
       "    '03:00PM-06:00PM',\n",
       "    '06:00PM-09:00PM']},\n",
       "  {'center_id': 587820,\n",
       "   'name': 'JPC Hospital Ground Floor - 2',\n",
       "   'address': 'Shastri Park, Delhi-110053',\n",
       "   'state_name': 'Delhi',\n",
       "   'district_name': 'North East Delhi',\n",
       "   'block_name': 'Not Applicable',\n",
       "   'pincode': 110053,\n",
       "   'from': '09:00:00',\n",
       "   'to': '21:00:00',\n",
       "   'lat': 28,\n",
       "   'long': 77,\n",
       "   'fee_type': 'Free',\n",
       "   'session_id': '84dca862-5b74-4267-9925-f2c571353ede',\n",
       "   'date': '21-05-2021',\n",
       "   'available_capacity': 67,\n",
       "   'fee': '0',\n",
       "   'min_age_limit': 45,\n",
       "   'vaccine': 'COVISHIELD',\n",
       "   'slots': ['09:00AM-12:00PM',\n",
       "    '12:00PM-03:00PM',\n",
       "    '03:00PM-06:00PM',\n",
       "    '06:00PM-09:00PM']}]}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## It will work only if some data is available for key sessions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "if data['sessions']:\n",
    "    session=pd.DataFrame(data['sessions'])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "if data['sessions']:\n",
    "    j=pd.DataFrame(session[['min_age_limit','name','address','vaccine','available_capacity','fee_type']])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "j.rename(columns={'min_age_limit':'Minimum Age','name':'Name','address':'Address','vaccine':'Vaccine','available_capacity':'Slots','fee_type':'Free/Paid'},inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Minimum Age</th>\n",
       "      <th>Name</th>\n",
       "      <th>Address</th>\n",
       "      <th>Vaccine</th>\n",
       "      <th>Slots</th>\n",
       "      <th>Free/Paid</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>45</td>\n",
       "      <td>JPC Hospital 4th Floor A Wing</td>\n",
       "      <td>Shastri Park, Delhi-110053</td>\n",
       "      <td>COVISHIELD</td>\n",
       "      <td>29</td>\n",
       "      <td>Free</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>45</td>\n",
       "      <td>JPC Hospital 4th Floor B Wing</td>\n",
       "      <td>Shastri Park, Delhi-110053</td>\n",
       "      <td>COVISHIELD</td>\n",
       "      <td>78</td>\n",
       "      <td>Free</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>45</td>\n",
       "      <td>JPC Hospital Ground Floor - 2</td>\n",
       "      <td>Shastri Park, Delhi-110053</td>\n",
       "      <td>COVISHIELD</td>\n",
       "      <td>67</td>\n",
       "      <td>Free</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Minimum Age                           Name                     Address  \\\n",
       "0           45  JPC Hospital 4th Floor A Wing  Shastri Park, Delhi-110053   \n",
       "1           45  JPC Hospital 4th Floor B Wing  Shastri Park, Delhi-110053   \n",
       "2           45  JPC Hospital Ground Floor - 2  Shastri Park, Delhi-110053   \n",
       "\n",
       "      Vaccine  Slots Free/Paid  \n",
       "0  COVISHIELD     29      Free  \n",
       "1  COVISHIELD     78      Free  \n",
       "2  COVISHIELD     67      Free  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
