Skip to content

Instantly share code, notes, and snippets.

@adamw
Created October 31, 2019 09:38

Revisions

  1. adamw created this gist Oct 31, 2019.
    133 changes: 133 additions & 0 deletions polynote_sttp.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    {
    "metadata" : {
    "config" : {
    "dependencies" : {
    "scala" : [
    "com.softwaremill.sttp.client:okhttp-backend_2.11:2.0.0-M8",
    "com.softwaremill.sttp.client:circe_2.11:2.0.0-M8",
    "io.circe:circe-optics_2.11:0.11.0",
    "io.circe:circe-generic_2.11:0.11.0"
    ]
    },
    "exclusions" : [
    ],
    "repositories" : [
    ],
    "sparkConfig" : {

    }
    }
    },
    "nbformat" : 4,
    "nbformat_minor" : 0,
    "cells" : [
    {
    "cell_type" : "markdown",
    "execution_count" : 0,
    "metadata" : {
    "language" : "text"
    },
    "language" : "text",
    "source" : [
    "# Polynote + sttp client\n",
    "\n",
    "\n"
    ],
    "outputs" : [
    ]
    },
    {
    "cell_type" : "code",
    "execution_count" : 1,
    "metadata" : {
    "jupyter.outputs_hidden" : true,
    "cell.metadata.exec_info" : {
    "startTs" : 1572512533070,
    "endTs" : 1572512533119
    },
    "language" : "scala"
    },
    "language" : "scala",
    "source" : [
    "import sttp.client.okhttp.quick._\n",
    "import sttp.client.circe._\n",
    "import io.circe.generic.auto._"
    ],
    "outputs" : [
    ]
    },
    {
    "cell_type" : "code",
    "execution_count" : 2,
    "metadata" : {
    "cell.metadata.exec_info" : {
    "startTs" : 1572514528701,
    "endTs" : 1572514529654
    },
    "language" : "scala"
    },
    "language" : "scala",
    "source" : [
    "case class GitHubProject(name: String)\n",
    "case class GitHubResult(total_count: Int,items: List[GitHubProject])\n",
    "\n",
    "val query = \"language:scala\"\n",
    "val result = quickRequest\n",
    " .get(uri\"https://api.github.com/search/repositories?q=$query&sort=stars&per_page=100\")\n",
    " .response(asJsonAlwaysUnsafe[GitHubResult])\n",
    " .send()\n",
    " .body"
    ],
    "outputs" : [
    ]
    },
    {
    "cell_type" : "code",
    "execution_count" : 3,
    "metadata" : {
    "cell.metadata.exec_info" : {
    "startTs" : 1572514487567,
    "endTs" : 1572514488032
    },
    "language" : "scala"
    },
    "language" : "scala",
    "source" : [
    "case class LetterAndCount(letter: String, count: Int)\n",
    "// result: Response[GitHubResult] is what we've read from the API\n",
    "val firstLetterToName = result.items.groupBy(_.name.toUpperCase.charAt(0))\n",
    "val pairs = firstLetterToName.toList.map { case (letter, projects) =>\n",
    " LetterAndCount(letter.toString, projects.size)\n",
    "}.sortBy(_.letter)\n",
    "pairs"
    ],
    "outputs" : [
    {
    "execution_count" : 3,
    "data" : {
    "text/plain" : [
    "List(LetterAndCount(A,8), LetterAndCount(B,5), LetterAndCount(C,6), LetterAndCount(D,4), LetterAndCount(E,2), LetterAndCount(F,7), LetterAndCount(G,7), LetterAndCount(H,1), LetterAndCount(I,1), LetterAndCount(J,1), LetterAndCount(K,4), LetterAndCount(L,4"
    ]
    },
    "metadata" : {
    "name" : "Out",
    "type" : "List[LetterAndCount]"
    },
    "output_type" : "execute_result"
    }
    ]
    },
    {
    "cell_type" : "code",
    "execution_count" : 4,
    "metadata" : {
    "language" : "scala"
    },
    "language" : "scala",
    "source" : [
    ],
    "outputs" : [
    ]
    }
    ]
    }