Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building Python Web APIs with FastAPI

You're reading from   Building Python Web APIs with FastAPI A fast-paced guide to building high-performance, robust web APIs with very little boilerplate code

Arrow left icon
Product type Paperback
Published in Jul 2022
Publisher Packt
ISBN-13 9781801076630
Length 216 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Abdulazeez Abdulazeez
Author Profile Icon Abdulazeez
Abdulazeez
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Part 1: An Introduction to FastAPI
2. Chapter 1: Getting Started with FastAPI FREE CHAPTER 3. Chapter 2: Routing in FastAPI 4. Chapter 3: Response Models and Error Handling 5. Chapter 4: Templating in FastAPI 6. Part 2: Building and Securing FastAPI Applications
7. Chapter 5: Structuring FastAPI Applications 8. Chapter 6: Connecting to a Database 9. Chapter 7: Securing FastAPI Applications 10. Part 3: Testing And Deploying FastAPI Applications
11. Chapter 8: Testing FastAPI Applications 12. Chapter 9: Deploying FastAPI Applications 13. Other Books You May Enjoy

Git basics

Git is a version control system that enables developers to record, keep track, and revert to earlier versions of files. It is a decentralized and lightweight tool that can be installed on any operating system.

You will be learning how to use Git for record-keeping purposes. As each layer of the application is being built, changes will be made, and it’s important that these changes are kept note of.

Installing Git

To install Git, visit the downloads page at https://git-scm.com/downloads and select a download option for your current operating system. You’ll be redirected to an instructional page on how to install Git on your machine.

It is also worth noting that Git comes as a CLI and a GUI application. Therefore, you can download the one that works best for you.

Git operations

As mentioned earlier, Git can be used to record, track, and revert to earlier versions of a file. However, only the basic operations of Git will be used in this book and will be introduced in this section.

In order for Git to run properly, folders housing files must be initialized. Initializing folders enables Git to keep track of the content except otherwise exempted.

To initialize a new Git repository in your project, you need to run the following command in your terminal:

$ git init

To enable tracking of files, a file must first be added and committed. A Git commit enables you to track file changes between timeframes; for example, a commit made an hour ago and the current file version.

What Is a Commit?

A commit is a unique capture of a file or folder status at a particular time, and it is identified by a unique code.

Now that we know what a commit is, we can go ahead and commit a file as follows:

$ git add hello.txt
$ git commit -m "Initial commit"

You can track the status of your files after making changes by running the following command:

$ git status

Your terminal should look similar to the following:

Figure 1.1 – Git commands

Figure 1.1 – Git commands

To view the changes made to the file, which can be additions or subtractions from the file contents, run the following command:

$ git diff

Your terminal should look similar to the following:

Figure 1.2 – Output from the git diff command

Figure 1.2 – Output from the git diff command

It is good practice to include a .gitignore file in every folder. The .gitignore file contains the names of files and folders to be ignored by Git. This way, you can add and commit all the files in your folder without the fear of committing files like .env.

To include a .gitignore file, run the following command in your terminal:

$ touch .gitignore

To exempt a file from being tracked by Git, add it to the .gitignore file as follows:

$ echo ".env" >> .gitignore

Common files contained in a .gitignore file include the following:

  • Environment files (*.env)
  • Virtualenv folder (env, venv)
  • IDE metadata folders (such as .vscode and .idea)

Git branches

Branches are an important feature that enables developers to easily work on different application features, bugs, and so on, separately before merging into the main branch. The system of branching is employed in both small-scale and large-scale applications and promotes the culture of previewing and collaborations via pull requests. The primary branch is called the main branch and it is the branch from which other branches are created.

To create a new branch from an existing branch, we run the git checkout -b newbranch command. Let’s create a new branch by running the following command:

$ git checkout -b hello-python-branch

The preceding command creates a new branch from the existing one, and then sets the active branch to the newly created branch. To switch back to the original main branch, we run git checkout main as follows:

$ git checkout main

Important Note

Running git checkout main makes main the active working branch, whereas git checkout -b newbranch creates a new branch from the current working branch and sets the newly created branch as the active one.

To learn more, refer to the Git documentation: http://www.git-scm.com/doc.

Now that we have learned the basics of Git, we can now proceed to learn about how to create isolated environments with virtualenv.

You have been reading a chapter from
Building Python Web APIs with FastAPI
Published in: Jul 2022
Publisher: Packt
ISBN-13: 9781801076630
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime