Reformat files and update flake ignore list

This commit is contained in:
Santiago Lo Coco 2023-10-25 10:38:01 -03:00
parent 56839570bc
commit 6a905e1c0e
11 changed files with 70 additions and 47 deletions

View File

@ -13,7 +13,7 @@ else
## Linting
flake8 src --extend-ignore E221
flake8 src --extend-ignore E221 --extend-ignore E501
# black src --check
# isort src --check

View File

@ -12,7 +12,7 @@ else
## Linting
flake8 src --extend-ignore E221
flake8 src --extend-ignore E221 --extend-ignore E501
# black src --check
# isort . --src-path src --check

View File

@ -14,7 +14,7 @@ else
## Linting
flake8 src --extend-ignore E221
flake8 src --extend-ignore E221 --extend-ignore E501
# black src --check
# isort . --src-path src --check

View File

@ -1,3 +1,3 @@
API_USERS = "http://127.0.0.1:5001/users/"
API_FLIGHTS = "http://127.0.0.1:5000/flights/"
API_AUTH = "http://127.0.0.1:5001/auth/"
API_AUTH = "http://127.0.0.1:5001/auth/"

View File

@ -19,4 +19,4 @@ app.add_middleware(
allow_credentials=True,
allow_methods=["POST", "GET", "PUT", "DELETE", "OPTIONS"],
allow_headers=["*"],
)
)

View File

@ -1,18 +1,20 @@
from typing import Annotated, Optional
from typing import Annotated
from fastapi import APIRouter, Depends, Header, HTTPException, Request, status
from fastapi import APIRouter, Header, HTTPException
from src.api.config import API_AUTH
from src.api.schemas.auth import RefreshToken, Token
from src.api.schemas.user import User, UserLogin, UserMin, UserRegister
from src.api.utils.network import make_request, request
from src.api.schemas.user import UserLogin, UserMin, UserRegister
from src.api.utils.network import request
router = APIRouter()
@router.post("/register", response_model=UserMin)
async def register(user: UserRegister):
(response, status, _) = await request(f'{API_AUTH}register', "POST", json=user.model_dump())
(response, status, _) = await request(
f"{API_AUTH}register", "POST", json=user.model_dump()
)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@ -20,22 +22,28 @@ async def register(user: UserRegister):
@router.post("/login", response_model=Token)
async def login(user: UserLogin):
(response, status, _) = await request(f'{API_AUTH}login', "POST", json=user.model_dump())
(response, status, _) = await request(
f"{API_AUTH}login", "POST", json=user.model_dump()
)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@router.post("/refresh", response_model=Token)
async def refresh(token: RefreshToken):
(response, status, _) = await request(f'{API_AUTH}refresh', "POST", json=token.model_dump())
(response, status, _) = await request(
f"{API_AUTH}refresh", "POST", json=token.model_dump()
)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@router.get("/status", response_model=UserMin)
async def status(authorization: Annotated[str | None, Header()] = None):
header = {'Authorization': authorization if authorization is not None else ''}
(response, status, _) = await request(f'{API_AUTH}status', "GET", headers=header)
header = {"Authorization": authorization if authorization is not None else ""}
(response, status, _) = await request(f"{API_AUTH}status", "GET", headers=header)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response

View File

