Skip to content

Instantly share code, notes, and snippets.

@adyork
Last active October 20, 2022 16:02
Show Gist options
  • Save adyork/1219915286eafa4cefa0122ea98d28dc to your computer and use it in GitHub Desktop.
Save adyork/1219915286eafa4cefa0122ea98d28dc to your computer and use it in GitHub Desktop.
API-examples-randomfox. ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "API-examples-randomfox. ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/adyork/1219915286eafa4cefa0122ea98d28dc/api-examples-randomfox-ipynb.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "u1h_tycYkdu3"
},
"source": [
"# Get a Random Fox image from the radomfox API\n",
"\n",
"import requests\n",
"\n",
"response = requests.get(\"http://randomfox.ca/floof\")\n"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "75AYZYyq2eIE",
"outputId": "2de17dce-1687-47c5-d520-b7aeab787d60"
},
"source": [
"# Check the status code retured from your API request\n",
"# You should get 200 (OK, Success)\n",
"# More on status codes here: https://httpstatuses.com/\n",
"response.status_code"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"200"
]
},
"metadata": {},
"execution_count": 11
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "E1Iw-_LcD-FT",
"outputId": "ce986db5-5bad-4aa9-ff9f-a277c929d7e4"
},
"source": [
"#you may want to make decisions based on the response code\n",
"if response.status_code == 200:\n",
" print('200: Success!')\n",
"elif response.status_code == 404:\n",
" print('404: Not Found.')"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Success!\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "SBIQb2YyFIbZ",
"outputId": "e9354ed5-b2af-4583-a3e3-7577c9a4fa80",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"response.headers['Content-Type']"
],
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'application/json'"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1Qy9Kd__lNre",
"outputId": "85d3aa93-c1b9-40ae-ba79-39899cc6145b"
},
"source": [
"#What did we get? Let's look at the response as a string\n",
"print(response.text)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{\"image\":\"https:\\/\\/randomfox.ca\\/images\\/71.jpg\",\"link\":\"https:\\/\\/randomfox.ca\\/?i=71\"}\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PWLhbYG-3laN",
"outputId": "649fe38f-2234-437b-d25a-fec7e770471e"
},
"source": [
"type(response.text)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"str"
]
},
"metadata": {},
"execution_count": 22
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XTDmbLDSl6wv",
"outputId": "e35355e7-4bc9-49d7-ccc3-99a47e393938"
},
"source": [
"#most apis return json so let's get that as a dictionary\n",
"resp_dict = response.json()\n",
"print(resp_dict)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'image': 'https://randomfox.ca/images/71.jpg', 'link': 'https://randomfox.ca/?i=71'}\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QX4o2mJH3Kql",
"outputId": "ceb37b5f-7364-490b-bc24-1aaba96db985"
},
"source": [
"# we can check the type to make sure it is a dictionary.\n",
"type(resp_dict)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict"
]
},
"metadata": {},
"execution_count": 15
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "A9HxLP9yr249",
"outputId": "162cd6fe-e5fd-40c3-c558-9c6355ffc3aa"
},
"source": [
"#look for image key and print it\n",
"print(resp_dict['image'])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"https://randomfox.ca/images/71.jpg\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 659
},
"id": "P96XlXQasI3a",
"outputId": "253f2233-dadb-4109-ab37-3e6cf5ab252a"
},
"source": [
"from IPython.core.display import Image, display\n",
"display(Image(url=resp_dict['image']) )"
],
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"<img src=\"https://randomfox.ca/images/71.jpg\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment