Skip to main content

FastAPI wrapper for Sonolus server creation and management This project is still under development.

Project description

Sonolus-FastAPI

このプロジェクトはまだ開発途中です。

This project is still under development.

Install

pip insatll sonolus-fastapi

Usage

こちらをお読みください。

Please read this.

https://sonolus-fastapi.pim4n-net.com

Example

# example.py

import time

from fastapi import HTTPException
from sonolus_fastapi import Sonolus
from sonolus_fastapi.model.base import SonolusServerInfo, SonolusConfiguration, SonolusButton, SonolusButtonType
from sonolus_fastapi.model.items.post import PostItem
from sonolus_fastapi.model.ServerItemInfo import ServerItemInfo
from sonolus_fastapi.model.sections import BackgroundSection
from sonolus_fastapi.model.ServerItemDetails import ServerItemDetails
from sonolus_fastapi.model.Request.authenticate import ServerAuthenticateRequest
from sonolus_fastapi.model.Response.authenticate import ServerAuthenticateResponse
from sonolus_fastapi.utils.generate import generate_random_string
from sonolus_fastapi.pack import freepackpath

# Sonolusインスタンスを作成 Create Sonolus instance

sonolus = Sonolus(
    address='https://example.com', # サーバーアドレスを指定してください Specify your server address
    port=8000, # サーバーポートを指定してください Specify your server port
    enable_cors=True, # CORSを有効にするかどうか Whether to enable CORS
    dev=True, # 開発モード Development mode
)


# ---------------------------------------- 

# PostItemの例 Example of PostItem


now = int(time.time() * 1000)

post_item = PostItem(
    name="example_post",
    title="Example Post",
    version=1,
    author="Author Name",
    tags=[],
    description="This is an example post item.",
    time=now,
    thumbnail=None,
)
sonolus.ItemMemory.Post.push(post_item) # メモリにPostItemを追加 Add PostItem to memory



# ---------------------------------------- 

# Sonolusパックを読み込む Load Sonolus pack
sonolus.load(freepackpath) # Sonolus packのパスを指定してください Specify the path to the Sonolus pack

# ---------------------------------------- 

# -- ハンドラーの登録 Register handlers

@sonolus.server.server_info(SonolusServerInfo) # サーバー情報ハンドラーを登録 Register server info handler
async def get_server_info(ctx):
    return SonolusServerInfo(
        title="Example Sonolus Server",
        description="This is an example Sonolus server.",
        buttons=[
            SonolusButton(type=SonolusButtonType.AUTHENTICATION),
            SonolusButton(type=SonolusButtonType.POST),
            SonolusButton(type=SonolusButtonType.LEVEL),
            SonolusButton(type=SonolusButtonType.SKIN),
            SonolusButton(type=SonolusButtonType.BACKGROUND),
            SonolusButton(type=SonolusButtonType.EFFECT),
            SonolusButton(type=SonolusButtonType.PARTICLE),
            SonolusButton(type=SonolusButtonType.ENGINE),
            SonolusButton(type=SonolusButtonType.CONFIGURATION)
        ],
        configuration=SonolusConfiguration(
            options=[]
        ),
        banner=None,
    )
    
@sonolus.server.authenticate(ServerAuthenticateResponse) # 認証ハンドラーを登録 Register authenticate handler
async def authenticate(ctx): # 認証処理 Authentication process
    session = generate_random_string(16) # セッションIDを生成 Generate session ID
    expiration = int(time.time() * 1000) + 3600 * 1000 # 有効期限を1時間後に設定 Set expiration to 1 hour later
    
    return ServerAuthenticateResponse( # 認証レスポンスを返す Return authentication response
        session=session, # セッションID Session ID
        expiration=expiration, # 有効期限 Expiration
    )

