Skip to content

Commit

Permalink
Merge pull request #13 from joselevelh/jlevel/pre_deployment
Browse files Browse the repository at this point in the history
Jlevel/pre deployment
  • Loading branch information
joselevelh authored Mar 26, 2024
2 parents f6ab966 + e8d480e commit 1ca610d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 79 deletions.
24 changes: 24 additions & 0 deletions drakes_api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
FROM python:3.9

#
LABEL authors="JoseLevel"

# Setting env variable via arg to be passed at container run time or build time
ARG MONGO_URI_ARG
ENV MONGO_URI=$MONGO_URI_ARG

#
WORKDIR /drakes_api

#
COPY ./requirements.txt /drakes_api/requirements.txt

#
RUN pip install --no-cache-dir --upgrade -r /drakes_api/requirements.txt

#
COPY ./app /drakes_api/app

#
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
10 changes: 7 additions & 3 deletions drakes_api/crud.py → drakes_api/app/crud.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from pymongo import MongoClient
from models import Lookbook
from app.models import Lookbook
import os

# MongoDB Setup
MONGO_URI = "mongodb+srv://user2:[email protected]/?retryWrites=true&w=majority"
MONGO_URI = os.getenv("MONGO_URI")
client = MongoClient(MONGO_URI)
db = client.drakes_lookbooks
lookbooks_collection = db.lookbooks
Expand All @@ -19,7 +20,10 @@ def check_db_connection():
if db.lookbooks is not None:
print(f"Lookbook collection: {db.lookbooks}")
check += 1
return check == 3
if db.lookbooks.find_one({"lookbook_name": "spring-summer-2017-lookbook"}) is not None:
print(f'Lookbook = {db.lookbooks.find_one({"lookbook_name": "spring-summer-2017-lookbook"})}')
check += 1
return check == 4


def retrieve_lookbook_by_tag(tags: list[str]) -> list[Lookbook]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from pymongo import MongoClient

from app.crud import client

import typer

MONGO_URI = "mongodb+srv://user2:[email protected]/?retryWrites=true&w=majority"

app = typer.Typer()
google_storage_bucket_base = "https://storage.googleapis.com/drakes_lookbooks_bucket/drakes_images/"

Expand All @@ -26,12 +27,8 @@ def iterate_subdirectories(directory_path: str):
for root, directories, _ in os.walk(directory_path):
for lookbook in directories:
lookbook_path = os.path.join(root, lookbook)
# typer.echo(f"Lookbook: {lookbook_path}")
for _, _, files in os.walk(lookbook_path):
# typer.echo(f"lookbook_name: {lookbook}")
# typer.echo(f" tags: []")
image_paths = [google_storage_bucket_base + lookbook + "/" + image_name for image_name in files]
# typer.echo(f" images: {image_paths}")
new_lookbook = create_lookbook_dict(lookbook_name=lookbook, lookbook_tags=[], image_paths=image_paths)
upload_lookbook(new_lookbook)

Expand All @@ -52,7 +49,7 @@ def upload_lookbook(lookbooks_to_upload: list[dict]):
"""Uploads given lookbook to Google Cloud"""

try:
client = MongoClient(MONGO_URI)

# Check the connection by listing database names
print(f"Client: {client}")
database_names = client.list_databases()
Expand Down
18 changes: 5 additions & 13 deletions drakes_api/main.py → drakes_api/app/main.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
from fastapi import FastAPI, Query

from models import FilterParams, Lookbook
import crud
from app.models import Lookbook
from app import crud


app = FastAPI(title="Unofficial Drakes Lookbook API",
summary="An API to browse Drakes lookbooks", )


@app.get("/syscheck")
async def system_check():
def system_check():
print(f"Starting system check for API and DB connection")
return crud.check_db_connection()


@app.get("/lookbooks/{title}")
async def get_lookbook(title: str) -> list[Lookbook]:
def get_lookbook(title: str) -> list[Lookbook]:
print(f"Searching for {title} in lookbooks db...")
lookbooks = crud.retrieve_lookbook_by_name(name=title)
print(f"{lookbooks =}")
return lookbooks


@app.get("/lookbooks/any/")
async def get_lookbooks_any(tags: list[str] = Query(default=None)) -> list[Lookbook]:
def get_lookbooks_any(tags: list[str] = Query(default=None)) -> list[Lookbook]:
"""Returns all lookbooks that have the given tag(s)"""
print(f"Searching for {tags} in lookbooks db...")
lookbooks = crud.retrieve_lookbook_by_tag(tags=tags)
print(f"{lookbooks =}")
return lookbooks


@app.get("/lookbooks/all")
async def get_lookbooks_all(filter_params: FilterParams):
"""Returns lookbooks that match ALL the tags given"""
# Use 'filter_params.tags' to filter lookbooks
# ...
return None
8 changes: 0 additions & 8 deletions drakes_api/models.py → drakes_api/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@ class FilterParams(BaseModel):
tags: List[str]


# Temporarily unused (need to add infra to mongodb)
# class Image(BaseModel):
# filename: str
# path: str


class Lookbook(BaseModel):
# id: str = Field(alias="_id", default=None) # Needs extra config to convert objectID to pydantic supported type
lookbook_name: str
tags: List[str]
# images: List[Image]
images: List[str]

class Config:
Expand Down
28 changes: 28 additions & 0 deletions drakes_api/app/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from fastapi.testclient import TestClient
from app.main import app

client = TestClient(app)


def test_system_check():
response = client.get("/syscheck")
assert response.status_code == 200
assert response.json() == True


def test_get_lookbook():
response = client.get("/lookbooks/paris")
assert response.status_code == 200
assert len(response.json()) == 1
assert response.json()[0]["lookbook_name"] == "autumn-in-paris-lookbook"


def test_get_lookbooks_any():
response = client.get("/lookbooks/any/?tags=2017")
assert response.status_code == 200
assert len(response.json()) >= 1
assert response.json()[0]["tags"] == [
"spring-summer",
"2017",
"casual",
"menswear"]
3 changes: 2 additions & 1 deletion drakes_api/utilities.py → drakes_api/app/utilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pymongo import MongoClient
import os

MONGO_URI = "mongodb+srv://user2:[email protected]/?retryWrites=true&w=majority"
MONGO_URI = os.getenv("MONGO_URI")
client = MongoClient(MONGO_URI)
db = client.drakes_lookbooks

Expand Down
47 changes: 0 additions & 47 deletions drakes_api/connection.py

This file was deleted.

5 changes: 5 additions & 0 deletions drakes_api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ httpcore==1.0.2
httptools==0.6.1
httpx==0.25.2
idna==3.6
iniconfig==2.0.0
install==1.3.5
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
orjson==3.9.10
packaging==24.0
pluggy==1.4.0
pydantic==2.5.2
pydantic-core==2.14.5
pydantic-extra-types==2.1.0
pydantic-settings==2.1.0
pymongo==4.6.2
pytest==8.1.1
python-dotenv==1.0.0
python-multipart==0.0.6
PyYAML==6.0.1
sniffio==1.3.0
starlette==0.27.0
tomli==2.0.1
typer==0.9.0
typing-extensions==4.8.0
ujson==5.8.0
Expand Down

0 comments on commit 1ca610d

Please sign in to comment.