How shoonya-dev/shoonyaapi-py works
Project Type & Purpose
• It’s a Python client-library (wrapper/facade) for Shoonya’s OMS REST API + real-time WebSocket feed.
• Provides a unified interface (api_helper.py → ShoonyaApiPy) so downstream scripts/apps can log in, query data, place orders, and subscribe to market/order feeds without dealing with raw HTTP or WS details.
File-Structure Analysis
• Top-level:
– api_helper.py: core connector class (ShoonyaApiPy).
– cred.yml: external credentials/config.
– example_market.py / example_orders.py: sample client usage.
– test_api.py + tests/*: unit/integration tests exercising each API method.
– dist/: packaged wheel for distribution.
– requirements.txt: dependencies (e.g., requests, websocket-client, PyYAML).
• No frontend; purely library + examples + tests + packaging.
README Insights
• Describes all REST methods (login, logout, place_order, modify_order, …) and WebSocket subscribe flows.
• Emphasizes one WS connection for both market tick feed and order updates.
• Shows sample code for builder pattern: instantiate ShoonyaApiPy, configure endpoints, login, then call methods or start_websocket + subscribe.
Main Components & Relationships
[ Client Application ]
• example_market.py, example_orders.py, any downstream script
↘ calls
[ Shoonya Python Wrapper (api_helper.py) ]
• ShoonyaApiPy class
• Loads cred.yml → holds userid, vendor_code, api_secret, URLs
• Uses HTTP client to invoke Shoonya REST endpoints
• Uses WebSocket client to open a single socket for tick + order feeds
↘ HTTP JSON requests/responses ↔
[ Shoonya REST API Endpoints ] (external service)
↗ WebSocket JSON streams ↔
[ Shoonya WebSocket Server ] (external feed)
Supporting components:
• [Configuration Store] (cred.yml) → used by wrapper on init
• [Local Cache / Example Data] (tests/realtime_excel_feed.xlsx) for tests
• [Test Suite] (test_api.py + tests/) → invokes wrapper methods, asserts JSON responses
• [Distribution] (dist/*.whl) ← built from code + requirements
Architectural Patterns & Design Principles
• Facade Pattern: ShoonyaApiPy hides raw HTTP/WS complexity behind simple verbs.
• Single Responsibility: api_helper.py handles comms; examples handle business usage; tests handle validation.
• Dependency Injection: endpoints & creds loaded from YAML; tests can swap in mocks.
• Event-Driven: WebSocket callbacks for market and order updates.
Essential Details to Capture
• Single WebSocket connection for two callback streams (market + order).
• How the wrapper maintains session token (susertoken/jKey) in headers or WS handshake.
• The requirement to HTTP‐login before invoking any other REST methods.
• Local credential file separation (no hard-coding).
• Test harness invoking each REST/WS method ensures coverage.