Refactor _load_swagger_meta
factor out _handle_endpoint
This commit is contained in:
parent
70ec3237b6
commit
726a8c2f61
1 changed files with 124 additions and 114 deletions
|
@ -23,7 +23,6 @@ file instead.
|
||||||
from batesian.units import Units
|
from batesian.units import Units
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
import logging
|
import logging
|
||||||
import inspect
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
@ -371,18 +370,38 @@ def get_example_for_response(response):
|
||||||
class MatrixUnits(Units):
|
class MatrixUnits(Units):
|
||||||
def _load_swagger_meta(self, api, group_name):
|
def _load_swagger_meta(self, api, group_name):
|
||||||
endpoints = []
|
endpoints = []
|
||||||
|
base_path = api.get("basePath", "")
|
||||||
|
|
||||||
for path in api["paths"]:
|
for path in api["paths"]:
|
||||||
for method in api["paths"][path]:
|
for method in api["paths"][path]:
|
||||||
single_api = api["paths"][path][method]
|
logger.info(" ------- Endpoint: %s %s ------- " % (method, path))
|
||||||
full_path = api.get("basePath", "").rstrip("/") + path
|
|
||||||
|
try:
|
||||||
|
endpoint = self._handle_endpoint(
|
||||||
|
api["paths"][path][method], method,
|
||||||
|
base_path.rstrip("/") + path)
|
||||||
|
|
||||||
|
endpoints.append(endpoint)
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(
|
||||||
|
"Error handling endpoint %s %s: %s" % (method, path, e),
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"base": api.get("basePath").rstrip("/"),
|
||||||
|
"group": group_name,
|
||||||
|
"endpoints": endpoints,
|
||||||
|
}
|
||||||
|
|
||||||
|
def _handle_endpoint(self, endpoint_swagger, method, path):
|
||||||
endpoint = {
|
endpoint = {
|
||||||
"title": single_api.get("summary", ""),
|
"title": endpoint_swagger.get("summary", ""),
|
||||||
"deprecated": single_api.get("deprecated", False),
|
"deprecated": endpoint_swagger.get("deprecated", False),
|
||||||
"desc": single_api.get("description", single_api.get("summary", "")),
|
"desc": endpoint_swagger.get("description",
|
||||||
|
endpoint_swagger.get("summary", "")),
|
||||||
"method": method.upper(),
|
"method": method.upper(),
|
||||||
"path": full_path.strip(),
|
"path": path.strip(),
|
||||||
"requires_auth": "security" in single_api,
|
"requires_auth": "security" in endpoint_swagger,
|
||||||
"rate_limited": 429 in single_api.get("responses", {}),
|
"rate_limited": 429 in endpoint_swagger.get("responses", {}),
|
||||||
"req_param_by_loc": {},
|
"req_param_by_loc": {},
|
||||||
"req_body_tables": [],
|
"req_body_tables": [],
|
||||||
"res_headers": [],
|
"res_headers": [],
|
||||||
|
@ -392,13 +411,10 @@ class MatrixUnits(Units):
|
||||||
"req": "",
|
"req": "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.info(" ------- Endpoint: %s %s ------- " % (method, path))
|
path_template = path
|
||||||
|
|
||||||
path_template = api.get("basePath", "").rstrip("/") + path
|
|
||||||
example_query_params = []
|
example_query_params = []
|
||||||
example_body = ""
|
example_body = ""
|
||||||
|
for param in endpoint_swagger.get("parameters", []):
|
||||||
for param in single_api.get("parameters", []):
|
|
||||||
# even body params should have names, otherwise the active docs don't work.
|
# even body params should have names, otherwise the active docs don't work.
|
||||||
param_name = param["name"]
|
param_name = param["name"]
|
||||||
|
|
||||||
|
@ -448,10 +464,9 @@ class MatrixUnits(Units):
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
raise Exception("Error handling parameter %s" % param_name, e)
|
raise Exception("Error handling parameter %s" % param_name, e)
|
||||||
# endfor[param]
|
# endfor[param]
|
||||||
|
|
||||||
good_response = None
|
good_response = None
|
||||||
for code in sorted(single_api.get("responses", {}).keys()):
|
for code in sorted(endpoint_swagger.get("responses", {}).keys()):
|
||||||
res = single_api["responses"][code]
|
res = endpoint_swagger["responses"][code]
|
||||||
if not good_response and code == 200:
|
if not good_response and code == 200:
|
||||||
good_response = res
|
good_response = res
|
||||||
description = res.get("description", "")
|
description = res.get("description", "")
|
||||||
|
@ -470,32 +485,27 @@ class MatrixUnits(Units):
|
||||||
)
|
)
|
||||||
if "headers" in good_response:
|
if "headers" in good_response:
|
||||||
headers = []
|
headers = []
|
||||||
for (header_name, header) in good_response["headers"].iteritems():
|
for (header_name, header) in good_response[
|
||||||
|
"headers"].iteritems():
|
||||||
headers.append({
|
headers.append({
|
||||||
"key": header_name,
|
"key": header_name,
|
||||||
"type": header["type"],
|
"type": header["type"],
|
||||||
"desc": header["description"],
|
"desc": header["description"],
|
||||||
})
|
})
|
||||||
endpoint["res_headers"] = headers
|
endpoint["res_headers"] = headers
|
||||||
|
query_string = "" if len(
|
||||||
query_string = "" if len(example_query_params) == 0 else "?"+urllib.urlencode(example_query_params)
|
example_query_params) == 0 else "?" + urllib.urlencode(
|
||||||
|
example_query_params)
|
||||||
if example_body:
|
if example_body:
|
||||||
endpoint["example"]["req"] = "%s %s%s HTTP/1.1\nContent-Type: application/json\n\n%s" % (
|
endpoint["example"][
|
||||||
|
"req"] = "%s %s%s HTTP/1.1\nContent-Type: application/json\n\n%s" % (
|
||||||
method.upper(), path_template, query_string, example_body
|
method.upper(), path_template, query_string, example_body
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
endpoint["example"]["req"] = "%s %s%s HTTP/1.1\n\n" % (
|
endpoint["example"]["req"] = "%s %s%s HTTP/1.1\n\n" % (
|
||||||
method.upper(), path_template, query_string
|
method.upper(), path_template, query_string
|
||||||
)
|
)
|
||||||
|
return endpoint
|
||||||
endpoints.append(endpoint)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"base": api.get("basePath").rstrip("/"),
|
|
||||||
"group": group_name,
|
|
||||||
"endpoints": endpoints,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _handle_body_param(self, param, endpoint_data):
|
def _handle_body_param(self, param, endpoint_data):
|
||||||
"""Update endpoint_data object with the details of the body param
|
"""Update endpoint_data object with the details of the body param
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue