Skip to content

Commit 94f26e9

Browse files
add aut_api_fast and test_api_requests projects
1 parent 9fba6a7 commit 94f26e9

26 files changed

+759
-2
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Tech With Alex
3+
Copyright (c) 2022-2024 Tech With Alex / TechWithAlexDuta / Alexandru Duta
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# api-python
2-
API tutorials with python: create & test API
2+
API tutorials with python:
3+
- build REST API with FastAPI and Python: aut_api_fastapi
4+
- test REST API with requests and Python: test_api_requests
5+
6+
# YT channel
7+
Please check my YouTube channel for step by step implementation or detailed tutorials on automation and more: https://www.youtube.com/@TechWithAlexDuta
8+
9+
# License
10+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for de

aut_api_fastapi/.gitignore

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports / pytest-html
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
*.html
54+
*.css
55+
56+
# Translations
57+
*.mo
58+
*.pot
59+
60+
# Django stuff:
61+
*.log
62+
local_settings.py
63+
db.sqlite3
64+
db.sqlite3-journal
65+
66+
# Flask stuff:
67+
instance/
68+
.webassets-cache
69+
70+
# Scrapy stuff:
71+
.scrapy
72+
73+
# Sphinx documentation
74+
docs/_build/
75+
76+
# PyBuilder
77+
.pybuilder/
78+
target/
79+
80+
# Jupyter Notebook
81+
.ipynb_checkpoints
82+
83+
# IPython
84+
profile_default/
85+
ipython_config.py
86+
87+
# pyenv
88+
# For a library or package, you might want to ignore these files since the code is
89+
# intended to run in multiple environments; otherwise, check them in:
90+
# .python-version
91+
92+
# pipenv
93+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
95+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
96+
# install all needed dependencies.
97+
#Pipfile.lock
98+
99+
# poetry
100+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
101+
# This is especially recommended for binary packages to ensure reproducibility, and is more
102+
# commonly ignored for libraries.
103+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
104+
#poetry.lock
105+
106+
# pdm
107+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
108+
#pdm.lock
109+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
110+
# in version control.
111+
# https://pdm.fming.dev/#use-with-ide
112+
.pdm.toml
113+
114+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115+
__pypackages__/
116+
117+
# Celery stuff
118+
celerybeat-schedule
119+
celerybeat.pid
120+
121+
# SageMath parsed files
122+
*.sage.py
123+
124+
# Environments
125+
.env
126+
.venv
127+
env/
128+
venv/
129+
ENV/
130+
env.bak/
131+
venv.bak/
132+
133+
# Spyder project settings
134+
.spyderproject
135+
.spyproject
136+
137+
# Rope project settings
138+
.ropeproject
139+
140+
# mkdocs documentation
141+
/site
142+
143+
# mypy
144+
.mypy_cache/
145+
.dmypy.json
146+
dmypy.json
147+
148+
# Pyre type checker
149+
.pyre/
150+
151+
# pytype static type analyzer
152+
.pytype/
153+
154+
# Cython debug symbols
155+
cython_debug/
156+
157+
# PyCharm
158+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160+
# and can be added to the global gitignore or merged into this file. For a more nuclear
161+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162+
#.idea/

aut_api_fastapi/.vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"fastapi",
4+
"pydantic"
5+
]
6+
}

aut_api_fastapi/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022-2024 Tech With Alex / TechWithAlexDuta / Alexandru Duta
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

aut_api_fastapi/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# python
2+
Build simple API with FastAPI and Python
3+
4+
## Installation
5+
1. Make sure you have [.Python 3](https://www.python.org/downloads/) or newer installed on your machine (project was developed using python 3.12). Check installation
6+
```PS
7+
python -V
8+
```
9+
2. Clone this repository to your local machine.
10+
3. Open folder (`aut_api_fastapi`) in VS Code.
11+
4. Using terminal navigate to a project and run
12+
```PS
13+
pip install -r requirements.txt
14+
```
15+
16+
## Usage
17+
1. Open the project directory in VS Code or your preferred IDE.
18+
2. Open terminal and run the server with:
19+
```PS
20+
fastapi dev app\main.py
21+
```
22+
23+
## Tech
24+
- python 3.12
25+
- fastapi 0.111.0
26+
27+
## YT channel
28+
Please check my YouTube channel for step by step implementation or detailed tutorials on automation and more: https://www.youtube.com/@TechWithAlexDuta
29+
30+
## License
31+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

aut_api_fastapi/app/__init__.py

Whitespace-only changes.

aut_api_fastapi/app/main.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from typing import List
2+
from fastapi import FastAPI, HTTPException
3+
4+
from app.models import Product
5+
6+
# run: fastapi dev app\main.py
7+
8+
app = FastAPI()
9+
products = [Product(id=1, name="product_1", price=100.00)]
10+
11+
12+
@app.get("/", response_model=dict)
13+
def read_root():
14+
"""
15+
read root
16+
"""
17+
return {"message": "Products API!"}
18+
19+
20+
@app.get("/products", response_model=List[Product])
21+
def get_products():
22+
"""
23+
get all products
24+
"""
25+
return products
26+
27+
28+
@app.get("/products/{product_id}", response_model=Product)
29+
def get_product(product_id: int):
30+
"""
31+
get product by id
32+
"""
33+
for product in products:
34+
if product.id == product_id:
35+
return product
36+
raise HTTPException(status_code=404, detail="Product not found")
37+
38+
39+
@app.post("/products", response_model=Product)
40+
def create_product(product: Product):
41+
"""
42+
create product
43+
"""
44+
max_product_id = max(prod.id for prod in products)
45+
product.id = max_product_id + 1
46+
products.append(product)
47+
return product
48+
49+
50+
@app.put("/products/{product_id}", response_model=Product)
51+
def update_product(product_id: int, updated_product: Product):
52+
"""
53+
update product
54+
"""
55+
for index, product in enumerate(products):
56+
if product.id == product_id:
57+
updated_product.id = product_id
58+
products[index] = updated_product
59+
return updated_product
60+
raise HTTPException(status_code=404, detail="Product not found")
61+
62+
63+
@app.delete("/products/{product_id}", response_model=Product)
64+
def delete_product(product_id: int):
65+
"""
66+
delete product
67+
"""
68+
for index, product in enumerate(products):
69+
if product.id == product_id:
70+
return products.pop(index)
71+
raise HTTPException(status_code=404, detail="Product not found")

aut_api_fastapi/app/models.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pydantic import BaseModel
2+
3+
4+
class Product(BaseModel):
5+
"""
6+
Pydantic model
7+
"""
8+
9+
id: int
10+
name: str
11+
price: float

aut_api_fastapi/pyproject.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[project]
2+
name = "aut_api_fastapi"
3+
version = "1.0.0"
4+
dependencies = [
5+
"fastapi"
6+
]
7+
requires-python = ">=3.12"
8+
authors = [
9+
{name = "Alex Duta"}
10+
]
11+
maintainers = [
12+
{name = "Alex Duta"}
13+
]
14+
description = "Build API with FastAPI and Python"
15+
readme = "README.md"
16+
17+
[project.urls]
18+
Repository = "https://github.com/TechWithAlexDuta/api-python.git"
19+
20+
[tool.pytest.ini_options]

aut_api_fastapi/requirements.txt

104 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)