Skip to content

Commit

Permalink
Implement code suggesitions
Browse files Browse the repository at this point in the history
  • Loading branch information
sternakt committed Oct 7, 2024
1 parent 42319e3 commit 5973922
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions fastagency/api/openapi/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

Alias: TypeAlias = type["BaseSecurity"]
BaseSecurityType: TypeAlias = type["BaseSecurity"]


class BaseSecurity(BaseModel):
Expand Down Expand Up @@ -44,7 +44,9 @@ def is_supported(cls, type: str, schema_parameters: dict[str, Any]) -> bool:
return cls.type == type and cls.in_value == schema_parameters.get("in")

@classmethod
def get_security_class(cls, type: str, schema_parameters: dict[str, Any]) -> Alias:
def get_security_class(
cls, type: str, schema_parameters: dict[str, Any]
) -> BaseSecurityType:
sub_classes = cls.__subclasses__()

for sub_class in sub_classes:
Expand All @@ -60,7 +62,7 @@ def get_security_class(cls, type: str, schema_parameters: dict[str, Any]) -> Ali

@classmethod
def get_security_parameters(cls, schema_parameters: dict[str, Any]) -> str:
return f"{cls.__name__}(name='{schema_parameters.get('name')}')"
return f"{cls.__name__}(name=\"{schema_parameters.get('name')}\")"


class BaseSecurityParameters(Protocol):
Expand Down Expand Up @@ -205,7 +207,7 @@ def is_supported(cls, type: str, schema_parameters: dict[str, Any]) -> bool:
def get_security_parameters(cls, schema_parameters: dict[str, Any]) -> str:
name = schema_parameters.get("name")
token_url = f'{schema_parameters.get("server_url")}/{schema_parameters["flows"]["password"]["tokenUrl"]}'
return f"{cls.__name__}(name='{name}', token_url='{token_url}')"
return f'{cls.__name__}(name="{name}", token_url="{token_url}")'

class Parameters(BaseModel): # BaseSecurityParameters
"""OAuth2 Password Bearer security class."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ autogen = [
openapi = [
"fastapi-code-generator==0.5.1",
"fastapi>=0.112,<1",
"python-multipart>=0.0.12",
"requests", # do not pin it
]

Expand Down Expand Up @@ -138,6 +137,7 @@ testing = [
"PyYAML==6.0.2",
"watchfiles==0.24.0",
"email-validator==2.2.0",
"python-multipart>=0.0.12",
]

dev = [
Expand Down
4 changes: 2 additions & 2 deletions tests/api/openapi/security/expected_main_gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ app = OpenAPI(
response_model=Any,
responses={'422': {'model': HTTPValidationError}},
security=[
APIKeyHeader(name='access_token'),
APIKeyCookie(name='access_token'),
APIKeyHeader(name="access_token"),
APIKeyCookie(name="access_token"),
],
)
def read_items_items__get(
Expand Down

0 comments on commit 5973922

Please sign in to comment.