Skip to content

Commit

Permalink
Merge pull request #13 from tuna2134/add-logger
Browse files Browse the repository at this point in the history
Add logging
  • Loading branch information
tuna2134 authored Mar 18, 2023
2 parents 520efbb + 08f49f0 commit 6eca1f6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions voicevox/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
from typing import Optional, List
from typing_extensions import Self

import logging

from .audio_query import AudioQuery
from .http import HttpClient
from .speakers import Speaker


logger = logging.getLogger(__name__)


class Client:
"""Voicevox client class
Expand All @@ -34,6 +39,7 @@ async def close(self) -> None:
You must run this function, when you finish process.
"""
logger.info("Close http client")
await self.http.close()

async def __aenter__(self) -> Self:
Expand Down
9 changes: 9 additions & 0 deletions voicevox/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@

from typing import List, Optional

import logging

from httpx import AsyncClient

from .errors import NotfoundError, HttpException
from .types import AudioQueryType, SpeakerType


logger = logging.getLogger(__name__)


class HttpClient:

def __init__(self, base_url: str, timeout: Optional[int] = None):
self.session = AsyncClient(base_url=base_url, timeout=timeout)
logger.debug("Start session.")

async def close(self) -> None:
logger.debug("Stop session")
await self.session.aclose()

async def request(self, method: str, path: str, **kwargs) -> dict:
logger.debug(f"Request: {method} Path: {path} kwargs: {kwargs}")
response = await self.session.request(method, path, **kwargs)
logger.debug("StatusCode: {0.status_code} Response: {0.content}".format(response))
if response.status_code == 200 or response.status_code == 204:
if response.headers.get("content-type") == "application/json":
return response.json()
Expand Down

0 comments on commit 6eca1f6

Please sign in to comment.