├── .gitignore ├── Procfile ├── README.md ├── app.py ├── requirements.txt └── runtime.txt /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: uvicorn app:app --host=0.0.0.0 --port=${PORT:-5000} 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rest API FastAPI 2 | simple rest api example using FastApi, and heroku deploy config 3 | 4 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI, HTTPException 2 | from pydantic import BaseModel 3 | from typing import Optional, Text 4 | from datetime import datetime 5 | from uuid import uuid4 as uuid 6 | import uvicorn 7 | 8 | app = FastAPI() 9 | 10 | posts = [] 11 | 12 | # Post model 13 | class Post(BaseModel): 14 | id: Optional[str] 15 | title: str 16 | author: str 17 | content: Text 18 | created_at: datetime = datetime.now() 19 | published_at: Optional[datetime] 20 | published: Optional[bool] = False 21 | 22 | @app.get('/') 23 | def read_root(): 24 | return {"welcome": "Welcome to my API"} 25 | 26 | @app.get('/posts') 27 | def get_posts(): 28 | return posts 29 | 30 | @app.post('/posts') 31 | def save_post(post: Post): 32 | post.id = str(uuid()) 33 | posts.append(post.dict()) 34 | return posts[-1] 35 | 36 | @app.get('/posts/{post_id}') 37 | def get_post(post_id: str): 38 | for post in posts: 39 | if post["id"] == post_id: 40 | return post 41 | raise HTTPException(status_code=404, detail="Item not found") 42 | 43 | @app.delete('/posts/{post_id}') 44 | def delete_post(post_id: str): 45 | for index, post in enumerate(posts): 46 | if post["id"] == post_id: 47 | posts.pop(index) 48 | return {"message": "Post has been deleted succesfully"} 49 | raise HTTPException(status_code=404, detail="Item not found") 50 | 51 | @app.put('/posts/{post_id}') 52 | def update_post(post_id: str, updatedPost: Post): 53 | for index, post in enumerate(posts): 54 | if post["id"] == post_id: 55 | posts[index]["title"]= updatedPost.dict()["title"] 56 | posts[index]["content"]= updatedPost.dict()["content"] 57 | posts[index]["author"]= updatedPost.dict()["author"] 58 | return {"message": "Post has been updated succesfully"} 59 | raise HTTPException(status_code=404, detail="Item not found") 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asgiref==3.4.1 2 | certifi==2021.5.30 3 | click==8.0.1 4 | et-xmlfile==1.1.0 5 | fastapi==0.65.3 6 | h11==0.12.0 7 | openpyxl==3.0.7 8 | pydantic==1.8.2 9 | starlette==0.14.2 10 | typing-extensions==3.10.0.0 11 | uvicorn==0.14.0 12 | -------------------------------------------------------------------------------- /runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.9.6 2 | --------------------------------------------------------------------------------