├── Day 1 └── Day1README.md ├── README.md ├── TestingFastAPIEndPoints.md └── prac ├── Dockerfile ├── app.py ├── requirements.txt └── server.py /Day 1/Day1README.md: -------------------------------------------------------------------------------- 1 | Hello @everyone, welcome to our **Python Fast API and Docker BootCamp Day 1** , We usually starts our boot camp by learning, Git and GitHub here is what you are suppose to at least look at on day 1 👇🏻: 2 | 3 | Day 1: Introduction to Python, Git and Github. 4 | 5 | What you are expected to learn on Day 1: 6 | * Introduction to Python for Web Development 7 | * Learn how to create a “repository” (project) with a git hosting tool (like GitHub or Bitbucket) 8 | * Learn how to copy/clone the repository to your local machine 9 | * Learn how to add a file to your local repo and “commit” (save) the changes 10 | * Learn how to “Push” your changes to your master branch 11 | * Learn how to make a change to your file with a git hosting tool and commit 12 | * Learn how to “Pull” the changes to your local machine 13 | * Learn how to create a “branch” (version), make a change, commit the change 14 | * Learn how to open a “pull request”. 15 | * Learn how to “Merge” your branch to the master branch 16 | 17 | References 👇🏻: 18 | 19 | Few announcements for those who didn’t join the onboarding session on Saturday: 20 | To get the certificate of completion you have to at least complete the first week of the boot camp and track your progress in these google forms :point_right::skin-tone-2: https://forms.gle/1TvMYRMvo3CrNkaN7, that is today, Monday to Friday, write and submit at least one technical article on a topic which will be given out tomorrow.  21 | 22 | Your article should be in one of the following topics:  23 | 1). Getting Started with Fast API and Docker.  24 | 2). Ultimate Docker and Fast API Guide  25 | 3). Fast API and Docker Play Book   26 | 4). Fast API and Docker CheatSheet  27 | 28 | We will be having two classes per week 8:00PM to 9:45PM EAT on Tuesday and Fridays.  29 | 30 | Important Link:  31 | 1). WhatsApp peer group :point_right::skin-tone-2: https://chat.whatsapp.com/H0wo1lrkufv8y1qbGXnIoK 32 | 2). Slack channel invite link :point_right::skin-tone-2: https://join.slack.com/t/luxtechacademy/shared_invite/zt-wyrywawn-ImHODjmMeXvqa3ZUs5S8xg 33 | 34 | Every evening we will be sharing reference materials and you can join our 20 minutes quick catch up meeting everyday at 8:00 PM we will be using the same link we used on Saturday. All the best, may your smart hard work pay 35 | 36 | After the two weeks of learning, there will be a capstone project that you have to create in 5 days, and the best 5 projects gets a give away from Lux Academy and Data Science East Africa while the best project gets a giveaway + a boot paperback. 37 | 38 | 39 | Python Tutorials: 40 | 41 | 42 | ### **Git and GitHub Basics:** 43 | Git is a version control system which comes handy when you're writing code as a team or independently. Some features that git allows includes; creation, merging, and deletion of multiple local branches that can be entirely independent of each other. 44 | 45 | ### **Installation** 46 | 47 | Visit [https://git-scm.com/downloads] and use the git installer and follow the steps, after you have successfully installed, check for successful installation and version using the following command. 48 | 49 | ```python 50 | git --version 51 | ``` 52 | **Set global configurations** 53 | Now lets configure git by running the following global commands. 54 | 55 | ```python 56 | git config --global user.name "user name" 57 | git config --global user.email "email address" 58 | ``` 59 | 60 | **git init** 61 | Initializes your local git repository as your master. 62 | 63 | **git add . or git add "file name"** 64 | To add all the files to staging area, run 65 | ```python 66 | git add . 67 | ``` 68 | or add specific file using 69 | 70 | ```python 71 | git add 72 | ``` 73 | 74 | **git commit -m "commit message"** 75 | To save the changes to your repository with a message which describes the commit. 76 | 77 | **git remote add origin "repository URL"** 78 | 79 | When you have an online repository and would like to link it to your local repository, you can do so by running. 80 | 81 | git remote add origin 82 | 83 | **git push -u origin master** 84 | 85 | To push the local repository to the online repository. 86 | 87 | **git remote rename origin upstream** 88 | Running this command will rename the online repository. Then add the new URL using "git remote add origin " and push the local to the online using "git push -u origin master" 89 | 90 | **git clone ** 91 | 92 | To clone an online repository to your local machine. 93 | 94 | **git revert** 95 | Will reverse changes made by an earlier commit. 96 | 97 | **git status** 98 | To view the current status of the repository. 99 | 100 | **git checkout** 101 | View file from last commit and add it to staging area. 102 | 103 | **git restore --staged** 104 | Unstage files from last commit. 105 | 106 | **git restore** 107 | Restore file to original last commit. 108 | 109 | **git log --online** 110 | Show all commits. 111 | 112 | **git log** 113 | 114 | Display detail information about all commits. 115 | 116 | git -b "branch name" 117 | Running this command will create a new branch. 118 | 119 | **git branch -a** 120 | Show all branches. 121 | 122 | **git checkout "branch name"** 123 | 124 | Will move git from the master branch to the new branch. 125 | 126 | **git merge "branch name"** 127 | 128 | If there are new changes that needs to be added to the master, you can do so by switching back to the master branch. 129 | 130 | **git branch -a** 131 | 132 | To list branches. 133 | 134 | **git pull** 135 | 136 | To match the remote repository to the local. 137 | 138 | 139 | 140 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Good programming language frameworks make it easy to produce quality products faster. Great frameworks even make the whole development experience enjoyable. **FastAPI** is a modern, fast (high-performance), a web framework for building APIs with Python. It is easy to learn, fast to code, and production-ready. The most exciting feature of FastAPI is that it supports asynchronous code out of the box using the async/await Python keywords. Testing FastAPI endpoints are really straightforward and can be done using TestClient provided by FastAPI. This makes Test Driven Development(TDD) very easy. It us also easy to deploy your Fast API application and APIs. 2 | 3 | > FastAPI is a Python web framework that has been built from the ground up to make use of modern Python features. It uses the ASGI standard for asynchronous, concurrent connectivity with clients, and it can work with WSGI if needed. Async functions can be used for routes and endpoints. And FastAPI allows web applications to be written efficiently in clean, modern Python code with type hints. 4 | 5 | **The following features make FastAPI worth trying:** 6 | * Speed: FastAPI is one of the fastest Python web frameworks. In fact, its speed is at par with Node.js and Go. Check these performance tests. 7 | * Detailed and easy-to-use developer docs 8 | * Type hint your code and get free data validation and conversion. 9 | * Create plugins easily using dependency injection. 10 | 11 | **Docker** is an open source platform for building, deploying, and managing containerized applications. It enables developers to package applications into containers—standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment. Containers simplify delivery of distributed applications, and have become increasingly popular as organizations shift to cloud-native development and hybrid multicloud environments. 12 | 13 | Developers can create containers without Docker, but the platform makes it easier, simpler, and safer to build, deploy and manage containers. Docker is essentially a toolkit that enables developers to build, deploy, run, update, and stop containers using simple commands and work-saving automation through a single API. 14 | 15 | 16 | **Major Technologies & Tools:** 17 | - Python 3.6+ 18 | - Fast API 19 | - Docker 20 | - Postman 21 | 22 | 23 | **More tools:** 24 | * Git and GitHub — Source code management (Version Control System) 25 | 26 | * Selenium — Automation testing 27 | 28 | * Docker — Software Containerization Platform 29 | 30 | * Kubernetes — Container Orchestration tool 31 | 32 | * Ansible — Configuration Management and Deployment 33 | 34 | * Terraform - An open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files. 35 | 36 | 37 | 38 | --- 39 | ### **Day 1: Introduction to Python, Git and Github** 40 | 41 | **What you are expected to learn on Day 1:** 42 | 43 | - Introduction to Python for Web Development 44 | - Learn how to create a “repository” (project) with a git hosting tool (like GitHub or Bitbucket) 45 | - Learn how to copy/clone the repository to your local machine 46 | - Learn how to add a file to your local repo and “commit” (save) the changes 47 | - Learn how to “Push” your changes to your master branch 48 | - Learn how to make a change to your file with a git hosting tool and commit 49 | - Learn how to “Pull” the changes to your local machine 50 | - Learn how to create a “branch” (version), make a change, commit the change 51 | - Learn how to open a “pull request”. 52 | - Learn how to “Merge” your branch to the master branch 53 | 54 | --- 55 | 56 | 57 | --- 58 | **Day 2: Introduction to Fast API** 59 | 60 | **What you are expected to learn on Day 2 ( Virtual Class ) :** 61 | 62 | 63 | --- 64 | 65 | 66 | -------------------------------------------------------------------------------- /TestingFastAPIEndPoints.md: -------------------------------------------------------------------------------- 1 | ## **Testing FastAPI Endpoints** 2 | 3 | 4 | Once you have developed your Applications Programming Interfaces(APIs) using Fast API, the next step is to consider adding automated tests for your API endpoints. You can achieve this using pytest and a TestClient class derived from Starlette. 5 | 6 | To get started, let’s consider a bare bones API that returns fruit information. 7 | 8 | ```python 9 | from fastapi import FastAPI, HTTPException 10 | app = FastAPI() 11 | 12 | fake_db = {} 13 | fake_db[1] = "apple" 14 | fake_db[2] = "banana" 15 | fake_db[3] = "orange" 16 | 17 | @app.get("/fruit/{id}") 18 | async def echo(id: int): 19 | if id in fake_db: 20 | return {"fruit": fake_db[id]} 21 | else: 22 | raise HTTPException(status_code=404, detail="id not found") 23 | ``` 24 | Here I have created a fake database with just three fruit, and a single fruit/id endpoint. Let’s assume that this API is located in the api package, and we run it like so: 25 | 26 | ```python 27 | uvicorn api.fruit:app — reload 28 | ``` 29 | 30 | You can then try the endpoint by going to: http://localhost:8000/fruit/1 and verify that you get one apple back. 31 | So far, so good. The next step is to add a test suite. To do so, we create a fastapi.testclient.TestClient object, and then define our tests via the standard pytest conventions. For example, we prefix each test method with test_ and use assert to validate responses from the API. 32 | 33 | Here is sample code: 34 | 35 | ```python 36 | 37 | from fastapi.testclient import TestClient 38 | from api.fruit import app 39 | 40 | client = TestClient(app) 41 | 42 | def test_valid_id(): 43 | response = client.get("/fruit/1") 44 | assert response.status_code == 200 45 | assert response.json() == {"fruit": "apple"} 46 | ``` 47 | 48 | Here we are calling fruit with an ID of 1, and verifying the response with assert. To run the test, just run pytest like so: 49 | 50 | ```python 51 | pytest 52 | ``` 53 | 54 | Pytest will automatically collect all your tests and run them sequentially. If all goes well, you should also then see all your tests pass. 55 | You can then easily add additional test cases for error conditions or edge cases. 56 | 57 | For example, the following code tests for an invalid identifier: 58 | 59 | 60 | ```python 61 | def test_invalid_id1(): 62 | response = client.get("/fruit/99") 63 | assert response.status_code == 404 64 | assert response.json() == {'detail': 'id not found'} 65 | ``` 66 | 67 | And, the following code tests for a non-integer identifier: 68 | 69 | ```python 70 | 71 | def test_invalid_id2(): 72 | response = client.get("/fruit/abc") 73 | assert response.status_code == 422 74 | assert response.json()["detail"][0]["msg"] == "value is not a valid integer" 75 | ``` 76 | 77 | Hopefully, you can now see how easy it is to add automated tests for your API endpoints. For further information, refer to the [official FastAPI tutorial on Testing](https://fastapi.tiangolo.com/tutorial/testing/). 78 | -------------------------------------------------------------------------------- /prac/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.6 2 | COPY . . 3 | COPY ./requirements.txt ./requirements.txt 4 | EXPOSE 8000:8000 5 | RUN pip3 install -r requirements.txt 6 | CMD ["python3", "server.py"] 7 | 8 | -------------------------------------------------------------------------------- /prac/app.py: -------------------------------------------------------------------------------- 1 | name = "Lux Academy" 2 | 3 | print(f"hello everyone welcome to {name}") -------------------------------------------------------------------------------- /prac/requirements.txt: -------------------------------------------------------------------------------- 1 | anyio==3.3.4 2 | asgiref==3.4.1 3 | click==8.0.3 4 | fastapi==0.70.0 5 | h11==0.12.0 6 | idna==3.3 7 | pydantic==1.8.2 8 | python-multipart==0.0.5 9 | six==1.16.0 10 | sniffio==1.2.0 11 | starlette==0.16.0 12 | typing-extensions==3.10.0.2 13 | uvicorn==0.15.0 14 | -------------------------------------------------------------------------------- /prac/server.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI, Form 2 | import uvicorn 3 | 4 | app = FastAPI() 5 | 6 | @app.get("/") 7 | async def read_root(): 8 | return { "Lux app": "Welcome to Lux app" } 9 | 10 | @app.post("/login/") 11 | async def login(username: str = Form(...), password: str = Form(...)): 12 | return {"username": username, "password": password} 13 | 14 | if __name__ == "__main__": 15 | uvicorn.run('server:app', port=8000, reload=False) --------------------------------------------------------------------------------