from typing import Dict

from httpx import AsyncClient, URL

from app.core.config import settings
from app.services.service import api_exception


class Duckling:
    """
    Duckling is a Haskell library that parses text into structured data.
    """

    def __init__(self, client: AsyncClient, server_settings=settings):
        super(Duckling, self).__init__()
        self._client = client
        self._host = URL(server_settings.DUCKLING_HOST)

    @api_exception
    async def parse(self, text: str, locale: str = "zh_CN") -> Dict:
        url = self._host.join("parse")
        data = {"locale": locale, "text": text}
        raw_response = await self._client.post(url, data=data)

        return raw_response.json()