Skip to content

docs: comprehensive API reference and cleanup#4

Merged
eman merged 7 commits intomainfrom
docs/comprehensive-api-reference
Feb 6, 2026
Merged

docs: comprehensive API reference and cleanup#4
eman merged 7 commits intomainfrom
docs/comprehensive-api-reference

Conversation

@eman
Copy link
Owner

@eman eman commented Feb 6, 2026

Overview

Comprehensive documentation overhaul transforming light reference material into a professional API reference manual.

Changes

Socket API Documentation (259 lines)

  • Document all 7 commands with request/response examples
    • list_devices, get_instantaneous_demand, get_device_data
    • get_network_info, get_history_data
    • Complete unsupported commands list
  • Add field-by-field explanations
  • Include unit conversion formulas and calculations

HTTP API Documentation (143 lines)

  • Authentication: HTTP Basic Auth with curl examples
  • Document /cgi-bin/cgi_manager endpoint thoroughly
  • All commands: get_usage_data, get_device_list, get_instantaneous_demand
  • Request/response examples in XML and JSON

Clients API Documentation (139 lines)

  • Python usage examples for both Socket and HTTP clients
  • Comparison table: Socket vs HTTP (protocol, auth, speed, features)
  • Clear recommendations for when to use each approach
  • Complete method signatures and return types

Data Models Documentation (255 lines)

  • All 6 models documented: DeviceInfo, DeviceList, InstantaneousDemand, CurrentSummation, UsageData, NetworkInfo
  • Field-by-field descriptions with examples
  • Complete JSON response examples for each model
  • Usage patterns and code examples
  • Explanation of computed properties and unit conversions

Cleanup

  • Remove embedded agent reasoning from socket_api.md
  • Professional reference manual format throughout
  • Clean parenthetical discussions and internal reasoning

Statistics

  • Total lines: 838 professional documentation (from ~130)
  • Code blocks: 35+ examples
  • Coverage: Complete API reference for all endpoints, commands, and models
  • Quality: Professional reference manual format with examples

Type

  • Bug fix
  • Documentation
  • Feature
  • Breaking change

eman added 6 commits February 6, 2026 11:46
- Socket API: Document all 7 commands with request/response examples
  - list_devices, get_instantaneous_demand, get_device_data
  - get_network_info, get_history_data, plus unsupported commands
  - Add field-by-field explanations and unit conversion formulas

- HTTP API: Expand documentation with complete endpoint details
  - Authentication (HTTP Basic Auth) with curl examples
  - Document get_usage_data, get_device_list, get_instantaneous_demand
  - Include request/response examples in XML and JSON

- Clients API: Add comprehensive usage guide
  - Full Python examples for both Socket and HTTP clients
  - Comparison table: Socket vs HTTP (protocol, auth, speed, features)
  - Clear recommendations for when to use each approach

- Data Models: Complete reference with all fields documented
  - Document all 6 models: DeviceInfo, DeviceList, InstantaneousDemand
  - CurrentSummation, UsageData, NetworkInfo
  - Include JSON response examples and usage patterns
  - Explain computed properties and unit conversions

- Remove embedded agent reasoning from socket_api.md
  - Clean up parenthetical authentication discussion
  - Professional reference manual format throughout

Expands documentation from ~130 lines to 838 lines with 35+ code examples.
… capabilities

- Add comprehensive real-time monitoring examples (polling-based)
  - Basic continuous demand monitoring with 10s interval
  - Alert example for high demand with cooldown
  - Data logging to CSV with network status
  - Peak demand tracking with summation

- Clarify push notification capabilities
  - Acknowledge gateway may support event callbacks
  - Don't claim definitively that push is unsupported
  - Note that library doesn't currently expose these features
  - Recommend checking official API docs and web interface

- Add performance considerations for polling
  - Socket connection efficiency
  - Monitoring scale limits
  - Network impact analysis
  - Graceful error handling patterns

- Revise comparison table to reflect polling-based approach
Add comprehensive support for /cgi-bin/post_manager endpoint:
- New EagleConfigClient class for system-level configuration
- Methods for mDNS, remote management, cloud status, device config
- Full documentation of all configuration commands
- Proper HTTP Basic Auth integration

Supported operations:
- get_mdns_status() / set_mdns(enabled) - Local network discovery
- get_device_config() - Firmware, hardware, MAC address
- get_cloud_status() - Cloud service connectivity
- get_remote_management_status() / set_remote_management(enabled)
- get_time_status() - NTP synchronization
- get_network_info() - IP, DNS, DHCP settings
- get_ssh_status() - SSH access status
- run_command(name, **kwargs) - Generic command execution

