(This page has no text content)
WEB API DEVELOPMENT WITH PYTHON A Beginner’s Guide using Flask and FastAPI (First Edition) By REHAN HAIDER Website: https://CloudBytes.dev Email: student@CloudBytes.dev
Copyright Notice Copyright © 2021 by Rehan Haider and CloudBytes. All rights reserved. No part of this publication may be reproduced, stored, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise without written permission from the publisher. It is illegal to copy this book, post it to a website, or distribute it by any other means without permission. Rehan Haider asserts the moral right to be identified as the author of this work. Rehan Haider or CloudBytes has no responsibility for the persistence or accuracy of URLs for external or third-party Internet Websites referred to in this publication and does not guarantee that any content on such Websites is, or will remain, accurate or appropriate. Designations used by companies to distinguish their products are often claimed as trademarks. All brand names and product names used in this book and on its cover are trade names, service marks, trademarks, and registered trademarks of their respective owners.
Preface This book is intended for beginners with basic programming knowledge, who are trying to understand advanced concepts and give them a hands-on experience of interacting with, and building an API using Python. My method of learning is by doing and doing something over and over again till it becomes a habit. Thus, this book will follow that philosophy, talk about concepts that you need to know, but will be focused on providing hands-on examples that you follow along but not act as a complete reference. The contents of this book are divided into three parts 1. Introductory Concepts : That explains terms and definitions that you will encounter 2. Interacting with an API : Explains how to use and interact with APIs. It’s easier to build things that you one knows how to operate 3. Building APIs : Main focus of the book, building APIs using Python’s Flask & FastAPI libraries The topics and presentation will get progressively more difficult as you go deeper into the book and that is by design to get the readers to a stage where they are able self-learn any framework or library without requiring any help.
Student Support If you face any challenges or need help, you can email student@CloudBytes.dev You can also create an issue on the GitHub repository of that particular exercise. In fact, this is a good practice. Additional Resources Modern developers have only two loyal friends: 1. Google 2. Stack Overflow Chances are your program will throw errors and you need to debug it. The best way to do that is start by Googling your error message and look through the links that Google suggests on Stack Overflow. Arguably over half of issues can be resolved this way. The rest requires looking through documentation, reading blog / tutorial posts, and talking to other developers.
Table of Contents Table of Contents Prerequisites 1. Internet 2. Accounts 3. Operating System 4. Python 5. Terminal 6. Text Editor / IDE 7. Git for Version Control 8. Docker for Desktop (Optional) 9. Jupyter Notebook Chapter 1: Introduction to Web APIs 1.1 What is API? 1.2 Types of APIs 1.3 What is a Web API? 1.4 Getting our hands dirty 1.5 Getting hands dirty programmatically 1.6 JavaScript Object Notation (JSON) 1.7 Why are APIs needed? 1.8 API Design Patterns Chapter 2: Python & Working with APIs 2.1 Programmatically accessing an API Chapter 3: Building APIs with Flask 3.1 Initialise the development environment 3.2 Understanding the Starter Kit 3.3 Initialising the starter kit 3.3 A minimal Flask API 3.3 Explanation 3.5 Running the API 3.6 Call the API Programmatically 3.7 JSONIFY the response Chapter 4: Building interactive APIs 4.1 Capturing request arguments 4.2 Explanation
4.3 Testing the API 4.4 Catching sneaky behaviour and errors 4.4 Handling incorrect API requests Chapter 5: Multi-argument interactive API 5.1 Capturing multiple arguments 5.2 Explanation 5.3 Testing the API 5.4 Reader Challenge Chapter 6. Google search as an API 6.1 An informal introduction to URL and Querystring 6.2 What can we do with this information? 6.3 Understanding the Starter Kit 6.4 Logic of the application 6.5 Rendering home page 6.6 Returning Search Results 6.7 Explanation 6.8 Student Challenge Chapter 7: Building a Dictionary API 7.1 Understanding the Starter Kit 7.2 Logic of the application 7.3 Handle incoming searches 7.4 Finding the definition of the word 7.5 Handling list of words 7.6 Testing the API 7.7 Student Challenge 7.8 Jupyter Notebook to test the API Chapter 8: Building a POST API 8.1 API to add Filters 8.2 Understanding the Starter Kit 8.3 Logic of the application 8.4 Implementing the Filter 8.5 Testing the API 8.6 Getting filtered image 8.7 Bonus Challenge #2 Chapter 9: Bonus Lesson: Deploying the API 9.1 Configuring the CD Pipeline 9.2 Testing the API 9.3 Pro Tip: Testing using Terminal
9.4 Bonus Challenge Chapter 10: Introducing FastAPI 10.1 Asynchronous Programming 10.2 Enter FastAPI 10.3 Understanding the Starter Kit 10.4 Saying Hello FastAPI Chapter 11: Dictionary using FastAPI 11.1 Understanding the Starter Kit 11.2 Implementing the usage instructions 11.3 Implementing the dictionary 11.4 Testing the API 11.5 : FastAPI OpenAPI Docs and Swagger UI 11.6 Handling a list of words 11.7 Testing The API Chapter 12: Image filters using FastAPI 12.1 Bonus: Deployment to Heroku Chapter 13: FastAPI async / await 13.1 Making the program asynchronous 13.2 Making the filter API asynchronous 13.3 Test Scenario 1 13.4 Test Scenario 2 13.5 Test scenario 3 13.6 Test Scenario 4 13.7 Conclusion 13.8 Bonus Challenge Chapter 14: Making a TODO API 14.1 API Specifications 14.2 Initialise the environment 14.3 Understanding the starter kit 14.4 Request Body and Data Validation 14.5 Data Model for TODO 14.6 Task manager 14.7 Building the API 14.8 Final API design 14.9 Testing the API 14.10 Where to from here? Au Revoir
About the Author Acknowledgements
Prerequisites 1. Internet Access to internet is needed to download resources 2. Accounts You need to create the following accounts 1. GitHub : https://www.github.com 2. Google : https://www.google.com 3. Operating System Any one of the following operating systems will do 1. Windows 10 build 17063 and later (April 2018) 2. Linux (Ubuntu 18.04, etc.) 3. MacOS (10.12.6 or above) 4. Python Install the latest version of Python for your Operating System from. Make sure you add Python to the PATH variable by checking the highlighted boxes below. https://www.python.org/downloads/ 5. Terminal For Windows: CMD.exe is good enough for our purposes. You can open it by pressing ⊞ Windows Key + R together and running “CMD”. You can also use PowerShell; however, it will require you to remove the alias for curl which is mapped incorrectly to a cmdlet by running Remove-Item alias:curl
You will need to run the above every time you start a new PowerShell terminal. For Linux / MacOS: Most distributions of Linux & macOS come with terminals & cURL in-built. Common to all OS: Make sure cURL is installed by running the below command curl -V If you get any error or cURL is not installed download and install relevant version from the below link https://curl.haxx.se 6. Text Editor / IDE You can use any Text Editor / IDE that you are comfortable with, but VSCode is recommended due to its tight integration with GitHub , Docker , and WSL . Download the appropriate version of VSCode for your OS and install from: https://code.visualstudio.com/download In VSCode , go to “ Extensions ” search for Python and install the extension from Microsoft called Pylance . Please ensure you login into the VSCode account using your GitHub Credentials, this will enable you to use VSCode terminal with GitHub without typing in username and password repeatedly. 7. Git for Version Control Recommended for Beginners : If you know absolutely nothing about Git , and are a Windows or macOS user, download the GitHub for Desktop from below. https://desktop.github.com/ Otherwise for advanced users, download, install, and configure the command line utility. Downloading Git: Check if Git is already available by running the below in the
terminal git --version If you see an output, you don’t need to do anything. But if you get an error, proceed to the below URL, and download and install the appropriate version of Git for your operating system https://git-scm.com/downloads Configure Git : Check if Git is already available by running. Open the terminal and run the below commands using your account details git config --global user.name "Your name here" git config --global user.email "your_email@example.com" 8. Docker for Desktop (Optional) If you’re running a CLI Linux, it’s likely I don’t need to explain this. For others, download the appropriate version of Docker for Desktop for your OS and install from: https://www.docker.com/products/docker-desktop/
9. Jupyter Notebook Jupyter Notebook is a browser based interactive iPython application that you can use to build your code by testing it step by step. Makes life infinitely easier. Jupyter requires Python & pip to be installed. To install Jupyter Notebook , open the terminal and type the below to install Jupyter Notebook pip install notebook Once done just run the below command in the terminal to launch the Jupyter Notebook in your default browser. jupyter notebook Note: You can also use Jupyter Lab or Google Colab instead of Jupyter Notebook.
Chapter 1: Introduction to Web APIs If you’re a beginner even the thought of APIs is somewhat scary. What kind of dark magic is that? And why is everyone paying hundreds of thousands to API developers. Do they drink blood and sacrifice goats in a circle under moon light? Pretty soon while the cool kids claim to be inventing calculus and you are out of the loop and feel like Andy below. If you identify with it, you’re in luck. We are going to talk about APIs. 1.1 What is API? API is an acronym for A pplication P rogramming I nterface, but nobody calls it that just like nobody calls USB a Universal Serial Bridge . The technical meaning of API is that it is a set of definitions and protocols for building, communicating, and integrating application software(s), thus the term “ Interface ”. But technical jargons apart, APIs are just a way to interact with applications whose internal workings are not visible to external users. APIs allow the external users (clients) to “ request ” something from the application or server and get a corresponding “ response ”.
In fact, you have already used the real-world equivalent of APIs . For example, riding in an old-fashioned cab. To get a ride in a cab you would need to 1. “ Request ” a cab to pick you up, along with your contact and location details 2. In “ response ” to your request you get a cab details and the cab picks you up 3. You again “ request ” the cab driver to take you to a specific location 4. In “ response ” the cab driver drives you to your desired location Now, you don’t need to know how to operate a car, to be able to get from one place to another, the cab driver acts like an API . You might not know anything about how a car operates but you can interact with the API layer, that is, the driver, and navigate to your desired outcome. Another way to look at APIs is, it is a magic box that takes input in a very specific format and gives output in a very specific format. In application development this “ standardisation ” of input and output (I/O) is useful and makes it easy for a developer to interact with systems that they don’t control. 1.2 Types of APIs If you’ve heard the term API , chances are it was used to refer to a very specific type of API called Web API . However, in general, the term API could have other meanings based on what the API is being used for.
Based on usage, APIs can be broadly divided into four categories as listed below: 1. Web APIs are used to communicate between a server and a client over the internet. Web API as the name suggests is a very specific type of API used to interact and manipulate information or resources over internet. 2. Remote APIs define the standards of interaction for applications running on different machines. For example, JDBC connectivity API that connects a database to the program 3. Libraries and Frameworks acts as the interface to a software library is also a type of API 4. Operating Systems can specify APIs for applications to interact with the device. For example, Android devices with camera requires an OS API to enable control of the camera by any app Each one these could have its own sub-categories. The scope of this book is limited to Web APIs . 1.3 What is a Web API? Web APIs are used for communication between a server and a client over the internet or any network. Typically, Web APIs use HTTP ( HyperText Transfer Protocol ) request methods, also known as HTTP verbs, to communicate with a server. HTTP standards ( RFCs 7231 & RFC 5789 but that is not important ) specify a set of “ request methods ” that indicates the action that is to be performed. These HTTP request methods are: 1. GET : “Gets” the specified resources from an endpoint. This results in a response from the server without any change in the state of the server 2. POST : Sends some data to an endpoint, typically resulting in an action that in turn changes the state of the server 3. PUT : Replaces some data on a server. Like POST but different in the sense that PUT requests will always produce the same result 4. DELETE : The DELETE method deletes the specified resource from server 5. HEAD : The HEAD method asks for a response like that of a GET request, but only the status line and header section 6. CONNECT : The CONNECT method establishes a tunnel to the server identified by the target resource 7. OPTIONS : The OPTIONS method is used to describe the communication options for the target resource. 8. TRACE : The TRACE method performs a message loop-back test along the path to the target resource. 9. PATCH : The PATCH method is used to apply partial modifications to a resource.
1.4 Getting our hands dirty (Please ensure the prerequisites as specified in the prerequisites chapter earlier are set up and satisfied before proceeding.) CoinDesk is a cryptocurrency news website, like Yahoo Finance , but for cryptocurrencies. CoinDesk offers a public and free API that provides the latest Bitcoin prices. We will interact with this CoinDesk API to fetch the latest Bitcoin prices using a classic utility cURL . But before that, open your favourite web browser (Firefox, Chrome, Edge, etc.) and open the below website address. https://api.coindesk.com/v1/bpi/currentprice.json You would see some seemingly cryptic text displayed on your browser that will be like below. { "time" :{ "updated" : "May 13, 2021 17:35:00 UTC" , "updatedISO" : "2021-05-13T17:35:00+00:00" , "updateduk" : "May 13, 2021 at 18:35 BST" }, "disclaimer" : "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org" , "chartName" : "Bitcoin" , "bpi" :{ "USD" :{ "code" : "USD" , "symbol" "$" , "rate" : "47,190.4555" , "description" : "United States Dollar" , "rate_float" : 47190.4555 }, "GBP" { "code" : "GBP" , "symbol" : "£" , "rate" : "33,618.8109" , "description" : "British Pound Sterling" "rate_float" : 33618.8109 }, "EUR" :{ "code" : "EUR" , "symbol" : "€" , "rate" : "39,064.2119" , "description" : "Euro" , "rate_float" : 39064.2119 }}} What is this garbage? It is a response that the API has sent. When you opened the URL above using a browser, what you have actually done is 1. Used your browser to send a “ GET ” request to the endpoint 2. The API then sent a response back with two component s a. An HTTP 200 OK Status indicating the request was valid b. A payload with data that is specified in the documentation 3. The browser then receives this data and renders it for you to see So, your browser just interacted with CoinDesk API . But this is not useful if one wants to build an app to track Bitcoin prices because they will have to open a browser, type the URL and then copy the data from the browser to be able to use it and, of course,
1.5 Getting hands dirty programmatically To use it in a program, we need to be able to get this data programmatically. So, let’s go one level deeper and try that out. On windows: Press ⊞ Windows Key-> Search Terminal -> Open -> then run: curl -i -X GET "https://api.coindesk.com/v1/bpi/currentprice.json" On Linux / Mac: Open Terminal and run: curl -i -X GET "https://api.coindesk.com/v1/bpi/currentprice.json" The URLs like the one above is also referred to as an “ endpoint ”. Once completed you will get a response like below: HTTP/ 1.1 200 OK Content-Type: application/javascript Content-Length: 679 Connection: keep-alive Access-Control-Allow-Origin: * Cache-Control: max-age= 15 Date: Mon, 10 May 2021 05 : 11 : 35 GMT Expires: Mon, 10 May 2021 05 : 12 : 07 UTC Server: nginx/ 1.18.0 X-Powered-By: Fat-Free Framework X-Cache: Hit from cloudfront Via: 1.1 b 83963 f 0701 c 4 af 7 f 684 fb 9 b 32 b 49 e 75 .cloudfront.net (CloudFront) X-Amz-Cf-Pop: DEL 54 -C 3 X-Amz-Cf-Id: INdmxs 7 vfcYBcjYRBWz_-YE 8 T 2 ZJfKi 0 QeZP 4 Z 8 y 2 mP 8 bbo 62 lqitg== { "time" :{ "updated" : "May 10, 2021 05:11:00 UTC" , "updatedISO" : "2021-05-10T05:11:00+00:00" , "updateduk" : "May 10, 2021 at 06:11 BST" }, "disclaimer" : "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org" , "chartName" : "Bitcoin" , "bpi" :{ "USD" :{ "code" : "USD" , "symbol"
"$" , "rate" : "59,387.4712" , "description" : "United States Dollar" , "rate_float" : 59387.4712 }, "GBP" { "code" : "GBP" , "symbol" : "£" , "rate" : "42,342.7919" , "description" : "British Pound Sterling" "rate_float" : 42342.7919 }, "EUR" :{ "code" : "EUR" , "symbol" : "€" , "rate" : "48,872.1474" , "description" : "Euro" , "rate_float" : 48872.1474 }}} 1.5.1 Explanation cURL a command line utility developed almost 25 years ago, its main function is to fetch the response from a URL. Using the flag ‘ i ’ with cURL includes protocol response headers in the output, and ‘ X ’ allows us to define the method used, in this case “ GET ”. And you can see, the first part of the response above are the headers while the second part is the content. Headers are useful in understanding the behaviour of the endpoint. The first line that says “ HTTP/1.1 200 O K ” is called a response status code . A “ 200 O K ” response code means your request was valid and it was successfully responded to. If you change anything in the endpoint URL and try again, you will get an “ HTTP/1.1 404 Not Foun d ” error meaning your request couldn’t be processed by the server. There are several such other response codes, most of them will be auto generated by our application. However, you can read about them here at Mozilla : https://developer.mozilla.org/en-US/docs/Web/HTTP/Status The content part of the response in curly braces { } follows a standard format called JSON that is used to store and transmit data to and from the endpoint. In this example, the API responds by sending the price of Bitcoin in USD , GBP , and EUR along with several other important information such as timestamp, descriptions, etc. Using this you can build a simple program that displays the price of Bitcoin to your users.
1.6 JavaScript Object Notation (JSON) JSON is composed of “ key ” and “ value ” pairs separated by a colon ‘:’ in the format {“key” : “value” } , a visual demonstration below The content in section 1.5 above seems garbled but once prettied the pattern becomes quite clear and clearly follows the key-value pair method. { "time" :{ "updated" : "May 10, 2021 05:11:00 UTC" , "updatedISO" : "2021-05-10T05:11:00+00:00" , "updateduk" : "May 10, 2021 at 06:11 BST" }, "disclaimer" : "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org" , "chartName" : "Bitcoin" , "bpi" :{ "USD" :{ "code" : "USD" , "symbol" : "$" , "rate" : "59,387.4712" , "description" : "United States Dollar" , "rate_float" : 59387.4712 }, "GBP" :{ "code" : "GBP" , "symbol" : "£" , "rate" : "42,342.7919" , "description" : "British Pound Sterling" , "rate_float" : 42342.7919 }, "EUR" :{ "code" : "EUR" , "symbol" : "€" , "rate" : "48,872.1474" ,
Comments 0
Loading comments...
Reply to Comment
Edit Comment