Skip to content

Commit

Permalink
Merge pull request #53 from grillazz/46-switch-to-python-311
Browse files Browse the repository at this point in the history
46 switch to python 311
  • Loading branch information
grillazz authored Nov 23, 2023
2 parents 2a73887 + 926444c commit 8863fdf
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 141 deletions.
9 changes: 5 additions & 4 deletions greens/routers/v1/vegs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ async def add_document(payload: Document):
"""
try:
# payload = jsonable_encoder(payload)
return await create_document(payload, collection)
document = await create_document(payload, collection)
return {"id": str(document.inserted_id)}
except ValueError as exception:
raise NotFoundHTTPException(msg=str(exception))
raise NotFoundHTTPException(msg=str(exception)) from exception


@router.get(
"/{object_id}",
response_description="Document retrieved",
# response_model=DocumentResponse,
response_model=DocumentResponse,
)
async def get_document(object_id: ObjectIdField):
"""
Expand All @@ -45,7 +46,7 @@ async def get_document(object_id: ObjectIdField):
try:
return await retrieve_document(object_id, collection)
except ValueError as exception:
raise NotFoundHTTPException(msg=str(exception))
raise NotFoundHTTPException(msg=str(exception)) from exception


# TODO: PUT for replace aka set PATCH for update ?
17 changes: 2 additions & 15 deletions greens/schemas/vegs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@
from typing_extensions import Annotated


# class ObjectIdField(str):
# @classmethod
# def __get_validators__(cls):
# yield cls.validate
#
# @classmethod
# def validate(cls, value):
# try:
# return ObjectId(str(value))
# except InvalidId as e:
# raise ValueError("Not a valid ObjectId") from e


def check_object_id(value: str) -> str:
if not _ObjectId.is_valid(value):
raise ValueError('Invalid ObjectId')
Expand All @@ -36,5 +23,5 @@ class Document(BaseModel):
desc: str = Field(...)


class DocumentResponse(Document):
id: ObjectIdField
class DocumentResponse(BaseModel):
id: ObjectIdField = Field(...)
14 changes: 7 additions & 7 deletions greens/services/repository.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from bson import ObjectId
from pymongo.errors import WriteError
from pymongo.results import InsertOneResult

import greens.main as greens
from greens.routers.exceptions import AlreadyExistsHTTPException
Expand All @@ -24,20 +25,19 @@ async def retrieve_document(document_id: str, collection: str) -> dict:
raise ValueError(f"No document found for {document_id=} in {collection=}")


async def create_document(document, collection: str) -> dict:
async def create_document(document, collection: str) -> InsertOneResult:
"""
:param document:
:param collection:
:return:
"""
try:
document = await greens.app.state.mongo_collection[collection].insert_one(document.model_dump())
# TODO: return await retrieve_document(document.inserted_id, collection)
return True
except WriteError:
raise AlreadyExistsHTTPException(f"Document with {document.inserted_id=} already exists")

document: InsertOneResult = await greens.app.state.mongo_collection[collection].insert_one(document.model_dump())
return document
except WriteError as e:
# TODO: this not make sense as id from mongo will be always unique if we not pass it
raise AlreadyExistsHTTPException(msg=str(e)) from e

async def get_mongo_meta() -> dict:
list_databases = await greens.app.state.mongo_client.list_database_names()
Expand Down
Loading

0 comments on commit 8863fdf

Please sign in to comment.