From d090389d013d6595f6270de088fd568c9a9c412f Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 29 May 2015 16:50:22 +0100 Subject: [PATCH] Start pre-processing swagger APIs before passing to sections. --- api/client-server/v1/profile.yaml | 6 +- templating/matrix_templates/sections.py | 3 + templating/matrix_templates/units.py | 76 ++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/api/client-server/v1/profile.yaml b/api/client-server/v1/profile.yaml index 99507186..622b463e 100644 --- a/api/client-server/v1/profile.yaml +++ b/api/client-server/v1/profile.yaml @@ -6,7 +6,7 @@ host: localhost:8008 schemes: - https - http -basePath: /_matrix/client/api/v1/profile +basePath: /_matrix/client/api/v1 consumes: - application/json produces: @@ -18,7 +18,7 @@ securityDefinitions: name: access_token in: query paths: - "/{userId}/displayname": + "/profile/{userId}/displayname": put: summary: Set the user's display name. security: @@ -67,7 +67,7 @@ paths: description: The user's display name if they have set one. 404: description: There is no display name for this user or this user does not exist. - "/{userId}/avatar_url": + "/profile/{userId}/avatar_url": put: summary: Set the user's avatar URL. security: diff --git a/templating/matrix_templates/sections.py b/templating/matrix_templates/sections.py index 2d35b8ae..09437b00 100644 --- a/templating/matrix_templates/sections.py +++ b/templating/matrix_templates/sections.py @@ -30,6 +30,9 @@ class MatrixSections(Sections): )) return "\n\n".join(sections) + def render_foo(self): + return json.dumps(self.units.get("swagger_apis")["profile"]["__meta"], indent=2) + def render_room_events(self): def filterFn(eventType): return ( diff --git a/templating/matrix_templates/units.py b/templating/matrix_templates/units.py index 485a5f75..f4c5f87f 100644 --- a/templating/matrix_templates/units.py +++ b/templating/matrix_templates/units.py @@ -9,6 +9,77 @@ import yaml class MatrixUnits(Units): + def _load_swagger_meta(self, api, group_name): + endpoints = [] + for path in api["paths"]: + for method in api["paths"][path]: + single_api = api["paths"][path][method] + endpoint = { + "title": single_api.get("summary"), + "desc": single_api.get("description"), + "method": method.upper(), + "path": path, + "requires_auth": "security" in single_api, + "rate_limited": 429 in single_api.get("responses", {}), + "req_params": [] + } + self.log(".o.O.o. Endpoint: %s %s" % (method, path)) + for param in single_api.get("parameters", []): + # description + desc = param.get("description") + if param.get("required"): + desc = "**Required.** " + desc + + # assign value expected for this param + val_type = param.get("type") # integer/string + refType = Units.prop(param, "schema/$ref/") # Error,Event + schemaFmt = Units.prop(param, "schema/format") # bytes e.g. uploads + if not val_type and refType: + val_type = refType # TODO: Resolve to human-readable. + if not val_type and schemaFmt: + val_type = schemaFmt + if val_type: + endpoint["req_params"].append({ + "name": param["name"], + "type": param["in"], + "val_type": val_type, + "desc": desc + }) + continue + # If we're here, either the param has no value or it is an + # object which we haven't $reffed (so probably just a json + # object with some keys; we'll add entries f.e one) + if "schema" not in param: + raise Exception( + "API endpoint group=%s path=%s method=%s param=%s"+ + " has no valid parameter value." % ( + group_name, path, method, param + ) + ) + if Units.prop(param, "schema/type") != "object": + raise Exception( + ("API endpoint group=%s path=%s method=%s defines a"+ + " param with a schema which isn't an object. Array?") + % (group_name, path, method) + ) + # loop top-level json keys + json_body = Units.prop(param, "schema/properties") + for key in json_body: + endpoint["req_params"].append({ + "name": key, + "type": "JSON", + "val_type": json_body[key]["type"], + "desc": json_body[key]["description"] + }) + + + endpoints.append(endpoint) + return { + "base": api.get("basePath"), + "group": group_name, + "endpoints": endpoints, + } + def load_swagger_apis(self): path = "../api/client-server/v1" apis = {} @@ -18,7 +89,10 @@ class MatrixUnits(Units): self.log("Reading swagger API: %s" % filename) with open(os.path.join(path, filename), "r") as f: # strip .yaml - apis[filename[:-5]] = yaml.load(f.read()) + group_name = filename[:-5] + api = yaml.load(f.read()) + api["__meta"] = self._load_swagger_meta(api, group_name) + apis[group_name] = api return apis def load_common_event_fields(self):