Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
viacep api and correct expires token
  • Loading branch information
otitamario committed Nov 21, 2022
commit deee3095d1fcee42838d9500589dab2c5a28dce3
8 changes: 4 additions & 4 deletions backend/app/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def login(payload: schemas.LoginUserSchema, response: Response, db: Session = De

# Create access token
access_token = Authorize.create_access_token(
subject=str(user.username), expires_time=int(time.time()+ACCESS_TOKEN_EXPIRES_IN))
subject=str(user.username), expires_time=timedelta(minutes=ACCESS_TOKEN_EXPIRES_IN))

# Create refresh token
refresh_token = Authorize.create_refresh_token(
subject=str(user.username), expires_time=int(time.time()+REFRESH_TOKEN_EXPIRES_IN))
subject=str(user.username), expires_time=timedelta(minutes=REFRESH_TOKEN_EXPIRES_IN))

token_response={'status': 'success','user':user.username ,'token':{'access_token':access_token,'refresh_token':refresh_token}}

Expand Down Expand Up @@ -93,11 +93,11 @@ def refresh_token(response: Response, request: Request, Authorize: AuthJWT = Dep
detail='The user belonging to this token no logger exist')
# Create access token
access_token = Authorize.create_access_token(
subject=str(user.username), expires_time=int(time.time()+ACCESS_TOKEN_EXPIRES_IN))
subject=str(user.username), expires_time=timedelta(minutes=ACCESS_TOKEN_EXPIRES_IN))

# Create refresh token
refresh_token = Authorize.create_refresh_token(
subject=str(user.username), expires_time=int(time.time()+REFRESH_TOKEN_EXPIRES_IN))
subject=str(user.username), expires_time=timedelta(minutes=REFRESH_TOKEN_EXPIRES_IN))
token_response={'status': 'success','user':user.username ,'token':{'access_token':access_token,'refresh_token':refresh_token}}
except Exception as e:
error = e.__class__.__name__
Expand Down
17 changes: 15 additions & 2 deletions backend/app/routers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from fastapi import Depends, HTTPException, status, APIRouter, Response
from ..database import get_db
from app.oauth2 import require_user
import requests
import json

router = APIRouter()

Expand Down Expand Up @@ -44,13 +46,24 @@ def update_project(id: str, project: schemas.UpdateProjectSchema, db: Session =
return updated_project


@router.get('/{id}', response_model=schemas.ProjectResponse)
@router.get('/{id}', response_model=schemas.ProjectResponseCep)
def get_project(id: str, db: Session = Depends(get_db), username: str = Depends(require_user)):
project = db.query(models.Project).filter(models.Project.id == id).first()
if not project:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail=f"No project with this id: {id} found")
return project

url_cep='http://viacep.com.br/ws/{0}/json/'.format(project.zip_code)
try:
data = requests.get(url_cep).json()
print(data)
project.cidade=data["localidade"]
project.uf=data["uf"]
except:
project.cidade=""
project.uf=""
finally:
return project


@router.delete('/{id}')
Expand Down
4 changes: 4 additions & 0 deletions backend/app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class ProjectResponse(ProjectBaseSchema):
created_at: datetime
updated_at: datetime

class ProjectResponseCep(ProjectResponse):
cidade: str
uf: str


class UpdateProjectSchema(BaseModel):
title: Union[ str , None] = None
Expand Down
2 changes: 2 additions & 0 deletions backend/readMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pip install "passlib[bcrypt]"
<br>
pip install 'fastapi-jwt-auth[asymmetric]'
<br>
pip install http3
<br>
pip install alembic
<br>
alembic init alembic
Expand Down