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.2.0.tar.gz (152.9 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.2.0-py3-none-any.whl (177.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sonolus_fastapi-0.2.0.tar.gz
  • Upload date:
  • Size: 152.9 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.2.0.tar.gz
Algorithm Hash digest
SHA256 ccbe9101273f5c5305227eff28f28c71cb9793b2bc6e37b2efe39823eea81044
MD5 27f646f15a975edf2176dfd8bdf98f46
BLAKE2b-256 7694da2a86d47b332a0fd737a50318c50d120deeec6a0c636aec181e00e886a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sonolus_fastapi-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18c64d718e4c64f54ebf5ddfd621e9cd69d0340efce126fb5c28f81831ec402e
MD5 f84e5544d1bccb15418550aebef6e6ae
BLAKE2b-256 ffd6d93975864f1386b901aa05f08c70f28febe734f6cb9091451ae9d72d42b3

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