integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
from typing import Any
|
||||
|
||||
from webapi.imports.connectors.base import Connector
|
||||
from webapi.imports.connectors.errors import ConnectorError
|
||||
|
||||
CONNECTORS: dict[str, type[Connector]] = {}
|
||||
|
||||
|
||||
def register_connector(cls: type[Connector]) -> type[Connector]:
|
||||
if cls.kind in CONNECTORS:
|
||||
raise ValueError(f"connector kind {cls.kind!r} is already registered by {CONNECTORS[cls.kind].__name__}")
|
||||
CONNECTORS[cls.kind] = cls
|
||||
return cls
|
||||
|
||||
|
||||
def connector_class(kind: str | None) -> type[Connector]:
|
||||
cls = CONNECTORS.get(kind or "")
|
||||
if cls is None:
|
||||
raise ConnectorError(f"unknown source kind {kind!r} (available: {', '.join(sorted(CONNECTORS))})")
|
||||
return cls
|
||||
|
||||
|
||||
def build_connector(kind: str | None, payload: dict[str, Any]) -> Connector:
|
||||
if not isinstance(payload, dict):
|
||||
raise ConnectorError("payload must be an object")
|
||||
try:
|
||||
return connector_class(kind).from_payload(payload)
|
||||
except KeyError as exc:
|
||||
raise ConnectorError(f"missing field in payload: {exc}") from exc
|
||||
|
||||
|
||||
def kind_for_filename(filename: str) -> str | None:
|
||||
extension = filename.lower().rsplit(".", 1)[-1]
|
||||
return next((kind for kind, cls in CONNECTORS.items() if extension in cls.extensions), None)
|
||||
|
||||
|
||||
def catalog() -> list[dict[str, Any]]:
|
||||
return [cls.describe() for cls in CONNECTORS.values()]
|
||||
Reference in New Issue
Block a user