Skip to content

Commit

Permalink
Merge pull request #20 from takeYY/feat/openapi-response
Browse files Browse the repository at this point in the history
【更新】OpenAPIのレスポンスを定義
  • Loading branch information
takeYY committed Jul 27, 2023
2 parents 95c7cc2 + f2ed168 commit dec5e03
Show file tree
Hide file tree
Showing 47 changed files with 1,386 additions and 409 deletions.
25 changes: 24 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,30 @@
"UMCS",
"pytest",
"openapi",
"redoc"
"redoc",
"pydantic",
"mrph_index",
"mrph_id",
"prev_mrph_id",
"doukei",
"midasi",
"yomi",
"genkei",
"hinsi",
"hinsi_id",
"bunrui",
"bunrui_id",
"katuyou1",
"katuyou1_id",
"katuyou2",
"katuyou2_id",
"imis",
"fstring",
"repname",
"jsonable",
"juman",
"pyknp",
"mrph"
],
"python.analysis.typeCheckingMode": "basic"
}
24 changes: 12 additions & 12 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
FROM python:3.10

# Jumanpp の導入
RUN wget https://github.com/ku-nlp/jumanpp/releases/download/v2.0.0-rc3/jumanpp-2.0.0-rc3.tar.xz \
&& apt-get update \
&& apt-get install -y sudo \
&& apt-get install -y make \
&& apt-get install -y cmake \
&& apt-get install -y tar \
&& tar xJvf jumanpp-2.0.0-rc3.tar.xz \
&& cd jumanpp-2.0.0-rc3/ \
&& mkdir bld \
&& cd bld \
&& cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local \
&& sudo make install
# RUN wget https://github.com/ku-nlp/jumanpp/releases/download/v2.0.0-rc3/jumanpp-2.0.0-rc3.tar.xz \
# && apt-get update \
# && apt-get install -y sudo \
# && apt-get install -y make \
# && apt-get install -y cmake \
# && apt-get install -y tar \
# && tar xJvf jumanpp-2.0.0-rc3.tar.xz \
# && cd jumanpp-2.0.0-rc3/ \
# && mkdir bld \
# && cd bld \
# && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local \
# && sudo make install

# 作業ディレクトリ
WORKDIR /api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from .film_record_query_domain_service import ImplFilmRecordQueryDomainService

logger = getLogger(__name__)