@sonolus.post.detail(ServerItemDetails) # Postの詳細ハンドラーを登録 Register Post detail handler
async def get_post_detail(ctx, name: str): # Postの詳細を取得 Get Post details
    post = sonolus.ItemMemory.Post.get_name(name) # メモリからPostItemを取得 Get PostItem from memory
    
    if post is None: # PostItemが見つからない場合 If PostItem not found
        raise HTTPException(404, "Post item not found") # 404エラーを返す Return 404 error
    
    return ServerItemDetails( # ServerItemDetailsを返す Return ServerItemDetails
        item=post, # PostItem
        description="This is the detail of the example post item.", # 詳細説明 Detail description
        actions=[], # アクションのリスト List of actions
        hasCommunity=False, # コミュニティがあるかどうか Whether there is a community
        leaderboards=[], # リーダーボードのリスト List of leaderboards
        sections=[], # セクションのリスト List of sections
    )

# ----------------------------------------     
 
# アイテムの一式のハンドラーを登録 Register item set handler 
    
    
@sonolus.background.info(ServerItemInfo)
async def get_background_info(ctx): # Backgroundの情報を取得 Get Background info
    
    background_section = BackgroundSection(
        title="Background",
        itemType="background", 
        items=sonolus.ItemMemory.Background.list_all() # メモリから全てのBackgroundItemを取得 Get all BackgroundItems from memory
    )
    
    return ServerItemInfo( # ServerItemInfoを返す Return ServerItemInfo
        creates=[], # 作成フォームのリスト List of create forms
        searches=[], # 検索フォームのリスト List of search forms
        sections=[background_section], # セクションのリスト List of sections
        banner=None, # バナー Banner
    )
    
@sonolus.background.detail(ServerItemDetails) # Backgroundの詳細ハンドラーを登録 Register Background detail handler
async def get_background_detail(ctx, name: str): # Backgroundの詳細を取得 Get Background
    background = sonolus.ItemMemory.Background.get_name(name) # メモリからBackgroundItemを取得 Get BackgroundItem from memory
    
    if background is None: # BackgroundItemが見つからない場合 If BackgroundItem not found
        raise HTTPException(404, "Background item not found") # 404エラーを返す Return 404 error
    
    return ServerItemDetails( # ServerItemDetailsを返す Return ServerItemDetails
        item=background, # BackgroundItem
        description="This is the detail of the example background item.", # 詳細説明 Detail description
        actions=[], # アクションのリスト List of actions
        hasCommunity=False, # コミュニティがあるかどうか Whether there is a community
        leaderboards=[], # リーダーボードのリスト List of leaderboards
        sections=[], # セクションのリスト List of sections
    )
    
# ---------------------------------------- 
    
@sonolus.app.get("/hoge") # ルートエンドポイントを追加 Add root endpoint
def huga():
    return {"message": "huga"}

if __name__ == "__main__":
    sonolus.run() # サーバーを起動します Start the server

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sonolus_fastapi-0.1.1.tar.gz (152.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sonolus_fastapi-0.1.1-py3-none-any.whl (175.1 kB view details)

Uploaded Python 3

File details

Details for the file sonolus_fastapi-0.1.1.tar.gz.

File metadata

  • Download URL: sonolus_fastapi-0.1.1.tar.gz
  • Upload date:
  • Size: 152.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for sonolus_fastapi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 233c2312bc4b25b17e90dfc529d531cdc1809115012eb1fbeaab44a6c86f4546
MD5 59b55a7d41df117ef17e838440c37d8f
BLAKE2b-256 3c2ed77138cb1554cc951ffe603d8cd365340bc5ba60eee4527bf2c38204d4da

See more details on using hashes here.

File details

Details for the file sonolus_fastapi-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sonolus_fastapi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 abe5caa742531859fb80c087bed9c73297a09e2394f043d7a234d91b9acfb9fa
MD5 1275bff68bff8808116c42842fdd1a3b
BLAKE2b-256 4f844e1c663eb8fa49510f061df24ffe815a98ac039e0dcf150e8872c20e4d09

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page