Documentation updates:
- Add api/config.md with complete reference
- Expand openapi.yaml with post_manager endpoints
- Update mkdocs.yml navigation to include Configuration section
- Update index.md to highlight configuration capabilities
- Update clients.md overview to mention all three client types

Exports in __all__:
- EagleConfigClient now exported from meter_reader package
- ConfigClient alias available in clients module
Test Coverage:
- test_config_client.py: 25 tests targeting EagleConfigClient
  - Client initialization (basic and custom timeout)
  - All 8 configuration methods (mDNS, device, cloud, remote, time, network, SSH)
  - Generic run_command() with and without parameters
  - Error handling (HTTP errors, connection failures)
  - XML command structure validation
  - Authentication and timeout passing
  - Content-Type header verification
  - URL format validation
  - Parameter filtering (None values excluded)
  - JSON response parsing
  - Multiple independent calls

- test_integration.py: 16 tests for package-level integration
  - Package exports verification (3 clients, 5 models)
  - Client instantiation (all three types)
  - Module exports from clients package
  - Client method presence (data vs configuration)
  - Address/URL format consistency
  - Authentication format consistency

Test Results:
- Total tests: 41 new tests + 82 existing tests = 123 total
- All 123 tests PASS
- Coverage: 92% overall, 100% for config client
- Config client: 47/47 statements covered (100%)
- HTTP client: 48/48 statements covered (100%)

Target Coverage Achievement:
- Config client: 100% (target: 80%) ✅
- HTTP client: 100% (target: 80%) ✅
- Overall: 92% (target: 80%) ✅

Key Test Areas:
✓ Client initialization patterns
✓ HTTP request parameters (auth, timeout, headers)
✓ XML command formatting
✓ JSON response parsing
✓ Error handling and exceptions
✓ Parameter validation and filtering
✓ Multiple sequential operations
✓ Package exports and compatibility
Property-based testing for critical protocol functions using Hypothesis:

Coverage Areas:

1. Two's Complement Conversions (10 tests)
   - Positive value preservation across bit widths (8, 16, 32-bit)
   - Negative value conversion validation
   - Boundary condition testing (max/min values)
   - Roundtrip consistency validation

2. Data Conversion Functions (9 tests)
   - None value handling
   - MAC address formatting (DeviceMacId, MeterMacId)
   - Timestamp conversion (BEGINNING_OF_TIME epoch handling)
   - Hex value conversion and edge cases
   - InstallCode formatting

3. Demand Calculations (2 tests)
   - Demand formula consistency: (demand × multiplier) ÷ divisor
   - Non-negativity with positive inputs
   - Arbitrary multiplier and divisor combinations

4. Summation Calculations (2 tests)
   - Summation formula consistency for delivered and received
   - Non-negativity with positive inputs
   - Large value handling

5. Network Status Properties (2 tests)
   - Link strength range validation (0-100)
   - ZigBee channel range validation (11-26)

6. Usage Data Properties (2 tests)
   - Usage data consistency with varying inputs
   - Unix timestamp to datetime conversion
   - Field value validation

7. Model Field Validation (2 tests)
   - Device list size consistency
   - Device model fields accept arbitrary values
   - Network status field variations

Test Configuration:
- Reduced example count (50-100) for reasonable runtime
- Health check suppression for slow test detection
- Proper NaN/infinity handling for float strategies

Results:
- 31 property-based tests all passing
- 154 total tests (123 previous + 31 new)
- Coverage maintained at 92%
- Test execution time: ~78 seconds

Benefits of Property-Based Testing:
✓ Discovers edge cases not covered by example-based tests
✓ Tests mathematical correctness of conversions
✓ Validates boundary conditions across all bit widths
✓ Tests with thousands of generated values per property
✓ Ensures protocol robustness against arbitrary inputs
✓ Finds off-by-one errors and precision issues
Linting Fixes:
- Remove unused imports (pytest, call, assume) from test files
- Remove unused variable assignments
- Restore pytest import in test_config_client.py (actually needed)

Dependency Management:
- Add hypothesis>=6.0 to dev dependencies in pyproject.toml
- Required for property-based tests using Hypothesis

All 154 tests passing:
- 123 example-based tests
- 31 property-based tests with Hypothesis
- 92% code coverage

