|
| 1 | +# This file is a part of globus-registered-api. |
| 2 | +# https://github.com/globusonline/globus-registered-api |
| 3 | +# Copyright 2025 Globus <support@globus.org> |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +""" |
| 7 | +Facade for OpenAPI target processing. |
| 8 | +
|
| 9 | +This module provides a simplified interface for OpenAPI processing |
| 10 | +by coordinating loader, selector, and reducer components. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +from dataclasses import dataclass |
| 16 | +from dataclasses import field |
| 17 | +from pathlib import Path |
| 18 | +from typing import Any |
| 19 | + |
| 20 | +import openapi_pydantic as oa |
| 21 | + |
| 22 | +from globus_registered_api.openapi.loader import load_openapi_spec |
| 23 | +from globus_registered_api.openapi.reducer import OpenAPITarget |
| 24 | +from globus_registered_api.openapi.reducer import reduce_to_target |
| 25 | +from globus_registered_api.openapi.selector import TargetSpecifier |
| 26 | +from globus_registered_api.openapi.selector import find_target |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class ProcessingResult: |
| 31 | + """Result of target processing.""" |
| 32 | + |
| 33 | + target: OpenAPITarget |
| 34 | + warnings: list[str] = field(default_factory=list) |
| 35 | + |
| 36 | + def to_dict(self) -> dict[str, Any]: |
| 37 | + """Convert to the format expected by POST /registered_api.""" |
| 38 | + return self.target.to_dict() |
| 39 | + |
| 40 | + |
| 41 | +def process_target( |
| 42 | + spec_or_path: oa.OpenAPI | str | Path, |
| 43 | + target: TargetSpecifier, |
| 44 | +) -> ProcessingResult: |
| 45 | + """ |
| 46 | + Process an OpenAPI spec to extract a target endpoint. |
| 47 | +
|
| 48 | + This is the primary entry point for target extraction. It coordinates |
| 49 | + loading, selection, and reduction into a single operation. |
| 50 | +
|
| 51 | + For fine-grained control, use the underlying modules directly: |
| 52 | + - `loader.load_openapi_spec()` - Parse OpenAPI files |
| 53 | + - `selector.find_target()` - Locate targets in a spec |
| 54 | + - `reducer.reduce_to_target()` - Extract target with dependencies |
| 55 | +
|
| 56 | + :param spec_or_path: OpenAPI spec object or path to spec file |
| 57 | + :param target: Target specifier (method, path, optional content-type) |
| 58 | + :return: ProcessingResult containing the reduced spec |
| 59 | + :raises OpenAPILoadError: If spec file cannot be loaded |
| 60 | + :raises TargetNotFoundError: If route or method not found |
| 61 | + :raises AmbiguousContentTypeError: If content-type selection is ambiguous |
| 62 | + """ |
| 63 | + # Load if given a path |
| 64 | + if isinstance(spec_or_path, (str, Path)): |
| 65 | + spec = load_openapi_spec(spec_or_path) |
| 66 | + else: |
| 67 | + spec = spec_or_path |
| 68 | + |
| 69 | + # Find and reduce |
| 70 | + target_info = find_target(spec, target) |
| 71 | + openapi_target = reduce_to_target(spec, target_info) |
| 72 | + |
| 73 | + return ProcessingResult(target=openapi_target) |
0 commit comments