Add high-level SFS2X client/server API with typed models#18
Merged
Conversation
Introduces a decorator-driven client/server layer on top of the existing core/protocol/transport stack: - SFSModel + field(): annotate attributes with SFS field classes (UtfString, Int, Long, nested SFSModel, SFSObject/SFSArray) and the metaclass generates __init__, to_sfs_object() and from_sfs_object() - SystemRequest/Response and ExtensionRequest/Response bases with class kwargs (action=SysAction.X, command="...") and a _RaisableMixin so handlers can `raise PongResponse(n=42)` to reply - HandlerRegistry + dispatcher that introspect handler signatures and inject Message, SFSObject payload, ClientSession/ServerSession, parsed models, or Union[A, B] (first matching schema wins) - SFSClient/SFSServer facades with on_login / on_handshake / on_extension(command="...") / on_message / on_connect / on_disconnect decorators and async context-manager lifecycle - Built-in models for HANDSHAKE / LOGIN (+ error) / LOGOUT / PING_PONG - README rewritten around the high-level API; low-level reference moved to docs/lowlevel.md and docs/protocol.md - 37 new tests (models, dispatcher, integration over loopback TCP) https://claude.ai/code/session_01WDQmcTcMNhA9uDjpztRFao
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a complete high-level application layer for ZewSFS, transforming it from a low-level protocol implementation into a developer-friendly framework with decorator-driven handlers, typed request/response models, and async client/server lifecycle management.
Key Changes
New
sfs2x.appmodule — High-level API built on top of the existing transport and protocol layersSFSClient— Decorator-driven async client with@client.on_login(),@client.on_extension(command="..."), lifecycle hooksSFSServer— Decorator-driven async server with per-session handlers andServerSessionstate managementSFSModel— Typed dataclass-like models with SFS field annotations (UtfString,Int, etc.) that auto-serialize/deserialize toSFSObjectSystemRequest/SystemResponseandExtensionRequest/ExtensionResponse— Base classes for typed request/response pairsHandshakeRequest,LoginRequest,LoginResponse,LoginErrorResponse,LogoutRequest,PingPongRequestand their responsesDecorator handlers — Register async functions with
@client.on_login(),@server.on_extension(command="..."), etc.Message,SFSObject,ServerSession, or typedSFSModelsubclassesreq: LoginResponse | LoginErrorResponseand the first matching schema winsraiseto reply — handlers canraise ResponseModel(...)orreturn ResponseModel(...)to send repliesHandler registry and dispatcher —
HandlerRegistrystores decorated handlers;Dispatcherroutes incoming messages to handlers by introspecting signatures and binding parametersSession objects —
ClientSessionandServerSessionprovide state dicts, connection metadata, andsend()/kick()methodsComprehensive test suite —
test_app_models.py,test_app_dispatcher.py,test_app_integration.pycovering model serialization, handler dispatch, and end-to-end client/server roundtripsDocumentation — New
docs/lowlevel.mdanddocs/protocol.mdreference guides for the protocol and transport layersUpdated README — Simplified introduction with high-level examples, feature list focused on the new API, and installation instructions
Package exports — Top-level
sfs2x/__init__.pyre-exports the high-level API for convenienceImplementation Details
FieldSpectuples, generates__init__,__repr__,__eq__, and serialization methodsfield(sfs_key, default=..., default_factory=...)marks attributes and binds them to SFSObject keys; supports nested models and optional fieldsinspect_handler()walks function signatures and classifies parameters (MESSAGE,PAYLOAD,SESSION,MODEL,UNION_MODEL) for runtime bindingExceptionsoraiseworks;Reply(model)wrapper for non-Response modelson_connect,on_disconnect,on_errorcallbacks fire at appropriate times in the dispatcher loopAll changes are backward compatible — the low-level
sfs2x.core,sfs2x.protocol, andsfs2x.transportAPIs remain unchanged.https://claude.ai/code/session_01WDQmcTcMNhA9uDjpztRFao