-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from lofidevops/openapi
Export title and version to openapi.yaml
- Loading branch information
Showing
7 changed files
with
193 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,52 @@ | ||
""" | ||
Helpers to translate acceptable metadata to OpenAPI specifications (OAS). | ||
""" | ||
|
||
from dataclasses import dataclass, field | ||
from typing import Any | ||
|
||
import yaml | ||
|
||
from acceptable._service import APIMetadata | ||
|
||
def _to_dict(source: Any): | ||
|
||
def _to_dict(source: Any): | ||
if hasattr(source, "_to_dict"): | ||
return source._to_dict() # noqa | ||
elif type(source) == dict: | ||
source_dict = {} | ||
for key, value in source.items(): | ||
source_dict[key] = _to_dict(value) | ||
return source_dict | ||
return {key: _to_dict(value) for key, value in source.items()} | ||
elif type(source) == list: | ||
source_list = [] | ||
for item in source: | ||
source_list.append(_to_dict(item)) | ||
return source_list | ||
return [_to_dict(value) for value in source] | ||
elif hasattr(source, "__dict__"): | ||
return {key: _to_dict(value) for key, value in source.__dict__.items()} | ||
else: | ||
return source | ||
|
||
|
||
def dump(metadata, stream): | ||
@dataclass | ||
class OasInfo(object): | ||
description: str = "" | ||
version: str = "" | ||
title: str = "" | ||
tags: list = field(default_factory=lambda: []) | ||
contact: dict = field(default_factory=lambda: {"name": "", "email": ""}) | ||
|
||
|
||
@dataclass | ||
class OasRoot31(object): | ||
openapi: str = "3.1.0" | ||
info: OasInfo = OasInfo() | ||
servers: dict = field(default_factory=lambda: {}) | ||
paths: dict = field(default_factory=lambda: {}) | ||
components_schemas: dict = field(default_factory=lambda: {}) | ||
|
||
|
||
def dump(metadata: APIMetadata, stream=None): | ||
service_name = None | ||
if len(metadata.services) == 1: | ||
service_name = list(metadata.services.keys())[0] | ||
|
||
# TODO: parse metadata as OpenAPI specification | ||
oas = OasRoot31() | ||
oas.info.title = service_name or "" | ||
oas.info.version = metadata.current_version or "" | ||
|
||
return yaml.safe_dump( | ||
_to_dict(None), | ||
stream, | ||
default_flow_style=False, | ||
encoding=None, | ||
) | ||
return yaml.safe_dump(_to_dict(oas), stream, default_flow_style=False, encoding=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.