Skip to content

Commit

Permalink
add openapi tags
Browse files Browse the repository at this point in the history
  • Loading branch information
voidZXL committed Nov 16, 2024
1 parent 609b21b commit e1b9619
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
2 changes: 2 additions & 0 deletions utilmeta/conf/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ def get_result(self, func, *args, **kwargs):
future = self._pool.submit(func, *args, **kwargs)
return future.result()

def submit(self, func, *args, **kwargs):
self._pool.submit(func, *args, **kwargs)

# pool = ThreadPool()
1 change: 1 addition & 0 deletions utilmeta/core/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ def _generate_routes(cls):
name=key,
route=val.route,
summary=val.getattr('summary'),
tags=val.getattr('tags'),
description=val.getattr('description'),
deprecated=val.getattr('deprecated'),
private=val.getattr('private'),
Expand Down
4 changes: 2 additions & 2 deletions utilmeta/core/api/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ def decorator(self, func, generator: APIGenerator = None):
def __call__(self, *fn_or_routes,
cls=None,
summary: str = None,
alias: Union[str, List[str]] = None,
# alias: Union[str, List[str]] = None,
deprecated: bool = None,
idempotent: bool = None,
private: bool = None,
priority: int = None,
eager: bool = None,
tags: List[str] = None,
tags: List[Union[dict, str]] = None,
description: str = None,
extension: dict = None,
**kwargs,
Expand Down
2 changes: 1 addition & 1 deletion utilmeta/core/api/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def __init__(self, f: Callable, *,
eager: bool = False,
# openapi specs:
operation_id: str = None,
tags: List[str] = None,
tags: list = None,
summary: str = None,
description: str = None,
local_vars: dict = None,
Expand Down
6 changes: 2 additions & 4 deletions utilmeta/core/api/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def __init__(self,
self.after_hooks = after_hooks or []
self.error_hooks = error_hooks or {}

@property
def response_types(self):
pass

@classmethod
def from_routes(cls, *routes):
# meant to be inherited
Expand Down Expand Up @@ -133,6 +129,7 @@ def __init__(self,
parent: Type['API'] = None,
summary: str = None,
description: str = None,
tags: list = None,
deprecated: bool = None,
private: bool = None,
priority: int = None,
Expand Down Expand Up @@ -169,6 +166,7 @@ def __init__(self,
self.kwargs = kwargs
self.summary = summary
self.description = description or get_doc(handler)
self.tags = tags
self.deprecated = deprecated
self.private = private or handler.__name__.startswith('_')
self.priority = priority
Expand Down
34 changes: 32 additions & 2 deletions utilmeta/core/api/specs/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def __init__(self, service: 'UtilMeta',
self.external_docs = external_docs
self.base_url = base_url
self.pref = Preference.get()
self.tags = {}
# self.operations = {}

def get_def_name(self, t: type):
Expand Down Expand Up @@ -298,6 +299,7 @@ def merge_openapi_docs(self, *docs: dict) -> OpenAPISchema:
paths = {}
additions = {}
security = []
tag_names = []
tags = []
info = None
servers = []
Expand Down Expand Up @@ -334,7 +336,14 @@ def merge_openapi_docs(self, *docs: dict) -> OpenAPISchema:
paths[path] = dict(values)

security.extend(doc.security)
tags.extend(doc.tags)
for tag in doc.tags:
tag_name = tag.get('name') if isinstance(tag, dict) else str(tag)
if not tag_name:
continue
if tag_name in tag_names:
continue
tags.append(tag_name)
tags.append(tag if isinstance(tag, dict) else {'name': tag_name})
for key, val in doc.items():
if key not in self.schema_cls.__parser__.fields:
additions[key] = val
Expand Down Expand Up @@ -511,6 +520,7 @@ def __call__(self):
info=self.generate_info(),
components=self.components,
paths=paths,
tags=list(self.tags.values()),
servers=[self.server]
)
docs = []
Expand Down Expand Up @@ -903,7 +913,7 @@ def from_endpoint(self, endpoint: Endpoint,

operation: dict = dict(
operationId=operation_id,
tags=tags,
tags=self.add_tags(tags),
responses=responses,
security=self.merge_requires(extra_requires, requires)
)
Expand All @@ -920,6 +930,25 @@ def from_endpoint(self, endpoint: Endpoint,
operation.update(extension)
return operation

def add_tags(self, tags: list):
if not tags:
return []
tag_names = []
for tag in tags:
if not tag:
continue
tag_name = None
if isinstance(tag, str):
tag_name = tag
elif isinstance(tag, dict):
tag_name = tag.get('name')
if not tag_name:
continue
tag_names.append(tag_name)
if tag_name not in self.tags:
self.tags[tag_name] = tag if isinstance(tag, dict) else {'name': tag_name}
return tag_names

def from_route(self, route: APIRoute,
*routes: str,
tags: list = (),
Expand All @@ -930,6 +959,7 @@ def from_route(self, route: APIRoute,
# https://spec.openapis.org/oas/v3.1.0#pathItemObject
new_routes = [*routes, route.route] if route.route else list(routes)
new_tags = [*tags, route.name] if route.name else list(tags)
# route_tags = route.get_tags()
path = self._path_join(*new_routes)
route_data = {k: v for k, v in dict(
summary=route.summary,
Expand Down
8 changes: 3 additions & 5 deletions utilmeta/ops/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,14 @@ def sync_resources(self, supervisor: Supervisor = None, force: bool = False):
ops_config = Operations.config()
if not ops_config:
raise TypeError('Operations not configured')

for supervisor in [supervisor] if supervisor else Supervisor.current().filter(connected=True):
supervisors = [supervisor] if supervisor and not supervisor.local else (
Supervisor.filter(connected=True, local=False, service=service.name))
for supervisor in supervisors:
if supervisor.service != service.name:
force = True # name changed

if not supervisor.node_id:
continue
if supervisor.local:
# do not sync local supervisor
continue

print(f'sync resources of [{service.name}] to supervisor[{supervisor.node_id}]...')

Expand Down

0 comments on commit e1b9619

Please sign in to comment.