Skip to content

Commit

Permalink
modified parameters for interactive swagger.
Browse files Browse the repository at this point in the history
also changed flake8 precommit repo.
  • Loading branch information
Dante Acosta committed Mar 14, 2024
1 parent 5285c91 commit 4aa3d17
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
args: [ --unsafe ]
- id: debug-statements
exclude: '^faraday/server/www/'
- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
Expand Down
41 changes: 37 additions & 4 deletions faraday/server/commands/app_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,47 @@
from flask import current_app

import yaml
import re
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from faraday import __version__ as f_version
import json
from urllib.parse import urljoin
from faraday.server.config import LOCAL_OPENAPI_FILE

from faraday.utils.faraday_openapi_plugin import FaradayAPIPlugin


def make_apispec_args(endpoint_route):

parameters = []

endpoint_params = [x[1:-1] for x in endpoint_route.split("/") if re.search("<(.*)>", x)]

for param in endpoint_params:

param_name = param_type = ""

if ":" not in param or re.search("(path|string|uuid):(.+)", param):
param_name = param.split(":")[-1]
param_type = "string"
elif re.search("int:(.+)", param):
param_name = param.split(":")[-1]
param_type = "integer"
elif re.search("float:(.+)", param):
param_name = param.split(":")[-1]
param_type = "number"

parameters.append({
"name": param_name,
"in": "path",
"schema": {"type": param_type},
"required": True
})

return parameters


def openapi_format(server, modify_default=False, return_tags=False):
extra_specs = {'info': {
'description': 'The Faraday REST API enables you to interact with '
Expand All @@ -33,8 +63,6 @@ def openapi_format(server, modify_default=False, return_tags=False):
if not server.startswith('http'):
raise ValueError('Server must be an http url')

server = urljoin(server, "/_api")

extra_specs['servers'] = [{'url': server}]

spec = APISpec(
Expand All @@ -58,12 +86,17 @@ def openapi_format(server, modify_default=False, return_tags=False):

tags = set()

endpoint_parameters = {}

for rule in current_app.url_map.iter_rules():
endpoint_parameters[rule.endpoint] = make_apispec_args(rule.rule)

with current_app.test_request_context():
for name, endpoint in current_app.view_functions.items():
# TODO: check why this endpoint is breaking spec.path
if name in ('static', 'index'):
continue
spec.path(view=endpoint, app=current_app)
spec.path(view=endpoint, app=current_app, parameters=endpoint_parameters[name])

# Set up global tags
spec_yaml = yaml.load(spec.to_yaml(), Loader=yaml.SafeLoader)
Expand Down

0 comments on commit 4aa3d17

Please sign in to comment.