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.0.tar.gz (152.2 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.0-py3-none-any.whl (175.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sonolus_fastapi-0.1.0.tar.gz
  • Upload date:
  • Size: 152.2 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.0.tar.gz
Algorithm Hash digest
SHA256 60acc8c3fd9d805d4bd9e9fad86fd92d788b7e5e9cffc0d091ec3ef1b7c30af2
MD5 58fd2f3d7fe0dadbbb983ec0949b52a8
BLAKE2b-256 933251557a637f3c837c25c979bf14dbba42b759671aa138347839240e7f4c9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sonolus_fastapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0169c3ac011314f37eb496a852adf04aaf9a28559dcc11be093eb74e36f38636
MD5 408a6e059cb160e92f640c037dada442
BLAKE2b-256 cd99a8bf855e0e88c4abdbb3317383abf15040c426f41432392390a2161ef1af

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