@ -1,38 +1,46 @@
import asyncio
from typing import Annotated, Optional
import aiohttp
from fastapi import APIRouter, Depends, Header, HTTPException, Request, status
from fastapi import APIRouter, Header, HTTPException
from src.api.config import API_FLIGHTS
from src.api.routes.auth import status as checkAuth
from src.api.schemas.flight import Flight, FlightCreate, FlightStatusUpdate
from src.api.utils.network import make_request, request
from src.api.utils.network import request
router = APIRouter()
@router.get("/{id}", response_model=Flight)
async def get_flight_by_id(id: int):
(response, status, _) = await request(f'{API_FLIGHTS}{id}', "GET")
(response, status, _) = await request(f"{API_FLIGHTS}{id}", "GET")
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@router.post("", response_model=Flight)
async def create_flight(flight: FlightCreate, authorization: Annotated[str | None, Header()] = None):
async def create_flight(
flight: FlightCreate, authorization: Annotated[str | None, Header()] = None
):
await checkAuth(authorization)
(response, status, _) = await request(f'{API_FLIGHTS}', "POST", json=flight.model_dump())
(response, status, _) = await request(
f"{API_FLIGHTS}", "POST", json=flight.model_dump()
)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@router.patch("/{id}", response_model=Flight)
async def update_flight(id: int, status_update: FlightStatusUpdate, authorization: Annotated[str | None, Header()] = None):
async def update_flight(
id: int,
status_update: FlightStatusUpdate,
authorization: Annotated[str | None, Header()] = None,
):
await checkAuth(authorization)
(response, status, _) = await request(f'{API_FLIGHTS}{id}', "PATCH", json=status_update.model_dump())
(response, status, _) = await request(
f"{API_FLIGHTS}{id}", "PATCH", json=status_update.model_dump()
)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@ -42,10 +50,10 @@ async def update_flight(id: int, status_update: FlightStatusUpdate, authorizatio
async def get_flights(origin: Optional[str] = None, lastUpdated: Optional[str] = None):
query = {}
if origin:
query['origin'] = origin
query["origin"] = origin
if lastUpdated:
query['lastUpdated'] = lastUpdated
(response, status, _) = await request(f'{API_FLIGHTS}', "GET", query=lastUpdated)
query["lastUpdated"] = lastUpdated
(response, status, _) = await request(f"{API_FLIGHTS}", "GET", query=lastUpdated)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response

View File

@ -1,45 +1,51 @@
from typing import Optional
from fastapi import APIRouter, Depends, Header, HTTPException, Request, status
from fastapi import APIRouter, HTTPException
from src.api.config import API_USERS
from src.api.schemas.user import User, UserLogin, UserRegister
from src.api.utils.network import make_request, request
from src.api.schemas.user import User, UserRegister
from src.api.utils.network import request
router = APIRouter()
@router.get("", response_model=list[User])
async def get_users():
(response, status, _) = await request(f'{API_USERS}', "GET")
(response, status, _) = await request(f"{API_USERS}", "GET")
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@router.post("", response_model=User)
async def create_users(user: UserRegister):
(response, status, _) = await request(f'{API_USERS}', "POST", json=user.dump_model())
(response, status, _) = await request(
f"{API_USERS}", "POST", json=user.dump_model()
)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@router.get("/{id}", response_model=User)
async def get_user(id: str):
(response, status, _) = await request(f'{API_USERS}{id}', "GET")
(response, status, _) = await request(f"{API_USERS}{id}", "GET")
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@router.put("/{id}", response_model=User)
async def update_user(user: UserRegister):
(response, status, _) = await request(f'{API_USERS}{id}', "PUT", json=user.model_dump())
(response, status, _) = await request(
f"{API_USERS}{id}", "PUT", json=user.model_dump()
)
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response
@router.delete("/{id}", response_model=User)
async def update_user():
(response, status, _) = await request(f'{API_USERS}{id}', "DELETE")
async def delete_user():
(response, status, _) = await request(f"{API_USERS}{id}", "DELETE")
if status < 200 or status > 204:
raise HTTPException(status_code=status, detail=response)
return response

View File

@ -1,7 +1,4 @@
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, validator
from pydantic import BaseModel
class Token(BaseModel):
@ -9,5 +6,6 @@ class Token(BaseModel):
refresh_token: str
user_id: int
class RefreshToken(BaseModel):
refresh_token: str

View File

@ -1,6 +1,4 @@
from datetime import datetime
from pydantic import BaseModel, validator
from pydantic import BaseModel
class User(BaseModel):
@ -10,16 +8,19 @@ class User(BaseModel):
created_date: str
airline: bool
class UserMin(BaseModel):
id: int
username: str
email: str
class UserRegister(BaseModel):
username: str
email: str
password: str
class UserLogin(BaseModel):
email: str
password: str

View File

@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Optional
import aiohttp
import async_timeout
@ -25,9 +25,11 @@ async def make_request(
return decoded_json, response.status, response.headers
async def request(url, method, headers = None, data = None, json = None, query = None):
async def request(url, method, headers=None, data=None, json=None, query=None):
try:
(x, y, z) = await make_request(url=url, method=method, headers=headers, data=data, json=json, query=query)
(x, y, z) = await make_request(
url=url, method=method, headers=headers, data=data, json=json, query=query
)
except ClientConnectorError:
raise HTTPException(status_code=503, detail="Service is unavailable.")
except ContentTypeError: