| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | import dataclasses |
| 8 | from dataclasses import dataclass |
| 9 | from http import HTTPStatus |
| 10 | from typing import Callable, ClassVar, Dict, List, Optional, Tuple, Union |
| 11 | |
| 12 | from .specification import ( |
| 13 | Info, |
| 14 | SecurityScheme, |
| 15 | SecuritySchemeAPI, |
| 16 | SecuritySchemeHTTP, |
| 17 | SecuritySchemeOpenIDConnect, |
| 18 | Server, |
| 19 | ) |
| 20 | |
| 21 | HTTPStatusCode = Union[HTTPStatus, int, str] |
| 22 | |
| 23 | |
| 24 | @dataclass |
| 25 | class Options: |
| 26 | """ |
| 27 | :param server: Base URL for the API endpoint. |
| 28 | :param info: Meta-information for the endpoint specification. |
| 29 | :param version: OpenAPI specification version as a tuple of major, minor, revision. |
| 30 | :param default_security_scheme: Security scheme to apply to endpoints, unless overridden on a per-endpoint basis. |
| 31 | :param extra_types: Extra types in addition to those found in operation signatures. Use a dictionary to group related types. |
| 32 | :param use_examples: Whether to emit examples for operations. |
| 33 | :param success_responses: Associates operation response types with HTTP status codes. |
| 34 | :param error_responses: Associates error response types with HTTP status codes. |
| 35 | :param error_wrapper: True if errors are encapsulated in an error object wrapper. |
| 36 | :param property_description_fun: Custom transformation function to apply to class property documentation strings. |
| 37 | :param captions: User-defined captions for sections such as "Operations" or "Types", and (if applicable) groups of extra types. |
| 38 | """ |
| 39 | |
| 40 | server: Server |
| 41 | info: Info |
| 42 | version: Tuple[int, int, int] = (3, 1, 0) |
| 43 | default_security_scheme: Optional[SecurityScheme] = None |
| 44 | extra_types: Union[List[type], Dict[str, List[type]], None] = None |
| 45 | use_examples: bool = True |
| 46 | success_responses: Dict[type, HTTPStatusCode] = dataclasses.field( |
| 47 | default_factory=dict |
| 48 | ) |
| 49 | error_responses: Dict[type, HTTPStatusCode] = dataclasses.field( |
| 50 | default_factory=dict |
| 51 | ) |
| 52 | error_wrapper: bool = False |
| 53 | property_description_fun: Optional[Callable[[type, str, str], str]] = None |
| 54 | captions: Optional[Dict[str, str]] = None |
| 55 | |
| 56 | default_captions: ClassVar[Dict[str, str]] = { |
| 57 | "Operations": "Operations", |
| 58 | "Types": "Types", |
| 59 | "Events": "Events", |
| 60 | "AdditionalTypes": "Additional types", |
| 61 | } |
| 62 | |
| 63 | def map(self, id: str) -> str: |
| 64 | "Maps a language-neutral placeholder string to language-dependent text." |
| 65 | |
| 66 | if self.captions is not None: |
| 67 | caption = self.captions.get(id) |
| 68 | if caption is not None: |
| 69 | return caption |
| 70 | |
| 71 | caption = self.__class__.default_captions.get(id) |
| 72 | if caption is not None: |
| 73 | return caption |
| 74 | |
| 75 | raise KeyError(f"no caption found for ID: {id}") |