Created
October 2, 2020 18:55
-
-
Save srinivas946/3aeb7ac23543d2a2aa6b27af4c9d3a85 to your computer and use it in GitHub Desktop.
Python REST API Demo Version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# REST API uses JSON format for communication most of the times\n", | |
| "# JSON Stands for Javascript Object Notation\n", | |
| "# JSON is light weight in nature" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# For Demo purpose i'm reffering the website https://reqres.in/" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### packages : requests" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# requests is the package, we generally used to interact with http/https protocol.\n", | |
| "# REST API is applicable for web services, so we need requests package to get information from webserver" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# import requests\n", | |
| "import requests" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'page': 2, 'per_page': 6, 'total': 12, 'total_pages': 2, 'data': [{'id': 7, 'email': '[email protected]', 'first_name': 'Michael', 'last_name': 'Lawson', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg'}, {'id': 8, 'email': '[email protected]', 'first_name': 'Lindsay', 'last_name': 'Ferguson', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg'}, {'id': 9, 'email': '[email protected]', 'first_name': 'Tobias', 'last_name': 'Funke', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg'}, {'id': 10, 'email': '[email protected]', 'first_name': 'Byron', 'last_name': 'Fields', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg'}, {'id': 11, 'email': '[email protected]', 'first_name': 'George', 'last_name': 'Edwards', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg'}, {'id': 12, 'email': '[email protected]', 'first_name': 'Rachel', 'last_name': 'Howell', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg'}], 'ad': {'company': 'StatusCode Weekly', 'url': 'http://statuscode.org/', 'text': 'A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things.'}}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# list users (it uses get request method)\n", | |
| "response = requests.get('https://reqres.in/api/users?page=2')\n", | |
| "if response.status_code == 200:\n", | |
| " print(response.json())\n", | |
| "else: print(f'Error | Status Code : {response.status_code} | Reason : {response.reason}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'data': {'id': 2, 'email': '[email protected]', 'first_name': 'Janet', 'last_name': 'Weaver', 'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg'}, 'ad': {'company': 'StatusCode Weekly', 'url': 'http://statuscode.org/', 'text': 'A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things.'}}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# single user (it uses get request method)\n", | |
| "response = requests.get('https://reqres.in/api/users/2')\n", | |
| "if response.status_code == 200:\n", | |
| " print(response.json())\n", | |
| "else: print(f'Error | Status Code : {response.status_code} | Reason : {response.reason}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Error | Status Code : 404 | Reason : Not Found\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# single user data not found (it uses get request method)\n", | |
| "response = requests.get('https://reqres.in/api/users/23')\n", | |
| "if response.status_code == 200:\n", | |
| " print(response.json())\n", | |
| "else: print(f'Error | Status Code : {response.status_code} | Reason : {response.reason}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'page': 1, 'per_page': 6, 'total': 12, 'total_pages': 2, 'data': [{'id': 1, 'name': 'cerulean', 'year': 2000, 'color': '#98B2D1', 'pantone_value': '15-4020'}, {'id': 2, 'name': 'fuchsia rose', 'year': 2001, 'color': '#C74375', 'pantone_value': '17-2031'}, {'id': 3, 'name': 'true red', 'year': 2002, 'color': '#BF1932', 'pantone_value': '19-1664'}, {'id': 4, 'name': 'aqua sky', 'year': 2003, 'color': '#7BC4C4', 'pantone_value': '14-4811'}, {'id': 5, 'name': 'tigerlily', 'year': 2004, 'color': '#E2583E', 'pantone_value': '17-1456'}, {'id': 6, 'name': 'blue turquoise', 'year': 2005, 'color': '#53B0AE', 'pantone_value': '15-5217'}], 'ad': {'company': 'StatusCode Weekly', 'url': 'http://statuscode.org/', 'text': 'A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things.'}}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# list resources (it uses get request method)\n", | |
| "response = requests.get('https://reqres.in/api/unknown')\n", | |
| "if response.status_code == 200:\n", | |
| " print(response.json())\n", | |
| "else: print(f'Error | Status Code : {response.status_code} | Reason : {response.reason}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'data': {'id': 2, 'name': 'fuchsia rose', 'year': 2001, 'color': '#C74375', 'pantone_value': '17-2031'}, 'ad': {'company': 'StatusCode Weekly', 'url': 'http://statuscode.org/', 'text': 'A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things.'}}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# single resource (it uses get request method)\n", | |
| "response = requests.get('https://reqres.in/api/unknown/2')\n", | |
| "if response.status_code == 200:\n", | |
| " print(response.json())\n", | |
| "else: print(f'Error | Status Code : {response.status_code} | Reason : {response.reason}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'name': 'cybersecpy', 'job': 'software', 'id': '600', 'createdAt': '2020-10-02T18:53:08.879Z'}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# create user (it uses post request method and we need to pass user data in the request)\n", | |
| "my_data = {'name': 'cybersecpy', 'job': 'software'}\n", | |
| "response = requests.post('https://reqres.in/api/users', data=my_data)\n", | |
| "if response.status_code == 201:\n", | |
| " print(response.json())\n", | |
| "else: print(f'Error | Status Code : {response.status_code} | Reason : {response.reason}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "{'name': 'python', 'job': 'developer', 'updatedAt': '2020-10-02T18:53:09.517Z'}\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# update user information (it uses put request method and we need to pass updated information in the request)\n", | |
| "my_data = {'name': 'python', 'job': 'developer'}\n", | |
| "response = requests.put('https://reqres.in/api/users/2', data=my_data)\n", | |
| "if response.status_code == 200:\n", | |
| " print(response.json())\n", | |
| "else: print(f'Error | Status Code : {response.status_code} | Reason : {response.reason}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "User Doesn't Exist !!! | Reason : Expecting value: line 1 column 1 (char 0)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# delet user information (it uses delete request method)\n", | |
| "response = requests.delete('https://reqres.in/api/users/2')\n", | |
| "try:\n", | |
| " if response.status_code == 204:\n", | |
| " print(response.json())\n", | |
| " else: print(f'Error | Status Code : {response.status_code} | Reason : {response.reason}')\n", | |
| "except Exception as e: print(f'User Doesn\\'t Exist !!! | Reason : {e}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Error | Status Code : 400 | Reason : Bad Request\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# register user account (it uses post request method)\n", | |
| "my_data = {'email': '[email protected]', 'password': '123456'}\n", | |
| "response = requests.post('https://reqres.in/api/register', data=my_data)\n", | |
| "if response.status_code == 200:\n", | |
| " print(response.json())\n", | |
| "else: print(f'Error | Status Code : {response.status_code} | Reason : {response.reason}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "<p>To Learn More Python Concepts, Refer <b><a href='https://cybersecpy.in/'>cybersecpy</a></b>" | |
| ] | |
| } | |
| ], | |
| "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.8.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment