Skip to content

Commit

Permalink
small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
artsmolin committed Oct 12, 2023
1 parent cce055d commit 9c9c374
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
5 changes: 0 additions & 5 deletions pythogen/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
"""
Точка входа. Запуск генератора клиента происходит отсюда.
"""


from importlib import metadata
from typing import Optional

Expand Down
64 changes: 39 additions & 25 deletions pythogen/parsers/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,14 @@ def __init__(
self._parameters_parser = parameters_parser

def parse_item(
self, path_str: str, method: models.HttpMethod, operation_data: dict[str, Any]
self,
path_str: str,
method: models.HttpMethod,
operation_data: dict[str, Any],
) -> models.OperationObject:
"""Спарсить спецификацию метода ручки (POST-, GET-, PUT-запроса и т.п.)"""
"""Parse endpoints method specification (POST/GET/PUT/... request)"""
responses: dict[str, models.ResponseObject] = {}

operation_id: str = operation_data.get('operationId', '')
if not operation_id:
operation_id = (
path_str.removeprefix('/').replace('-', '_').replace('/', '_').replace('{', '').replace('}', '')
)
operation_id = method.value + '_' + operation_id
operation_id = operation_id.lower()
else:
operation_id = operation_id.replace('_', ' ').title().replace(' ', '')
operation_id: str = self.parse_operation_id(path_str, method, operation_data)

for status_code, response_data in operation_data['responses'].items():
if status_code == 'default':
Expand All @@ -53,7 +47,6 @@ def parse_item(
response_data = resolved_ref.ref_data
response_id = resolved_ref.ref_id
else:

response_id = f"{operation_id}Response{status_code}"

responses[status_code] = self._response_parser.parse_item(response_id, response_data)
Expand All @@ -64,24 +57,45 @@ def parse_item(
else:
request_body = None

parameters = []
for parameter_data in operation_data.get('parameters', []):
if parameter_data.get('$ref', None):
resolved_ref = self._ref_resolver.resolve(parameter_data['$ref'])
parameter = self._parameters_parser.parse_item(resolved_ref.ref_id, resolved_ref.ref_data)
parameters.append(parameter)
else:
parameter_id = f'<inline+{models.ParameterObject.__name__}>'
parameter = self._parameters_parser.parse_item(parameter_id, parameter_data)
parameters.append(parameter)

return models.OperationObject(
method=method,
summary=operation_data.get('summary'),
description=operation_data.get('description'),
operation_id=operation_data.get('operationId'),
request_body=request_body,
responses=models.ResponsesObject(patterned=responses),
parameters=parameters,
parameters=self.parse_parameters(operation_data),
path_str=path_str,
)

def parse_operation_id(
self,
path_str: str,
method: models.HttpMethod,
operation_data: dict[str, Any],
) -> str:
operation_id: str = operation_data.get('operationId', '')

if not operation_id:
operation_id = (
path_str.removeprefix('/').replace('-', '_').replace('/', '_').replace('{', '').replace('}', '')
)
operation_id = method.value + '_' + operation_id
operation_id = operation_id.lower()
return operation_id

operation_id = operation_id.replace('_', ' ').title().replace(' ', '')
return operation_id

def parse_parameters(self, operation_data: dict[str, Any]) -> list[models.ParameterObject]:
parameters: list[models.ParameterObject] = []
for parameter_data in operation_data.get('parameters', []):
if parameter_data.get('$ref', None):
resolved_ref = self._ref_resolver.resolve(parameter_data['$ref'])
parameter = self._parameters_parser.parse_item(resolved_ref.ref_id, resolved_ref.ref_data)
parameters.append(parameter)
else:
parameter_id = f'<inline+{models.ParameterObject.__name__}>'
parameter = self._parameters_parser.parse_item(parameter_id, parameter_data)
parameters.append(parameter)
return parameters

0 comments on commit 9c9c374

Please sign in to comment.