Skip to content

sessatakuma/API-tools

Repository files navigation

API-tools

Warning

This project is still under development!

This is a repository that implement several API interfaces for Discord BOT and web interface.

Currently we have 2 implemented APIs

  1. Mark Accent API
  2. Mark Furigana API
  3. Usage query API
  4. Disctionary query API
  5. Sentence Query API

Note

The following document is only for developer, we will use Swagger UI to generate official API document.

  1. Mark Accent

    Mark accent of given Japanese text

    • Request URL

      https://{TODO}/api/MarkAccent/

    • Request Parameter (POST)

      Note that we only accept POST request

      Parameter Type Explanation
      text (required) string The text to query

      Sample request

      {
          "text": "お金を稼ぐ"
      }
    • Respond Parameter

      Parameter Type Explanation
      status int status code Reference
      result object An array contains marked results
      surface string The original input (partial) text
      accent list The accent of given word, with furigana and accent type info. 0 for no accent, 1 for plain, 2 for fall down
      subword object An array contains more details when a word contains both kanji and kana
      error object An object that describe the details of an error when occur
      error/code integer Reference
      error/message string Reference

      Sample response

      {
          "status": 200,
          "result": [
              {
                  "furigana": "おかね",
                  "surface": "お金",
                  "accent": [
                      {
                          "furigana": "",
                          "accent_marking_type": 0
                      },
                      {
                          "furigana": "",
                          "accent_marking_type": 1
                      },
                      {
                          "furigana": "",
                          "accent_marking_type": 1
                      }
                  ],
                  "subword": [
                      {
                          "furigana": "",
                          "surface": ""
                      },
                      {
                          "furigana": "かね",
                          "surface": ""
                      }
                  ]
              },
              {
                  "furigana": "",
                  "surface": "",
                  "accent": [
                      {
                          "furigana": "",
                          "accent_marking_type": 1
                      }
                  ]
              },
              {
                  "furigana": "かせぐ",
                  "surface": "稼ぐ",
                  "accent": [
                      {
                          "furigana": "",
                          "accent_marking_type": 1
                      },
                      {
                          "furigana": "",
                          "accent_marking_type": 2
                      },
                      {
                          "furigana": "",
                          "accent_marking_type": 0
                      }
                  ],
                  "subword": [
                      {
                          "furigana": "かせ",
                          "surface": ""
                      },
                      {
                          "furigana": "",
                          "surface": ""
                      }
                  ]
              }
          ],
          "error": null
      }
  2. Mark Furigana

    Mark furigana of given text

    • Request URL

      https://{TODO}/api/MarkFurigana/

    • Request Parameter (POST)

      Note that we only accept POST request

      Parameter Type Explanation
      text (required) string The text to query

      Sample request

      {
          "text": "漢字かな交じり文"
      }
    • Respond Parameter

      Parameter Type Explanation
      status int status code Reference
      result object An array contains marked results
      surface string The original input (partial) text
      furigana string The marked furigana
      subword object An array contains more details when a word contains both kanji and kana
      error object An object that describe the details of an error when occur
      error/code integer Reference
      error/message string Reference

      Sample response

      {
          "status": "200",
          "result": [
              {
                  "furigana": "かんじ",
                  "surface": "漢字"
              },
              {
                  "furigana": "かなまじり",
                  "subword": [
                  {
                      "furigana": "かな",
                      "surface": "かな"
                  },
                  {
                      "furigana": "",
                      "surface": ""
                  },
                  {
                      "furigana": "じり",
                      "surface": "じり"
                  }
                  ],
                  "surface": "かな交じり"
              },
              {
                  "furigana": "ぶん",
                  "surface": ""
              }
          ]
      }

Build Environment

Download uv and run this command:

uv sync

After build the environment, you should also obtain a Yahoo API Client ID from Yahoo Japan website.

Then add the sensitive information in file .env as follows:

YAHOO_API_KEY=<Our Yahoo API Key>
ALLOW_ORIGINS=<Our Allowed Origins>
ALLOWED_HOSTS=<Our Allowed Hosts>
X_API_KEY=<Our X-API-KEY>

How to run?

uvicorn main:app --reload

To check the functionality, you may send POST request with curl as follows.

curl -X POST -H "Content-Type: application/json" -d "{\"text\": \"test\"}" 127.0.0.1:8000/api/MarkFurigana/

How to use a shared httpx.AsyncClient?

If your router needs to send HTTP requests, you can follow the instructions below to use a shared httpx.AsyncClientto enhance performance.

import httpx
from fastapi import APIRouter, Depends
from api.dependencies import get_http_client

router = APIRouter()

@router.post(
    "/Foo/", tags=["Foo"], response_model=FooResponse
)
async def foo(
    request: FooRequest, client: httpx.AsyncClient = Depends(get_http_client)
):
    try:
        response = await client.post(url)
    except httpx.TimeoutException:
        ...
    except httpx.HTTPError as e:
        ...

About

A collection of API interfaces

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5