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:
KhalimCK 2023-07-17 10:11:52 +01:00 committed by GitHub
commit fb41e91a57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 78 additions and 8 deletions

View file

@ -45,12 +45,9 @@ jobs:
- name: Deploy to AWS Lambda via Serverless
env:
# These will be in inserted for real
# API_KEY: ${{ secrets.API_KEY }}
# SECRET_KEY: ${{ secrets.SECRET_KEY }}
# ALGORITHM: ${{ secrets.ALGORITHM }}
API_KEY: 'example-api-key'
API_KEY: ${{ secrets.FASTAPI_API_KEY }}
ENVIRONMENT: ${{ github.ref_name }}
SECRET_KEY: 'YOUR_SECRET_KEY'
SECRET_KEY: ${{ secrets.NEXTAUTH_SECRET }}
ALGORITHM: 'HS256'
PLAN_TRIGGER_BUCKET: 'retrofit-plan-inputs-${{ github.ref_name }}'
run: cd backend && sls deploy --stage ${{ github.ref_name }} --verbose

View file

@ -8,6 +8,7 @@ class Settings(BaseSettings):
SECRET_KEY: str
ALGORITHM: str
ENVIRONMENT: str
PLAN_TRIGGER_BUCKET: str
class Config:
env_file = ".env"

View file

View 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"}

View 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
View 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

View file

@ -25,3 +25,4 @@ uvicorn==0.22.0
uvloop==0.17.0
watchfiles==0.19.0
websockets==11.0.3
boto3

View file

@ -6,10 +6,10 @@ provider:
region: eu-west-2
environment:
API_KEY: ${env:API_KEY}
# ENVIRONMENT: ${self:provider.stage}
ENVIRONMENT: 'local'
ENVIRONMENT: ${self:provider.stage}
SECRET_KEY: ${env:SECRET_KEY}
ALGORITHM: ${env:ALGORITHM}
PLAN_TRIGGER_BUCKET: ${env:PLAN_TRIGGER_BUCKET}
package:
individually: true