{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "# Making requests with Python\n\nTo make a request, you'll need to specify the server and extension, using the requests module." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "import requests, sys\n\nserver = \"http://rest.ensembl.org\"\next = \"/lookup/id/ENSG00000157764\"\n \nr = requests.get(server+ext, headers={ \"Content-Type\" : \"application/json\"})\n\nprint (r)", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "Never assume that your request has worked. If it doesn't work, you should check the response code." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "import requests, sys\n\nserver = \"http://rest.ensembl.org\"\next = \"/lookup/id/ENSG00000157764\"\n \nr = requests.get(server+ext, headers={ \"Content-Type\" : \"application/json\"})\n\nif not r.ok:\n r.raise_for_status()", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "If you get responses in json (recommended), you can then decode them. I've also imported the pretty print (pprint) module from python, which makes my json easy to read. You'll find this useful during the exercises to see how the json looks." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "import requests, sys, json\nfrom pprint import pprint\n\nserver = \"http://rest.ensembl.org\"\next = \"/lookup/id/ENSG00000157764\"\n \nr = requests.get(server+ext, headers={ \"Content-Type\" : \"application/json\"})\n\nif not r.ok:\n r.raise_for_status()\n\ndecoded = r.json()\n\npprint (decoded)", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "The helper function allows you to call the request, check the status and decode the json in a single line in your script. If you're using lots of REST calls in your script, creating the function at the beginning of your script will save you a lot of time." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "import requests, sys, json\nfrom pprint import pprint\n\ndef fetch_endpoint(server, request, content_type):\n\n r = requests.get(server+request, headers={ \"Content-Type\" : content_type})\n\n if not r.ok:\n r.raise_for_status()\n sys.exit()\n\n if content_type == 'application/json':\n return r.json()\n else:\n return r.text\n\n\nserver = \"http://rest.ensembl.org/\"\next = \"lookup/id/ENSG00000157764?\"\ncon = \"application/json\"\nget_gene = fetch_endpoint(server, ext, con)\n\npprint (get_gene)", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "## Exercises 2\n\n1. Write a script to **lookup** the gene called *ESPN* in human and print the results in json." }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "# Exercise 2.1", "execution_count": null, "outputs": [] }, { "metadata": {}, "cell_type": "markdown", "source": "[Next page: Exercises 2 – answers](2_Making_requests_with_python_answers.ipynb)" } ], "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "3.5.4", "pygments_lexer": "ipython3", "codemirror_mode": { "version": 3, "name": "ipython" } } }, "nbformat": 4, "nbformat_minor": 2 }