Skip to content

Commit

Permalink
Feature: set up initial FastAPI project
Browse files Browse the repository at this point in the history
1. add requirements.txt
2. Add one HelloWorld API method:  main.py
3. Add a README file that describes how to run this FastAPI project
4. Set up testing with coverage and mocker: test_main.py
5. Set up GitHub actions for automatic testing: actions.yml
6. add .gitignore
7. add requirements.txt

issue #1
  • Loading branch information
Luna-Jia committed Dec 15, 2023
1 parent ee023e7 commit e18aa33
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 8 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: test
run-name: ${{ github.actor }} is testing
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- run: python -m pip install --upgrade pip
- run: pip install -r requirements.txt
- run: pip install pytest-cov
- run: pip install pytest-mock
- run: pytest --cov=.
128 changes: 128 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
## Overview
# FastAPI Project Guide

This final project test your skills in Git, GitHub, Python, virtual environment, Web APIs, FastAPIs, and testing.
###### This project is built using FastAPI, a modern, fast web framework for building APIs with Python.

### Final Project
#### Follow the following steps to run the project:

The final project is to create a FastAPI app that finds movie review videos. In total, the app has four API methods.
1. download the project to your machine
`git clone [email protected]:COSC381-2023Fall/final-project-Luna-Jia.git`

/ "Hello World"
/moviereviews/{movie_name} Retrieve 10 movie review videos (Issue 2)
/moviereviews/{language}/{movie_name} Retrieve 10 movie review videos in a specific language (Issue 4)
/descriptions/{video_id} Retrieve a complete description of a video (Issue 3)
2. at the project directory, create vertual enviroment
`python3 -m venv .venv`

3. run vertual enviroment
`source .venv/bin/activate`

4. make sure pip is pointing to the right path
`which pip`

5. To install the dependencies in batch(set up a virtual environement), run the command:
`python3 -m pip install -r requirements.txt`

6. To run the application, use the following command:
`uvicorn main:app --reload`

7. In the browser, go to `http://127.0.0.1:8000`, the webpage will return a JSON response:
`{"Hello": "world"}`

8. Run test
Open terminal, run `pytest --cov=.`
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Union
from fastapi import FastAPI

app = FastAPI()

# Process the HTTP get request for "/"
@app.get("/")
def read_root():
return {"Hello": "world"}
32 changes: 32 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
annotated-types==0.6.0
anyio==3.7.1
certifi==2023.11.17
click==8.1.7
dnspython==2.4.2
email-validator==2.1.0.post1
exceptiongroup==1.2.0
fastapi==0.105.0
h11==0.14.0
httpcore==1.0.2
httptools==0.6.1
httpx==0.25.2
idna==3.6
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
orjson==3.9.10
pydantic==2.5.2
pydantic-extra-types==2.2.0
pydantic-settings==2.1.0
pydantic_core==2.14.5
python-dotenv==1.0.0
python-multipart==0.0.6
PyYAML==6.0.1
sniffio==1.3.0
starlette==0.27.0
typing_extensions==4.9.0
ujson==5.9.0
uvicorn==0.24.0.post1
uvloop==0.19.0
watchfiles==0.21.0
websockets==12.0
9 changes: 9 additions & 0 deletions test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"Hello": "world"}

0 comments on commit e18aa33

Please sign in to comment.