Lambda-School-Labs/Labs26-StorySquad-DS-TeamB

View on GitHub
notebooks/submission_endpoint_interations.ipynb

Summary

Maintainability
Test Coverage
{
 "metadata": {
  "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-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1600367979214",
   "display_name": "Python 3.7.6 64-bit ('ds-base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# outlining the submission endpoints interactions\n",
    "\n"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from requests import get, post"
   ]
  },
  {
   "source": [
    "## Launch the API via uvicorn using the following command:\n",
    "```\n",
    "export GOOGLE_CREDS=[content of service account key file]\n",
    "uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000\n",
    "```"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "## the following cell will check if the api is runnning and accepting requests"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "200"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "server_check = get(\"http://0.0.0.0:8000\")\n",
    "server_check.status_code"
   ]
  },
  {
   "source": [
    "## Open a local file and nest it in a files dictonary"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'files': ('3101.jpg', <_io.BufferedReader name='3101.jpg'>)}"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "image = open('3101.jpg', 'rb')\n",
    "files = {\"files\":(image.name, image)}\n",
    "files"
   ]
  },
  {
   "source": [
    "## Make a post request to the `/submission/text` enpoint using the keyword `files` and the url argument `story_id`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "200\n"
    }
   ],
   "source": [
    "story_id = \"3101\"\n",
    "args = f\"?story_id={story_id}\"\n",
    "response = post(f\"http://0.0.0.0:8000/submission/text\" + args, files=files)\n",
    "print(response.status_code)\n"
   ]
  },
  {
   "source": [
    "## Open an illustraion file"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "{'files': ('questionable.png', <_io.BufferedReader name='questionable.png'>)}"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "image = open('questionable.png', 'rb')\n",
    "files = {\"files\":(image.name, image)}\n",
    "files"
   ]
  },
  {
   "source": [
    "## post to the `/submission/illustration` enpoint and print the results"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "200\n"
    }
   ],
   "source": [
    "response = post(\"http://0.0.0.0:8000/submission/illustration\", files=files)\n",
    "print(response.status_code)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "b'{\"is_flagged\":true,\"reason\":[\"adult: VERY_UNLIKELY\",\"violence: VERY_UNLIKELY\",\"racy: POSSIBLE\"]}'"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "response.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}