Skip to content

Commit

Permalink
Added other internal endpoint, possible fix SqlAlchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
CedricCortenraede committed Jun 19, 2024
1 parent bf6e156 commit 1dcfe2f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
5 changes: 2 additions & 3 deletions src/api/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def create_app_private() -> Litestar:
allow_origins=settings.CORS_ALLOWED_ORIGINS,
),
openapi_config=OpenAPIConfig(
title="Personalized wildlife stream API",
title="Internal API",
version="1.0.0",
components=Components(
security_schemes={
Expand All @@ -108,10 +108,9 @@ def create_app_private() -> Litestar:
),
plugins=[
StructlogPlugin(StructlogConfig(config)),
# SQLAlchemyPlugin(config=db_config),
SQLAlchemyPlugin(config=db_config),
],
lifespan=[
postgres_connection,
redis_connection,
],
)
Expand Down
1 change: 0 additions & 1 deletion src/api/api/models/animal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
relationship,
)
from models.base import Base
from models.stream import Stream


class Animal(Base):
Expand Down
1 change: 0 additions & 1 deletion src/api/api/models/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
relationship,
)
from models.base import Base
from models.stream import Stream


class Country(Base):
Expand Down
4 changes: 2 additions & 2 deletions src/api/api/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from litestar import Router

from routers.v1 import streams, word_cloud
from routers.v1 import internal
from routers.v1 import internal, internal_streams


def create_router() -> Router:
Expand All @@ -14,5 +14,5 @@ def create_router() -> Router:
def create_router_private() -> Router:
return Router(
path="v1",
route_handlers=[internal.internalController],
route_handlers=[internal.internalController, internal_streams.internalStreamsController],
)
32 changes: 16 additions & 16 deletions src/api/api/routers/v1/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from sqlalchemy.dialects.postgresql import insert
from sqlalchemy import select
from models.country import Country
from models.animal import Animal
from models.stream import Stream
from litestar.datastructures import State
Expand All @@ -26,15 +27,12 @@ class internalController(Controller):
path = "/internal"
tags = ["internal"]

@get("/streams")
async def get_streams(
self, session: AsyncSession
) -> list[Stream]:
pass

@post("/stream_animals")
async def store_stream_animals(
self, session: AsyncSession, stream_id: int, data: Annotated[list[AnimalItem], Body()]
self,
session: AsyncSession,
stream_id: int,
data: Annotated[list[AnimalItem], Body()],
) -> Response:
# Check if provided stream_id is valid.
if not await session.scalars(select(Stream.id).filter_by(id=stream_id)).first():
Expand All @@ -43,26 +41,28 @@ async def store_stream_animals(
content="Provided stream id is not valid.",
status_code=422,
)

# Save animals to provided stream_id.
for animal in data:
animal_name, animal_count = animal.animal, animal.count

# Check if animal already exists in database, if not create animal.
animal_id = await session.scalars(select(Animal.id).filter_by(name=animal_name)).first()

animal_id = await session.scalars(
select(Animal.id).filter_by(name=animal_name)
).first()

# If no animal exists, create one.
if not animal_id:
# Create animal object, get extra information from external API.
animal_db = Animal(
name=animal_name,
)
session.add(animal_db)

# Get id of newly created animal.
session.flush()
animal_id = animal_db.id

# Link animal to stream_id.
stmt = insert(streams_animals).values(
stream_id=stream_id,
Expand All @@ -74,12 +74,12 @@ async def store_stream_animals(
set_={"count": streams_animals.c.count + animal_count},
)
await session.execute(stmt)

# Save all changes to database.
await session.commit()

return Response(
media_type=MediaType.TEXT,
content="Successfully saved provided animals to stream.",
status_code=201,
)
)
7 changes: 4 additions & 3 deletions src/api/api/routers/v1/internal_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from sqlalchemy.dialects.postgresql import insert
from sqlalchemy import select
from models.country import Country
from models.animal import Animal
from models.stream import Stream
from litestar.datastructures import State
Expand All @@ -26,8 +27,8 @@ async def provide_streams_repository(session: AsyncSession) -> StreamRepository:

# TODO: exclude from schemas
# Controller for internal endpoints
class internalController(Controller):
path = "/internal"
class internalStreamsController(Controller):
path = "/internal-streams"
tags = ["internal-streams"]

dependencies = {"streams_repository": Provide(provide_streams_repository)}
Expand All @@ -36,6 +37,6 @@ class internalController(Controller):
async def get_streams(self, stream_repository: StreamRepository) -> list[Stream]:
return await stream_repository.list()

@get("/streams/{stream_id}")
@get("/streams/{stream_id:int}")
async def get_stream(self, stream_repository: StreamRepository, stream_id: int) -> Stream:
return await stream_repository.get(item_id=stream_id, load=[Stream.tag, Stream.country, Stream.animals])

0 comments on commit 1dcfe2f

Please sign in to comment.