Ruff linting: All checks passed ✓
@eman eman requested a review from Copilot February 6, 2026 21:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR expands the project into a more complete reference by adding extensive API documentation and introducing a new HTTP configuration client, backed by integration + property-based tests.

Changes:

  • Added EagleConfigClient for /cgi-bin/post_manager system configuration commands and exported it from the package.
  • Added integration tests plus a new Hypothesis-based property test suite for protocol/model behaviors.
  • Overhauled and expanded documentation (Socket API, HTTP API, models, clients) and updated MkDocs navigation/OpenAPI content.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tests/test_property_based.py Adds Hypothesis property-based tests for low-level conversions and model computed properties.
tests/test_integration.py Verifies package exports and basic client instantiation/shape.
tests/test_config_client.py Adds unit tests for the new configuration client using request mocking.
src/meter_reader/clients/config.py Introduces the new configuration client making XML POST calls to /cgi-bin/post_manager.
src/meter_reader/clients/init.py Exports ConfigClient from meter_reader.clients.
src/meter_reader/init.py Exports EagleConfigClient at package top-level.
pyproject.toml Adds Hypothesis to dev dependencies to support new property tests.
mkdocs.yml Adds Configuration docs page to the nav.
docs/socket_api.md Rewrites Socket API docs with detailed command coverage and examples.
docs/openapi.yaml Expands OpenAPI descriptions and adds post_manager-related entries/examples.
docs/index.md Updates library feature bullets to include gateway/system configuration.
docs/http_api.md Replaces minimal note with a full HTTP API reference and examples.
docs/api/models.md Expands model documentation with field descriptions, examples, and computed properties.
docs/api/config.md Adds configuration client documentation and usage patterns.
docs/api/clients.md Expands client docs, adds configuration client notes, and adds usage examples/comparisons.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Security & Code Quality:

1. XML Injection Prevention (config.py)
   - Added xml.sax.saxutils.escape() for safe XML text encoding
   - Implemented tag name validation (alphanumeric + underscore allowlist)
   - Prevents injection via special characters like <, >, &
   - All values converted to strings and properly escaped

2. Fixed Hypothesis Test Strategies (test_property_based.py)
   - test_network_status_field: Changed from st.text() to st.sampled_from()
     * Was generating random strings like 'c,nJ' instead of valid statuses
     * Now constrained to: ['Connected', 'Unavailable', 'Joining']

3. Floating-Point Comparison Fixes
   - test_demand_calculation_consistency: Added tolerance comparison (< 1e-9)
   - test_summation_calculation_consistency: Added tolerance comparison (< 1e-9)
   - Prevents brittle failures due to rounding differences

4. Improved Test Assertions
   - test_hex_value_conversion_positive: Changed to use 'Multiplier' key
     * Now properly tests hex-to-integer conversion
     * Stronger assertions: isinstance(int) AND result == value

5. Implemented Roundtrip Property Test
   - test_roundtrip_consistency: Actual roundtrip validation
     * Truncates value to bit width
     * Converts to signed via twos_complement()
     * Reconstructs unsigned from signed
     * Verifies reconstruction matches truncated value

6. OpenAPI Specification Refactor
   - Consolidated synthetic endpoints (/cgi-bin/post_manager-get-*)
   - Single endpoint: /cgi-bin/post_manager with multiple examples
   - Uses 'examples:' field with named request/response examples
   - Commands distinguished via 'name' field in XML payload
   - More realistic and follows OpenAPI best practices

All Tests: 154/154 PASSING ✅
Coverage: 92% maintained
Execution time: ~72 seconds
Copy link
Owner Author

@eman eman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed all review comments:

✅ Comment 1 (XML injection): Added xml.sax.saxutils.escape() and tag name validation to config.py
✅ Comment 2 (Hypothesis status strategy): Changed to st.sampled_from() for valid status values
✅ Comment 3 (Float equality): Added tolerance comparison (< 1e-9) for demand/summation tests
✅ Comment 4 (OpenAPI endpoints): Consolidated to single /cgi-bin/post_manager with examples
✅ Comment 5 (Roundtrip property): Implemented actual roundtrip invariant validation
✅ Comment 6 (Weak assertion): Strengthened with isinstance(int) AND result == value check

All 154 tests passing, 92% coverage maintained.

@eman eman merged commit cd6f9f3 into main Feb 6, 2026
2 checks passed
@eman eman deleted the docs/comprehensive-api-reference branch February 6, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants