Skip to content

Add GenericRestApiAdapter#81

Merged
BeArchiTek merged 8 commits intomainfrom
copilot/fix-80
Sep 16, 2025
Merged

Add GenericRestApiAdapter#81
BeArchiTek merged 8 commits intomainfrom
copilot/fix-80

Conversation

Copy link
Contributor

Copilot AI commented Jul 24, 2025

This PR introduces a GenericRestApiAdapter that provides a reusable, configurable base for REST API integrations, eliminating the need to create separate adapters for each tool while maintaining full backward compatibility.

Problem

The existing LibreNMS, Observium, and PeeringManager adapters contained significant code duplication (~500+ lines of nearly identical logic) for:

  • REST client creation and authentication
  • Data fetching and processing
  • Object-to-DiffSync conversion
  • Error handling

This pattern would require creating a new adapter for every REST API tool, leading to maintenance overhead and code bloat.

Solution

🆕 GenericRestApiAdapter

A flexible adapter that supports:

  • Multiple authentication methods: token, x-auth-token, api-key, key, basic
  • Configurable environment variables: Custom variable names per tool
  • Flexible response handling: Supports list, dict, and direct response formats
  • Customizable endpoints and timeouts
  • Tool-specific type naming

🔄 Refactored Existing Adapters

  • LibrenmsAdapter: Now extends GenericRestApiAdapter (67 lines vs 180 original)
  • ObserviumAdapter: Now extends GenericRestApiAdapter (97 lines vs 181 original)
  • PeeringmanagerAdapter: Now extends GenericRestApiAdapter (121 lines vs 236 original)
  • Full backward compatibility: Existing configurations continue to work unchanged

Usage Examples

Generic Configuration for Any Tool

adapters:
  - name: "monitoring_tool"
    source: "GenericRestApi"
    adapter_type: "CustomMonitoring"
    settings:
      url: "https://api.example.com"
      auth_method: "api-key"
      token: "your_api_key"
      api_endpoint: "/v2"

Backward Compatible LibreNMS

# This continues to work exactly as before
adapters:
  - name: "librenms"
    source: "LibreNMS" 
    settings:
      url: "https://librenms.example.com"
      token: "librenms_token"

Benefits

  • 🎯 Eliminates duplicate adapters: No need for tool-specific adapters for standard REST APIs
  • 🔧 Reduces maintenance: Common logic centralized in one place
  • 🚀 Faster onboarding: New tools require only configuration, no coding
  • 🛡️ Maintains compatibility: Zero breaking changes to existing code
  • Comprehensive testing: 17 test cases covering all functionality

Files Added/Modified

  • NEW: infrahub_sync/adapters/generic_rest_api.py - Main generic adapter
  • UPDATED: infrahub_sync/adapters/librenms.py - Now extends generic adapter
  • UPDATED: infrahub_sync/adapters/observium.py - Now extends generic adapter
  • UPDATED: infrahub_sync/adapters/peeringmanager.py - Now extends generic adapter
  • NEW: examples/generic_rest_api_examples.md - Configuration documentation
  • NEW: examples/generic_adapter_example.py - Working demonstration script
  • NEW: tests/adapters/test_generic_rest_api.py - Comprehensive test suite
  • UPDATED: tests/adapters/test_backward_compatibility.py - Compatibility verification including PeeringManager

Testing

All tests pass including:

  • ✅ Generic adapter functionality (10 tests)
  • ✅ Backward compatibility verification (7 tests including PeeringManager)
  • ✅ Multiple authentication methods
  • ✅ Response format handling
  • ✅ Environment variable configuration

Fixes #80.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 24, 2025

Deploying infrahub-sync with  Cloudflare Pages  Cloudflare Pages

Latest commit: f058cdd
Status: ✅  Deploy successful!
Preview URL: https://404c65d0.infrahub-sync.pages.dev
Branch Preview URL: https://copilot-fix-80.infrahub-sync.pages.dev

View logs

Copilot AI and others added 2 commits July 24, 2025 08:57
Co-authored-by: BeArchiTek <1334310+BeArchiTek@users.noreply.github.com>
Co-authored-by: BeArchiTek <1334310+BeArchiTek@users.noreply.github.com>
Copilot AI changed the title [WIP] feature: Transform or create a generic Adapter from the RestApiClient feat: Add GenericRestApiAdapter to eliminate code duplication across REST API adapters Jul 24, 2025
Copilot AI requested a review from BeArchiTek July 24, 2025 09:01
@BeArchiTek
Copy link
Contributor

@copilot look good to me, update LibrenmsAdapter as well

Copy link
Contributor Author

Copilot AI commented Sep 15, 2025

@copilot look good to me, update LibrenmsAdapter as well

The LibreNMS adapter has already been updated as part of this PR! It now extends the GenericRestApiAdapter and has been reduced from 179 lines to 67 lines (~63% reduction) while maintaining full backward compatibility. All tests are passing, including specific backward compatibility tests for LibreNMS configurations (commit d9cc570).

@BeArchiTek
Copy link
Contributor

@copilot I meant PeeringManager Adapter as it's using the RestApiclient as well.

Co-authored-by: BeArchiTek <1334310+BeArchiTek@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Sep 15, 2025

@copilot I meant PeeringManager Adapter as it's using the RestApiclient as well.

Perfect catch! The PeeringManager adapter has now been refactored to extend the GenericRestApiAdapter as well.

Changes made:

  • Reduced from 236 lines to 121 lines (~49% reduction)
  • Eliminated duplicate REST client creation, data fetching, and object conversion logic
  • Maintained full backward compatibility with existing configurations
  • Preserved the custom update method specific to PeeringManager
  • Added comprehensive backward compatibility tests

All 17 tests are passing, including new PeeringManager backward compatibility tests. The adapter now follows the same pattern as LibreNMS and Observium while maintaining its unique functionality (commit ae9395d).

@BeArchiTek BeArchiTek marked this pull request as ready for review September 15, 2025 15:24
@BeArchiTek BeArchiTek changed the title feat: Add GenericRestApiAdapter to eliminate code duplication across REST API adapters Add GenericRestApiAdapter Sep 15, 2025
@BeArchiTek BeArchiTek merged commit ec44d06 into main Sep 16, 2025
14 checks passed
@BeArchiTek BeArchiTek deleted the copilot/fix-80 branch September 16, 2025 11:48
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.

feature: Transform or create a generic Adapter from the RestApiClient

2 participants