@@ -22,12 +22,8 @@ class BookService:
2222 @inject
2323 def __init__ (
2424 self ,
25- book_repository : BookRepositoryInterface = Provide [
26- BookRepositoryInterface .__name__
27- ],
28- event_gateway : BookEventGatewayInterface = Provide [
29- BookEventGatewayInterface .__name__
30- ],
25+ book_repository : BookRepositoryInterface = Provide [BookRepositoryInterface .__name__ ],
26+ event_gateway : BookEventGatewayInterface = Provide [BookEventGatewayInterface .__name__ ],
3127 ) -> None :
3228 super ().__init__ ()
3329 self ._book_repository = book_repository
@@ -37,14 +33,10 @@ async def create_book(self, book: BookData) -> Book:
3733 # Example of CPU intensive task ran in a different thread
3834 # Using processes could be better, but it would bring technical complexity
3935 # https://anyio.readthedocs.io/en/3.x/subprocesses.html#running-functions-in-worker-processes
40- book_data_altered : dict = await to_thread .run_sync (
41- self ._some_cpu_intensive_blocking_task , book .model_dump ()
42- )
36+ book_data_altered : dict = await to_thread .run_sync (self ._some_cpu_intensive_blocking_task , book .model_dump ())
4337
4438 book_model = BookModel (** book_data_altered )
45- book = Book .model_validate (
46- await self ._book_repository .save (book_model ), from_attributes = True
47- )
39+ book = Book .model_validate (await self ._book_repository .save (book_model ), from_attributes = True )
4840
4941 # Example of CPU intensive task ran in a dramatiq task. We should not rely on
5042 # dramatiq if we need to wait the operation result.
@@ -53,9 +45,7 @@ async def create_book(self, book: BookData) -> Book:
5345 book_cpu_intensive_task .send (book_id = str (book .book_id ))
5446
5547 await self ._event_gateway .emit (
56- BookCreatedV1 .event_factory (
57- data = BookCreatedV1Data .model_validate (book_model , from_attributes = True )
58- )
48+ BookCreatedV1 .event_factory (data = BookCreatedV1Data .model_validate (book_model , from_attributes = True ))
5949 )
6050 return book
6151
0 commit comments