Skip to content

Commit

Permalink
Merge pull request #13 from 4l3j0Ok/hide-mongo-id
Browse files Browse the repository at this point in the history
Hide mongo ID from response
  • Loading branch information
4l3j0Ok authored Mar 28, 2024
2 parents 93b2983 + 35548e0 commit feb0ba3
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ jobs:
name: Build and push to Docker Hub
uses: docker/build-push-action@v5
with:
context: ./backend
context: ./src
tags: ${{ steps.meta.outputs.tags }}
push: true
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
name: Setting up Stack
run: |
mkdir -p $PROJECT_PATH
cp -f ./stack/docker-compose.yml $PROJECT_PATH/docker-compose.yml
cp -f ./stack/prod.compose.yml $PROJECT_PATH/docker-compose.yml
echo "$ENV_CONTENT" > $PROJECT_PATH/.env
env:
PROJECT_PATH: ${{ secrets.PROJECT_PATH }}
Expand Down
21 changes: 0 additions & 21 deletions backend/src/models/bank.py

This file was deleted.

6 changes: 3 additions & 3 deletions backend/Dockerfile → src/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM python:3.11.4-alpine as dependencies
FROM python:3.12.2-alpine as dependencies

WORKDIR /app
COPY ./requirements.txt .
RUN pip install --user -r ./requirements.txt

FROM python:3.11.4-alpine as builder
FROM python:3.12.2-alpine as builder
WORKDIR /app
COPY ./src /app/src
COPY . /app/src
COPY --from=dependencies /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH
EXPOSE 8080
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions backend/src/main.py → src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from fastapi import FastAPI, responses
import uvicorn
import importlib
import uvicorn
from fastapi import FastAPI, responses
from modules import config, utils
from modules.logger import logger

Expand Down
11 changes: 11 additions & 0 deletions src/models/bank.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydantic import BaseModel


class Bank(BaseModel):
label: str
bcra_id: str


class AdminCredentials(BaseModel):
username: str
password: str
4 changes: 1 addition & 3 deletions backend/src/models/response.py → src/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ class Detail(TypedDict):


class Success(BaseModel):
input: str | int | dict | None
detail: Detail


class Failed(BaseModel):
input: str | int | dict | None
detail: Detail


class Result(BaseModel):
success: list[Success] | list
failed: list[Failed] | list
failed: list[Failed] | list
28 changes: 13 additions & 15 deletions backend/src/modules/bank.py → src/modules/bank.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
from modules import soup, messages, config
from modules.logger import logger
from models.bank import BankDB
from models.bank import Bank
from db.client import client
from bson import ObjectId
import os


db = client.bcra_scraper.banks


def get_banks(bank_id: str = None, as_dict: bool = False):
logger.info(f"Validando ID {bank_id}.")
if bank_id:
if not ObjectId.is_valid(bank_id):
logger.warning(f"ID {bank_id} inválido.")
return False, messages.ERR_SCHEMA.format(err_args="id")
def get_banks(bcra_id: str = None, as_dict: bool = False):
logger.info("Buscando bancos en la DB.")
result = list(db.find({"_id":ObjectId(bank_id)} if bank_id else None))
result = list(db.find({"bcra_id": bcra_id} if bcra_id else None))
if not result:
return False, messages.ERR_BANK_NOT_FOUND
banks = [BankDB(**bank) for bank in result] if not as_dict \
else [BankDB(**bank).model_dump() for bank in result]
banks = [Bank(**bank) for bank in result] if not as_dict \
else [Bank(**bank).model_dump() for bank in result]
return True, banks if len(banks) != 1 else banks[0]


def update_banks():
logger.info("Actualizando la lista de bancos con SOUP...")
data = soup.get_banks_from_url(config.BCRA_URL)
delete_query = {"bcra_id": {"$in": [bank["bcra_id"] for bank in data]}}
db.delete_many(delete_query)
result = db.insert_many(data)
return True, [str(bank_id) for bank_id in result.inserted_ids]
try:
db.delete_many(delete_query)
db.insert_many(data)
except Exception as ex:
logger.exception(ex)
return False, messages.ERR_FAILED_TO_UPDATE
return True, messages.MSG_SUCCESS_SAVE


