Bind NPM clients to configured endpoints

This commit is contained in:
Lyncoln
2026-09-19 16:58:15 -04:00
parent d4d200551e
commit 79342590ba
2 changed files with 90 additions and 2 deletions
+15 -2
View File
@@ -4,12 +4,22 @@ from typing import Any
import requests
import urllib3
from .adapters import Endpoint
class NpmClient:
"""Authenticated client for the Nginx Proxy Manager API."""
def __init__(self, config):
def __init__(
self,
config,
endpoint: Endpoint | None = None,
):
self.config = config
self.endpoint = endpoint or config.npm_endpoints[0]
if self.endpoint.plugin != "npm":
raise RuntimeError("NpmClient requires an npm endpoint")
self.identity = os.getenv("NPM_IDENTITY")
self.secret = os.getenv("NPM_SECRET")
@@ -30,7 +40,10 @@ class NpmClient:
@property
def api_url(self) -> str:
return f"{self.config.proxy_url}/api"
return (
f"{self.config.proxy_api_scheme}://"
f"{self.endpoint.address}/api"
)
def login(self) -> None:
response = self.session.post(
+75
View File
@@ -0,0 +1,75 @@
import os
import unittest
from unittest.mock import patch
from dns_sync.adapters import Endpoint
from dns_sync.config import AppConfig
from dns_sync.npm_client import NpmClient
class NpmClientEndpointTests(unittest.TestCase):
def config(self):
return AppConfig.from_env()
def test_default_client_uses_first_configured_endpoint(self):
with patch.dict(
os.environ,
{
"NPM_ENDPOINTS": "192.168.1.3,192.168.1.4:8081",
"DNS_TARGET_IP": "192.168.1.101",
"NPM_IDENTITY": "test@example.com",
"NPM_SECRET": "not-a-real-secret",
},
clear=True,
):
config = self.config()
with patch(
"dns_sync.npm_client.requests.Session.request"
) as request:
client = NpmClient(config)
request.assert_not_called()
self.assertEqual(client.api_url, "http://192.168.1.3:81/api")
def test_client_can_select_specific_configured_endpoint(self):
with patch.dict(
os.environ,
{
"NPM_ENDPOINTS": "192.168.1.3,192.168.1.4:8081",
"DNS_TARGET_IP": "192.168.1.101",
"NPM_IDENTITY": "test@example.com",
"NPM_SECRET": "not-a-real-secret",
},
clear=True,
):
config = self.config()
client = NpmClient(config, config.npm_endpoints[1])
self.assertEqual(
client.api_url,
"http://192.168.1.4:8081/api",
)
self.assertEqual(
client.endpoint.identity,
"npm://192.168.1.4:8081",
)
def test_client_rejects_non_npm_endpoint(self):
with patch.dict(
os.environ,
{
"PROXY_IP": "192.168.1.101",
"NPM_IDENTITY": "test@example.com",
"NPM_SECRET": "not-a-real-secret",
},
clear=True,
):
config = self.config()
endpoint = Endpoint("verizon", "192.168.1.1")
with self.assertRaisesRegex(RuntimeError, "npm endpoint"):
NpmClient(config, endpoint)
if __name__ == "__main__":
unittest.main()