Skip to content

Commit d70a98f

Browse files
committed
re-factor parameter names
1 parent f58bf7f commit d70a98f

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

example/__main__.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def main():
3838
"""Execute main method."""
3939
bind_address = "127.0.0.01"
4040
bind_port = 8000
41-
api_url = None
4241
workers = 2
4342
parser = argparse.ArgumentParser()
4443
resource_path = os.path.abspath(os.path.dirname(sys.modules[__name__].__file__)) + "/resources"
@@ -49,7 +48,9 @@ def main():
4948
default=bind_address,
5049
)
5150
parser.add_argument("--port", help="Bind port to listen for requests", default=bind_port)
52-
parser.add_argument("--api-url", help="API URL", default=api_url)
51+
parser.add_argument("--ui-url", help="OpenAPI UI URL (ie Swagger index) default is address:port")
52+
parser.add_argument("--docs", help="Docs", default="/docs")
53+
parser.add_argument("--server", help="Additional server(s) usable in the API docs", nargs="*", action="append")
5354
parser.add_argument("--resource-path", help="Path to API resource modules", default=resource_path)
5455
parser.add_argument("--workers", help="Number of worker threads", default=workers)
5556
parser.add_argument("--config", help="Configuration file", default=None)
@@ -65,6 +66,8 @@ def main():
6566
auth = AuthMiddleware([basic_auth, cookie_auth], control=AccessResource(default_mode="deny"))
6667

6768
args = parser.parse_args()
69+
70+
servers = [{"url": x, "description": ""} for x in args.server] if args.server else []
6871
middleware = [auth]
6972
if args.config:
7073
config = load_config(args.config)
@@ -91,11 +94,12 @@ def main():
9194
openapi = {
9295
"highlight": True,
9396
"sort": "alpha",
97+
"ui_url": args.ui_url,
98+
"servers": servers,
9499
}
95100

96101
app = Application(
97102
resource_path=args.resource_path,
98-
api_url=args.api_url,
99103
loglevel="info",
100104
accesslog=None,
101105
middleware=middleware,

src/reliqua/api.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def __init__(
7171
openapi = openapi or {}
7272

7373
openapi_path = openapi["path"]
74-
self.url = openapi["api_url"]
74+
self.url = openapi["ui_url"]
75+
self.servers = openapi["servers"]
7576

7677
self.openapi = openapi
7778
self.info = info
@@ -199,11 +200,11 @@ def _add_docs(self):
199200
sort=self.openapi["sort"],
200201
highlight=self.openapi["highlight"],
201202
)
202-
openapi = OpenApi(**self.info, auth=self.auth)
203+
openapi = OpenApi(**self.info, auth=self.auth, servers=self.servers)
203204
openapi.process_resources(self.resources)
204205
schema = openapi.schema()
205-
print(f"adding static route {self.openapi['docs_endpoint']} {self.openapi['file_path']}")
206+
print(f"adding static route {self.openapi['docs']} {self.openapi['file_path']}")
206207
self.add_static_route(self.openapi["static"], self.openapi["file_path"])
207-
self.add_route(self.openapi["docs_endpoint"], swagger)
208+
self.add_route(self.openapi["docs"], swagger)
208209
print(f"adding openapi file {self.openapi['spec']}")
209210
self.add_route(self.openapi["spec"], Docs(schema))

src/reliqua/app.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
}
2424

2525
OPENAPI_DEFAULTS = {
26-
"api_url": None, # API URL used by OpenAI UI (if different from bind, for example, behind a proxy)
27-
"ui_url": None, # OpenAPI UI index url
26+
"ui_url": None, # Swagger UI index URL (if different from bind, for example, behind a proxy))
2827
"highlight": True, # Enable OpenAPI syntax highlighting
2928
"sort": "alpha", # OpenAPI endpoint/tag sort order
3029
"path": "/openapi", # OpenAPI endpoint path (JSON and static files)
31-
"docs_endpoint": "/docs", # Documentation endpoint
30+
"docs": "/docs", # Documentation endpoint
31+
"servers": [], # List of target API servers (default is bind address)
3232
}
3333

3434
INFO_DEFAULTS = {
@@ -129,7 +129,11 @@ def __init__(
129129

130130
# Trim slashes from proxy URL if specified; otherwise set default proxy URL
131131
bind = self.gunicorn_options["bind"]
132-
openapi["api_url"] = openapi["api_url"].rstrip("/") if openapi["api_url"] else f"http://{bind}"
132+
openapi["ui_url"] = openapi["ui_url"].rstrip("/") if openapi["ui_url"] else f"http://{bind}"
133+
134+
# Add default api server if none specified
135+
if len(openapi["servers"]) == 0:
136+
openapi["servers"] = [{"url": f"http://{bind}", "description": "Default server"}]
133137

134138
self.application = Api(
135139
resource_path=resource_path,

src/reliqua/openapi.py

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def camelcase(string):
4747
"dict": "object",
4848
}
4949

50+
5051
DEFAULT_RESPONSE = {
5152
"type": "object",
5253
"properties": {
@@ -59,6 +60,10 @@ def camelcase(string):
5960
},
6061
}
6162

63+
64+
BINARY_RESPONSE = {"type": "string", "format": "binary"}
65+
66+
6267
verbs = ["get", "patch", "put", "post", "delete"]
6368

6469

@@ -591,6 +596,7 @@ def __init__(
591596
contact_email="",
592597
auth=None,
593598
parser=None,
599+
servers=None,
594600
):
595601
"""
596602
Create an OpenAPI class.
@@ -609,6 +615,7 @@ def __init__(
609615
:param str contact_url: Software URL
610616
:param str contact_email: Contact email
611617
:param Parser parser: The docstring parser
618+
:param list servers: List of servers
612619
:return:
613620
"""
614621
self.openapi = "3.1.0"
@@ -618,6 +625,7 @@ def __init__(
618625
self.summary = summary or ""
619626
self.terms = terms or ""
620627
self.auth = auth or []
628+
self.servers = servers or []
621629

622630
contact = Contact(name=contact_name, email=contact_email, url=contact_url)
623631
license = License(name=license, url=license_url)
@@ -633,6 +641,7 @@ def __init__(
633641
self.paths = {}
634642
self.component_schemas = {
635643
"default_response": DEFAULT_RESPONSE,
644+
"binary": BINARY_RESPONSE,
636645
}
637646
self.parser = parser
638647

@@ -682,4 +691,5 @@ def schema(self):
682691
"paths": self.paths,
683692
"components": self.components,
684693
"security": self.security_names,
694+
"servers": self.servers,
685695
}

0 commit comments

Comments
 (0)