mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-30 13:10:47 +00:00
Merge pull request #34 from Hestia-Homes/main
Added in basic structure of the plan trigger api to be used by the frontend
This commit is contained in:
commit
fb41e91a57
8 changed files with 78 additions and 8 deletions
9
.github/workflows/deploy_fastapi_backend.yml
vendored
9
.github/workflows/deploy_fastapi_backend.yml
vendored
|
|
@ -45,12 +45,9 @@ jobs:
|
||||||
|
|
||||||
- name: Deploy to AWS Lambda via Serverless
|
- name: Deploy to AWS Lambda via Serverless
|
||||||
env:
|
env:
|
||||||
# These will be in inserted for real
|
API_KEY: ${{ secrets.FASTAPI_API_KEY }}
|
||||||
# API_KEY: ${{ secrets.API_KEY }}
|
|
||||||
# SECRET_KEY: ${{ secrets.SECRET_KEY }}
|
|
||||||
# ALGORITHM: ${{ secrets.ALGORITHM }}
|
|
||||||
API_KEY: 'example-api-key'
|
|
||||||
ENVIRONMENT: ${{ github.ref_name }}
|
ENVIRONMENT: ${{ github.ref_name }}
|
||||||
SECRET_KEY: 'YOUR_SECRET_KEY'
|
SECRET_KEY: ${{ secrets.NEXTAUTH_SECRET }}
|
||||||
ALGORITHM: 'HS256'
|
ALGORITHM: 'HS256'
|
||||||
|
PLAN_TRIGGER_BUCKET: 'retrofit-plan-inputs-${{ github.ref_name }}'
|
||||||
run: cd backend && sls deploy --stage ${{ github.ref_name }} --verbose
|
run: cd backend && sls deploy --stage ${{ github.ref_name }} --verbose
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ class Settings(BaseSettings):
|
||||||
SECRET_KEY: str
|
SECRET_KEY: str
|
||||||
ALGORITHM: str
|
ALGORITHM: str
|
||||||
ENVIRONMENT: str
|
ENVIRONMENT: str
|
||||||
|
PLAN_TRIGGER_BUCKET: str
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
env_file = ".env"
|
env_file = ".env"
|
||||||
|
|
|
||||||
0
backend/app/plan/__init__.py
Normal file
0
backend/app/plan/__init__.py
Normal file
29
backend/app/plan/router.py
Normal file
29
backend/app/plan/router.py
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from app.dependencies import validate_token
|
||||||
|
from app.plan.schemas import PlanTriggerRequest
|
||||||
|
from app.utils import read_csv_from_s3
|
||||||
|
from app.config import get_settings
|
||||||
|
|
||||||
|
router = APIRouter(
|
||||||
|
prefix="/plan",
|
||||||
|
tags=["plan"],
|
||||||
|
dependencies=[Depends(validate_token)],
|
||||||
|
responses={404: {"description": "Not found"}}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/trigger")
|
||||||
|
async def trigger_plan(body: PlanTriggerRequest):
|
||||||
|
print("Getting the inputs")
|
||||||
|
# Read in the trigger file from s3
|
||||||
|
bucket_name = get_settings().PLAN_TRIGGER_BUCKET
|
||||||
|
print("bucket_name: ", bucket_name)
|
||||||
|
print("body.trigger_file_path: ", body.trigger_file_path)
|
||||||
|
plan_input = read_csv_from_s3(bucket_name=bucket_name, filepath=body.trigger_file_path)
|
||||||
|
print("Got the inputs")
|
||||||
|
print(plan_input)
|
||||||
|
|
||||||
|
# TODO: Parse the file
|
||||||
|
# TODO: Put messages on the queue
|
||||||
|
|
||||||
|
return {"message": "Plan triggered"}
|
||||||
10
backend/app/plan/schemas.py
Normal file
10
backend/app/plan/schemas.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class PlanTriggerRequest(BaseModel):
|
||||||
|
budget: float | None = None
|
||||||
|
goal: str
|
||||||
|
housting_type: str
|
||||||
|
goal_value: float
|
||||||
|
portfolio_id: int
|
||||||
|
trigger_file_path: str
|
||||||
32
backend/app/utils.py
Normal file
32
backend/app/utils.py
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
import boto3
|
||||||
|
import csv
|
||||||
|
from io import StringIO
|
||||||
|
import string
|
||||||
|
import secrets
|
||||||
|
|
||||||
|
|
||||||
|
def read_csv_from_s3(bucket_name, filepath):
|
||||||
|
s3 = boto3.client('s3')
|
||||||
|
|
||||||
|
# Get the object from s3
|
||||||
|
s3_object = s3.get_object(Bucket=bucket_name, Key=filepath)
|
||||||
|
|
||||||
|
# Read the CSV body from the s3 object
|
||||||
|
body = s3_object['Body'].read()
|
||||||
|
|
||||||
|
# Use StringIO to create a file-like object from the string
|
||||||
|
csv_data = StringIO(body.decode('utf-8'))
|
||||||
|
|
||||||
|
# Use csv library to read it into a list of dictionaries
|
||||||
|
reader = csv.DictReader(csv_data)
|
||||||
|
data = list(reader)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def generate_api_key():
|
||||||
|
# Define the characters that will be used to generate the api key
|
||||||
|
characters = string.ascii_letters + string.digits
|
||||||
|
# Generate a 40 character long api key
|
||||||
|
api_key = ''.join(secrets.choice(characters) for _ in range(40))
|
||||||
|
return api_key
|
||||||
|
|
@ -25,3 +25,4 @@ uvicorn==0.22.0
|
||||||
uvloop==0.17.0
|
uvloop==0.17.0
|
||||||
watchfiles==0.19.0
|
watchfiles==0.19.0
|
||||||
websockets==11.0.3
|
websockets==11.0.3
|
||||||
|
boto3
|
||||||
|
|
@ -6,10 +6,10 @@ provider:
|
||||||
region: eu-west-2
|
region: eu-west-2
|
||||||
environment:
|
environment:
|
||||||
API_KEY: ${env:API_KEY}
|
API_KEY: ${env:API_KEY}
|
||||||
# ENVIRONMENT: ${self:provider.stage}
|
ENVIRONMENT: ${self:provider.stage}
|
||||||
ENVIRONMENT: 'local'
|
|
||||||
SECRET_KEY: ${env:SECRET_KEY}
|
SECRET_KEY: ${env:SECRET_KEY}
|
||||||
ALGORITHM: ${env:ALGORITHM}
|
ALGORITHM: ${env:ALGORITHM}
|
||||||
|
PLAN_TRIGGER_BUCKET: ${env:PLAN_TRIGGER_BUCKET}
|
||||||
|
|
||||||
package:
|
package:
|
||||||
individually: true
|
individually: true
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue