Skip to content

Commit

Permalink
Update indent-style.
Browse files Browse the repository at this point in the history
  • Loading branch information
hasansezertasan committed Feb 3, 2024
1 parent e147ba3 commit 78ce738
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 311 deletions.
12 changes: 6 additions & 6 deletions examples/fastapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@

@app.get("/")
def root() -> dict[str, str]:
return {"Hello": "World"}
return {"Hello": "World"}


@app.get("/swaggerui", response_class=HTMLResponse, include_in_schema=False)
def get_swaggerui() -> str:
return SwaggerUI(title="Swagger UI").render()
return SwaggerUI(title="Swagger UI").render()


@app.get("/redoc", response_class=HTMLResponse, include_in_schema=False)
def get_redoc() -> str:
return ReDoc(title="ReDoc").render()
return ReDoc(title="ReDoc").render()


@app.get("/scalar", response_class=HTMLResponse, include_in_schema=False)
def get_scalar() -> str:
return Scalar(title="Scalar").render()
return Scalar(title="Scalar").render()


@app.get("/elements", response_class=HTMLResponse, include_in_schema=False)
def get_elements() -> str:
return Elements(title="Elements").render()
return Elements(title="Elements").render()


@app.get("/rapidoc", response_class=HTMLResponse, include_in_schema=False)
def get_rapidoc() -> str:
return RapiDoc(title="RapiDoc").render()
return RapiDoc(title="RapiDoc").render()
12 changes: 6 additions & 6 deletions examples/litestar/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@

@get("/")
def root() -> dict[str, str]:
return {"Hello": "World"}
return {"Hello": "World"}


@get("/swaggerui", media_type=MediaType.HTML, include_in_schema=False)
def get_swaggerui() -> str:
return SwaggerUI(title="Swagger UI", openapi_url=openapi_url).render()
return SwaggerUI(title="Swagger UI", openapi_url=openapi_url).render()


@get("/redoc", media_type=MediaType.HTML, include_in_schema=False)
def get_redoc() -> str:
return ReDoc(title="ReDoc", openapi_url=openapi_url).render()
return ReDoc(title="ReDoc", openapi_url=openapi_url).render()


@get("/scalar", media_type=MediaType.HTML, include_in_schema=False)
def get_scalar() -> str:
return Scalar(title="Scalar", openapi_url=openapi_url).render()
return Scalar(title="Scalar", openapi_url=openapi_url).render()


@get("/elements", media_type=MediaType.HTML, include_in_schema=False)
def get_elements() -> str:
return Elements(title="Elements", openapi_url=openapi_url).render()
return Elements(title="Elements", openapi_url=openapi_url).render()


@get("/rapidoc", media_type=MediaType.HTML, include_in_schema=False)
def get_rapidoc() -> str:
return RapiDoc(title="RapiDoc", openapi_url=openapi_url).render()
return RapiDoc(title="RapiDoc", openapi_url=openapi_url).render()


app = Litestar([root, get_swaggerui, get_redoc, get_scalar, get_elements, get_rapidoc])
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fixable = ["ALL"]
# Like Black, use double quotes for strings.
quote-style = "double"
# Like Black, indent with spaces, rather than tabs.
indent-style = "tab"
indent-style = "space"
# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false
# Like Black, automatically detect the appropriate line ending.
Expand Down
150 changes: 75 additions & 75 deletions src/openapidoc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,81 @@

@dataclass
class Base:
"""Base class for alternative API docs."""
"""Base class for alternative API docs."""

title: Annotated[
str,
Doc(
"""
title: Annotated[
str,
Doc(
"""
The HTML `<title>` content, normally shown in the browser tab.
"""
),
]
js_url: Annotated[
str,
Doc(
"""
),
]
js_url: Annotated[
str,
Doc(
"""
The URL to use to load the JavaScript.
It is normally set to a CDN URL.
"""
),
]
openapi_url: Annotated[
str,
Doc(
"""
),
]
openapi_url: Annotated[
str,
Doc(
"""
The OpenAPI URL that page should load and use.
Default URL `/openapi.json`.
"""
),
] = "/openapi.json"
head_js_urls: Annotated[
List[str],
Doc(
"""
),
] = "/openapi.json"
head_js_urls: Annotated[
List[str],
Doc(
"""
A list of URLs to JavaScript files that should be loaded in the `<head>` tag.
"""
),
] = field(default_factory=list)
tail_js_urls: Annotated[
List[str],
Doc(
"""
),
] = field(default_factory=list)
tail_js_urls: Annotated[
List[str],
Doc(
"""
A list of URLs to JavaScript files that should be loaded at the end of the `<body>` tag.
"""
),
] = field(default_factory=list)
head_css_urls: Annotated[
List[str],
Doc(
"""
),
] = field(default_factory=list)
head_css_urls: Annotated[
List[str],
Doc(
"""
A list of URLs to CSS files that should be loaded in the `<head>` tag.
"""
),
] = field(default_factory=list)
favicon_url: Annotated[
str,
Doc(
"""
),
] = field(default_factory=list)
favicon_url: Annotated[
str,
Doc(
"""
The URL of the favicon to use. It is normally shown in the browser tab.
"""
),
] = "/favicon.ico"
),
] = "/favicon.ico"

def render(self) -> str:
"""Generate and return the HTML response that leads page for alternative API docs."""
html_template = self.get_html_template()
return html_template.format(
title=self.title,
favicon_url=self.favicon_url,
head_css_str=self.get_head_css_str(),
head_js_str=self.get_head_js_str(),
tail_js_str=self.get_tail_js_str(),
)
def render(self) -> str:
"""Generate and return the HTML response that leads page for alternative API docs."""
html_template = self.get_html_template()
return html_template.format(
title=self.title,
favicon_url=self.favicon_url,
head_css_str=self.get_head_css_str(),
head_js_str=self.get_head_js_str(),
tail_js_str=self.get_tail_js_str(),
)

def get_html_template(self) -> str:
"""Return the HTML template for the alternative API docs."""
html = """
def get_html_template(self) -> str:
"""Return the HTML template for the alternative API docs."""
html = """
<!DOCTYPE html>
<html>
<head>
Expand All @@ -93,25 +93,25 @@ def get_html_template(self) -> str:
</body>
</html>
"""
return html
return html

def get_tail_js_str(self) -> str:
"""Return the string of JavaScript URLs that should be loaded at the end of the `<body>` tag."""
string = "\n".join([f'<script src="{url}"></script>' for url in self.tail_js_urls])
return string
def get_tail_js_str(self) -> str:
"""Return the string of JavaScript URLs that should be loaded at the end of the `<body>` tag."""
string = "\n".join([f'<script src="{url}"></script>' for url in self.tail_js_urls])
return string

def get_head_js_str(self) -> str:
"""Return the string of JavaScript URLs that should be loaded in the `<head>` tag."""
if self.head_js_urls:
string = "\n".join([f'<script src="{url}"></script>' for url in self.head_js_urls])
else:
string = ""
return string
def get_head_js_str(self) -> str:
"""Return the string of JavaScript URLs that should be loaded in the `<head>` tag."""
if self.head_js_urls:
string = "\n".join([f'<script src="{url}"></script>' for url in self.head_js_urls])
else:
string = ""
return string

def get_head_css_str(self) -> str:
"""Return the string of CSS URLs that should be loaded in the `<head>` tag."""
if self.head_css_urls:
string = "\n".join([f'<link type="text/css" rel="stylesheet" href="{url}">' for url in self.head_css_urls])
else:
string = ""
return string
def get_head_css_str(self) -> str:
"""Return the string of CSS URLs that should be loaded in the `<head>` tag."""
if self.head_css_urls:
string = "\n".join([f'<link type="text/css" rel="stylesheet" href="{url}">' for url in self.head_css_urls])
else:
string = ""
return string
58 changes: 29 additions & 29 deletions src/openapidoc/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,43 @@

@dataclass
class Elements(Base):
"""Alternative API docs using Stoplight Elements."""
"""Alternative API docs using Stoplight Elements."""

js_url: Annotated[
str,
Doc(
"""
js_url: Annotated[
str,
Doc(
"""
The URL to use to load the Stoplight Elements JavaScript.
It is normally set to a CDN URL.
"""
),
] = "https://unpkg.com/@stoplight/elements/web-components.min.js"
css_url: Annotated[
str,
Doc(
"""
),
] = "https://unpkg.com/@stoplight/elements/web-components.min.js"
css_url: Annotated[
str,
Doc(
"""
The URL to use to load the Stoplight Elements CSS.
It is normally set to a CDN URL.
"""
),
] = "https://unpkg.com/@stoplight/elements/styles.min.css"
),
] = "https://unpkg.com/@stoplight/elements/styles.min.css"

def render(self) -> str:
"""Generate and return the HTML response that loads Stoplight Elements for the alternative API docs"""
self.head_css_urls.insert(0, self.css_url)
self.head_js_urls.insert(0, self.js_url)
html_template = self.get_html_template()
return html_template.format(
title=self.title,
favicon_url=self.favicon_url,
openapi_url=self.openapi_url,
head_css_str=self.get_head_css_str(),
head_js_str=self.get_head_js_str(),
tail_js_str=self.get_tail_js_str(),
)
def render(self) -> str:
"""Generate and return the HTML response that loads Stoplight Elements for the alternative API docs"""
self.head_css_urls.insert(0, self.css_url)
self.head_js_urls.insert(0, self.js_url)
html_template = self.get_html_template()
return html_template.format(
title=self.title,
favicon_url=self.favicon_url,
openapi_url=self.openapi_url,
head_css_str=self.get_head_css_str(),
head_js_str=self.get_head_js_str(),
tail_js_str=self.get_tail_js_str(),
)

def get_html_template(self) -> str:
html = """<!doctype html>
def get_html_template(self) -> str:
html = """<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
Expand All @@ -61,4 +61,4 @@ def get_html_template(self) -> str:
{tail_js_str}
</body>
</html>"""
return html
return html
Loading

0 comments on commit 78ce738

Please sign in to comment.