def add_bank(bank):
logger.info("Agregando un nuevo banco...")
result = db.insert_one(bank.dict())
return True, str(result.inserted_id)
return True, messages.MSG_SUCCESS_SAVE


def validate_admin(credentials):
Expand Down
5 changes: 3 additions & 2 deletions backend/src/modules/config.py → src/modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

APP_HOST = os.getenv("APP_HOST", "localhost")
APP_PORT = int(os.getenv("APP_PORT", "8080"))
MONGO_URI = os.getenv("MONGO_URI")
MONGO_URI = os.getenv("MONGO_URI")
if not MONGO_URI:
MONGO_HOST = os.getenv("MONGO_HOST")
MONGO_PORT = int(os.getenv("MONGO_PORT"))
Expand All @@ -12,7 +12,8 @@
BCRA_URL = "https://www.bcra.gob.ar/SistemasFinancierosYdePagos/Sistema_financiero_nomina_de_entidades.asp?bco=AAA00&tipo=1"
DOCKER_ORGANIZATION = os.getenv("DOCKER_ORGANIZATION", "alejoide")
DOCKER_REPOSITORY = os.getenv("DOCKER_REPOSITORY", "bcra-scraper-api")
DOCKER_API_URL = f"https://registry.hub.docker.com/v2/repositories/{DOCKER_ORGANIZATION}/{DOCKER_REPOSITORY}/tags"
DOCKER_API_URL = f"https://registry.hub.docker.com/v2/repositories/{
DOCKER_ORGANIZATION}/{DOCKER_REPOSITORY}/tags"
ROUTERS = [
"bank"
]
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion backend/src/modules/messages.py → src/modules/messages.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ERR_BANK_NOT_FOUND = "Banco no encontrado."
ERR_BANK_ALREADY_EXISTS = "El banco con id {id} ya existe."
ERR_BANK_NOT_CHANGED = "El banco ingresado posee los mismos datos."
ERR_FAILED_TO_UPDATE = "No se pudo actualizar la lista de bancos."
ERR_SCHEMA = "Los datos ingresados no tienen la estructura adecuada. Argumentos faltantes o incorrectos: {err_args}."
MSG_SUCCESS_GENERIC = "Éxito al realizar la operación."
MSG_SUCCESS_SAVE = "Exito al guardar los datos."
MSG_ERROR_UNAUTHORIZED = "No autorizado."
MSG_ERROR_UNAUTHORIZED = "No autorizado."
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions backend/src/routers/bank.py → src/routers/bank.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async def update_banks_list(credentials: AdminCredentials) -> Success:
)
)
return Success(
input=None,
detail=Detail(
payload={"ids": result},
message=messages.MSG_SUCCESS_SAVE
Expand Down Expand Up @@ -79,7 +78,6 @@ def add_bank(bank: Bank, credentials: AdminCredentials) -> Success:
)
)
return Success(
input=None,
detail=Detail(
payload={"id": result},
message=messages.MSG_SUCCESS_SAVE
Expand Down
21 changes: 21 additions & 0 deletions stack/dev.compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: "3.9"
services:
BCRA-Scraper-API:
image: alejoide/bcra-scraper-api:latest
container_name: bcra-scraper-api
build:
context: ../src/
dockerfile: ../src/Dockerfile
volumes:
- ../src:/app/src
restart: unless-stopped
ports:
- "8080:8080"
env_file:
- .env
networks:
- mongo

networks:
mongo:
external: true
9 changes: 2 additions & 7 deletions stack/docker-compose.yml → stack/prod.compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
version: "3.9"
services:

backend:
BCRA-Scraper-API:
image: alejoide/bcra-scraper-api:latest
restart: unless-stopped
env_file:
- .env
networks:
- mongo
- nginx-proxy

# Only for production environments when using jwilder/nginx-proxy.
networks:
nginx-proxy:
external: true
mongo:
external: true
external: true

0 comments on commit feb0ba3

Please sign in to comment.