-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
120 lines (98 loc) · 3.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from fastapi import Depends, FastAPI
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from starlette.requests import Request
from starlette.responses import Response
from DataBase.db_scrapping import Githuber, Repository, Star, Follower, Following, insert_data
from DataBase.db_scrapping import insert_data
import json
SQLALCHEMY_DATABASE_URI = 'sqlite:///databases.db'
engine = create_engine(
SQLALCHEMY_DATABASE_URI, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# db_session = SessionLocal()
# Utility
def get_user(db_session: Session, user_acc: str):
user = db_session.query(Githuber).filter(Githuber.useracc == user_acc).first()
repository = db_session.query(Repository).filter(Repository.useracc_id == user_acc).all()
stars = db_session.query(Star).filter(Star.useracc_id == user_acc).all()
followers = db_session.query(Follower).filter(Follower.useracc_id == user_acc).all()
followings = db_session.query(Following).filter(Following.useracc_id == user_acc).all()
obj = {
"githuber": user,
"repository": repository,
"stars": stars,
"followers": followers,
"followings": followings,
}
return obj
def get_repo(db_session: Session, user_acc: str):
return db_session.query(Repository).filter(Repository.useracc_id == user_acc).all()
# Dependency
def get_db(request: Request):
return request.state.db
# FastAPI specific code
app = FastAPI()
@app.get("/githubers/{user_acc}")
def read_user(user_acc: str, db: Session = Depends(get_db)):
user = get_user(db, user_acc=user_acc)
return user
@app.get("/repository/{user_acc}")
def read_repository(user_acc: str, db: Session = Depends(get_db)):
repo = get_repo(db, user_acc=user_acc)
return repo
@app.post("/githubers/{user_acc}")
def create_user(user_acc: str):
url = "https://github.com/" + user_acc
insert_data(url)
@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
response = Response("Internal server error", status_code=500)
try:
request.state.db = SessionLocal()
response = await call_next(request)
finally:
request.state.db.close()
return response
# from typing import List, Set
# from fastapi import FastAPI
# from pydantic import BaseModel, UrlStr
# app = FastAPI()
# class Repository(BaseModel):
# repo : str
# used_lang : str
# user_acc : str
# class Star(BaseModel):
# repo_starred : str
# used_lang_starred : str
# user_acc : str
# class Follower(BaseModel):
# useracc : str
# username : str
# bio : str
# location : str
# class Following(BaseModel):
# useracc : str
# username : str
# bio : str
# location : str
# class Githuber(BaseModel):
# # name: str
# # description: str = None
# # price: float
# # tax: float = None
# # tags: Set[str] = []
# # images: List[Image] = None
# useracc : str
# username : str
# bio : str
# location : str
# repository : List[Repository] = None
# star : List[Star] = None
# follower : List[Follower] = None
# following : List[Following] = None
# @app.put("/items/{item_id}")
# async def update_item(*, item_id: int, item: Item):
# results = {"item_id": item_id, "item": item}
# return results