Browse Source

Fix: Server enum generator default value

dev
Peter Zaitcev 7 months ago
parent
commit
e67dedffee
1 changed files with 12 additions and 5 deletions
  1. + 12
    - 5
      src/openapi_parser/exporter/client_writer.py

+ 12
- 5
src/openapi_parser/exporter/client_writer.py

@ -58,7 +58,7 @@ class ClientWriter(HeaderWriter, InspectWriter, MethodWriter, FooterWriter):
yield from self.objects_to_from_imports(imports)
yield from other_imports
def parse_server_data(self, client_name: str, servers: List[ModelServer]) -> Option[Tuple[str, Iterator[str]]]:
def parse_server_data(self, client_name: str, servers: List[ModelServer]) -> Option[Tuple[str, str, Iterator[str]]]:
servers = [ s for s in servers if (s.url != '/') ]
if (not servers):
return OptionNone
@ -78,7 +78,7 @@ class ClientWriter(HeaderWriter, InspectWriter, MethodWriter, FooterWriter):
yield f'{s_name} = {s.url!r}'
yield
return Some((f'{enum_name}.{default_server_name}' , _gen()))
return Some((enum_name, f'{enum_name}.{default_server_name}', _gen()))
def dump_security_schema(self, security_schema: ModelSecurityScheme, *, flow: Optional[OAuthFlowsType] = None) -> Iterator[str]:
method_name = self.method_name_pretty(f'provide_{security_schema.name}_authorization')
@ -199,7 +199,8 @@ class ClientWriter(HeaderWriter, InspectWriter, MethodWriter, FooterWriter):
logger_name = f'{parser.name}.client'
self.export(client_name)
default_server, enum_gen = tuple_transform(self.parse_server_data(client_name, parser.metadata.servers), 2)
enum_name, default_server, enum_gen = tuple_transform(self.parse_server_data(client_name, parser.metadata.servers), 2)
server_type = enum_name.map('Union[{}, str]'.format).get_or_else('str')
if (enum_gen.non_empty):
yield from enum_gen.get
@ -207,13 +208,19 @@ class ClientWriter(HeaderWriter, InspectWriter, MethodWriter, FooterWriter):
with self.indent():
yield from self.smart_description(partial(self.generate_item_description, item=parser.metadata.info, item_type='client'))
yield 'server: str'
yield f'server: {server_type}'
yield f'logger_name: str = {logger_name!r}'
yield f'model_encoder: Type[IEncoder] = {self.ref_name_pretty(FilteringJsonEncoder)}'
yield
yield from self.write_method_head('__init__', params=[ "server: str" + default_server.map(' = {}'.format).get_or_else('') ], method_type=MethodType.RegularMethod)
yield from self.write_method_head('__init__', params=[ self.join_attribute('server', Some(server_type), default_server) ], method_type=MethodType.RegularMethod)
with self.indent():
if (enum_name.non_empty):
yield f'if (isinstance(server, {enum_name.get})):'
with self.indent():
yield 'server = server.value'
yield
code = \
"""
super().__init__()

Loading…
Cancel
Save