class ImplFilmRecordQueryApplication(IFilmRecordQueryApplication):
def __init__(
Expand All @@ -21,7 +23,6 @@ def __init__(
film_watch_history_repository: IFilmWatchHistoryRepository,
watch_medium_repository: IWatchMediumRepository,
) -> None:
logger = getLogger(__name__)
logger.info("映画記録アプリケーションの初期化")
self.film_record_repository: IFilmRecordRepository = film_record_repository
self.film_record_domain_service: IFilmRecordQueryDomainService = ImplFilmRecordQueryDomainService(
Expand All @@ -32,39 +33,28 @@ def __init__(
watch_medium_repository=watch_medium_repository,
)

async def fetch_film_record_by_id(self, id: str) -> dict[str, str | FilmRecordEntity | None]:
logger = getLogger(__name__)
async def fetch_film_record_by_id(self, id: str) -> FilmRecordEntity:
try:
logger.info(f"{id}に一致する映画記録をfetch: 開始")
film_record_id = FilmRecordIdObject(value=id)
film_record: FilmRecordEntity | None = self.film_record_repository.find_by_id(id=film_record_id)
logger.info(f"{id}に一致する映画記録をfetch: 終了")

return dict(
status="success",
result=film_record,
)
except Exception as e:
logger.error(e)
return dict(
status="error",
result=str(e),
)
if not film_record:
raise ValueError("映画記録がありません.")

return film_record

async def fetch_film_records(self):
logger = getLogger(__name__)
except Exception:
raise

async def fetch_film_records(self) -> list[FilmRecordEntity]:
try:
logger.info("映画記録を一括取得: 開始")
film_records = self.film_record_domain_service.get_film_records()
logger.info("映画記録を一括取得: 終了")

return dict(
status="success",
result=film_records,
)
except Exception as e:
logger.error(e)
return dict(
status="error",
result=str(e),
)
return film_records

except Exception:
raise
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
self.film_watch_history_repository: IFilmWatchHistoryRepository = film_watch_history_repository
self.watch_medium_repository: IWatchMediumRepository = watch_medium_repository

def get_film_records(self):
def get_film_records(self) -> list[FilmRecordEntity]:
# 映画記録をNotionから取得し、データクラスに変換
notion_film_records = self.film_record_repository.get_film_records()
notion_film_record_data = NotionFilmRecordData.from_dict(notion_film_records)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class IFilmRecordQueryApplication(ABC):
async def fetch_film_record_by_id(
self,
id: str,
) -> dict[str, str | FilmRecordEntity | None]:
) -> FilmRecordEntity:
raise NotImplementedError

@abstractmethod
async def fetch_film_records(self):
async def fetch_film_records(self) -> list[FilmRecordEntity]:
raise NotImplementedError
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# 標準ライブラリ
from abc import ABC, abstractmethod

# 独自ライブラリ
from src.domain.film_record import FilmRecordEntity


class IFilmRecordQueryDomainService(ABC):
@abstractmethod
def get_film_records(self):
def get_film_records(self) -> list[FilmRecordEntity]:
raise NotImplementedError
40 changes: 23 additions & 17 deletions api/src/application/jumanpp/jumanpp_application.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
# 標準ライブラリ
from abc import ABC, abstractmethod
from logging import getLogger

# 外部ライブラリ
from pyknp import Juman

# 独自ライブラリ
from schemas.word.word import Word
from src.schemas.jumanpp import JumanppResult, WordRequest

logger = getLogger(__name__)


class JumanppApplication(ABC):
@abstractmethod
async def morphological_analysis(self, target: Word) -> dict[str, str]:
async def morphological_analysis(
self,
target: WordRequest,
) -> list[JumanppResult]:
raise NotImplementedError


class ImplJumanppApplication(JumanppApplication):
def __init__(self):
print("application init")
logger.info("jumanpp application init")

async def morphological_analysis(self, word: Word) -> dict[str, str]:
async def morphological_analysis(
self,
word: WordRequest,
) -> list[JumanppResult]:
try:
print("application analysis")
logger.info("application analysis")

juman = Juman()
result = juman.analysis(word.target)
result_list = result.mrph_list()

return dict(
status="success",
result=result_list,
)
except Exception as e:
print(e)
return dict(
status="error",
result=str(e),
)
result_list: list[JumanppResult] = result.mrph_list()
if not result_list:
raise ValueError("形態素解析結果がありません.")

return result_list

except Exception:
raise
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# 標準ライブラリ
from abc import ABC, abstractmethod

# 外部ライブラリ
from dataclass_wizard.type_def import JSONObject


class IFilmGenreRepository(ABC):
@abstractmethod
def get_genres(self):
def get_genres(self) -> JSONObject:
raise NotImplementedError
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# 標準ライブラリ
from abc import ABC, abstractmethod

# 外部ライブラリ
from dataclass_wizard.type_def import JSONObject


class IFilmSeriesRepository(ABC):
@abstractmethod
def get_series(self):
def get_series(self) -> JSONObject:
raise NotImplementedError
5 changes: 4 additions & 1 deletion api/src/domain/film_record/film_record_repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# 標準ライブラリ
from abc import ABC, abstractmethod

# 外部ライブラリ
from dataclass_wizard.type_def import JSONObject

# 独自ライブラリ
from .film_record_entity import FilmRecordEntity
from .film_record_id_object import FilmRecordIdObject
Expand All @@ -12,7 +15,7 @@ def find_by_id(self, id: FilmRecordIdObject) -> FilmRecordEntity | None:
raise NotImplementedError

@abstractmethod
def get_film_records(self):
def get_film_records(self) -> JSONObject:
raise NotImplementedError

@abstractmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# 標準ライブラリ
from abc import ABC, abstractmethod

# 外部ライブラリ
from dataclass_wizard.type_def import JSONObject


class IFilmWatchHistoryRepository(ABC):
@abstractmethod
def get_film_watch_history(self):
def get_film_watch_history(self) -> JSONObject:
raise NotImplementedError
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# 標準ライブラリ
from abc import ABC, abstractmethod

# 外部ライブラリ
from dataclass_wizard.type_def import JSONObject


class IWatchMediumRepository(ABC):
@abstractmethod
def get_watch_media(self):
def get_watch_media(self) -> JSONObject:
raise NotImplementedError
4 changes: 3 additions & 1 deletion api/src/infrastructure/film_record/film_record_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import os

# 外部ライブラリ
from dataclass_wizard.type_def import JSONObject
from notion_client import Client
from notion_client.typing import SyncAsync

# 独自ライブラリ
from src.domain.film_record import FilmRecordEntity, FilmRecordIdObject, IFilmRecordRepository
Expand All @@ -15,7 +17,7 @@ def __init__(self) -> None:
def find_by_id(self, id: FilmRecordIdObject) -> FilmRecordEntity | None:
return None

def get_film_records(self):
def get_film_records(self) -> SyncAsync[JSONObject]:
notion_token = os.environ["NOTION_TOKEN"]
notion = Client(auth=notion_token)
div_film_record_id: str = os.environ["FILM_RECORD_DB_ID"]
Expand Down
25 changes: 17 additions & 8 deletions api/src/infrastructure/film_record/genre/film_genre_repository.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
# 標準ライブラリ
import os
from logging import getLogger

# 外部ライブラリ
from dataclass_wizard.type_def import JSONObject
from notion_client import Client
from notion_client.typing import SyncAsync

# 独自ライブラリ
from src.domain.film_record.film.genre import IFilmGenreRepository

# 定数
NOTION_TOKEN = os.environ["NOTION_TOKEN"]
FILM_GENRE_DB_ID = os.environ["FILM_GENRE_DB_ID"]

logger = getLogger(__name__)


class ImplFilmGenreRepository(IFilmGenreRepository):
def __init__(self):
pass
# Notionの設定
self.notion = Client(auth=NOTION_TOKEN)
self.notion_film_genre_id = FILM_GENRE_DB_ID

def get_genres(self):
def get_genres(self) -> SyncAsync[JSONObject]:
"""Notionから映画ジャンルデータを取得する"""
logger.info("Notionから映画ジャンルを取得する処理: 開始")
query: dict[str, str] = dict(database_id=self.notion_film_genre_id)
logger.info("Notionから映画ジャンルを取得する処理: 終了")

notion_token = os.environ["NOTION_TOKEN"]
notion = Client(auth=notion_token)
notion_film_genre_id: str = os.environ["FILM_GENRE_DB_ID"]

query: dict[str, str] = dict(database_id=notion_film_genre_id)
return notion.databases.query(**query)
return self.notion.databases.query(**query)
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
from logging import getLogger

# 外部ライブラリ
from dataclass_wizard.type_def import JSONObject
from notion_client import Client
from notion_client.typing import SyncAsync

# 独自ライブラリ
from src.domain.film_record.film.genre import IFilmGenreRepository

# 定数
NOTION_TOKEN = os.environ["NOTION_TOKEN"]
DEV_FILM_GENRE_DB_ID = os.environ["DEV_FILM_GENRE_DB_ID"]

logger = getLogger(__name__)


class ImplInmemoryFilmGenreRepository(IFilmGenreRepository):
def __init__(self) -> None:
pass

def get_genres(self):
logger.info("【inmemory】Notionから映画ジャンルを取得する処理: 開始")
# Notionの設定
notion_token = os.environ["NOTION_TOKEN"]
self.notion = Client(auth=notion_token)
self.notion_div_film_genre_id: str = os.environ["DEV_FILM_GENRE_DB_ID"]
self.notion = Client(auth=NOTION_TOKEN)
self.notion_div_film_genre_id: str = DEV_FILM_GENRE_DB_ID

def get_genres(self) -> SyncAsync[JSONObject]:
logger.info("【inmemory】Notionから映画ジャンルを取得する処理: 開始")
query: dict[str, str] = dict(database_id=self.notion_div_film_genre_id)
logger.info("【inmemory】Notionから映画ジャンルを取得する処理: 終了")

return self.notion.databases.query(**query)
Loading

0 comments on commit dec5e03

Please sign in to comment.