From 1c21b8575bfd70b98b1102fd3dd09fc0bc335e14 Mon Sep 17 00:00:00 2001 From: "David M. Lee" Date: Mon, 22 Apr 2013 14:58:53 +0000 Subject: This patch adds a RESTful HTTP interface to Asterisk. The API itself is documented using Swagger, a lightweight mechanism for documenting RESTful API's using JSON. This allows us to use swagger-ui to provide executable documentation for the API, generate client bindings in different languages, and generate a lot of the boilerplate code for implementing the RESTful bindings. The API docs live in the rest-api/ directory. The RESTful bindings are generated from the Swagger API docs using a set of Mustache templates. The code generator is written in Python, and uses Pystache. Pystache has no dependencies, and be installed easily using pip. Code generation code lives in rest-api-templates/. The generated code reduces a lot of boilerplate when it comes to handling HTTP requests. It also helps us have greater consistency in the REST API. (closes issue ASTERISK-20891) Review: https://reviewboard.asterisk.org/r/2376/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@386232 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- rest-api-templates/make_stasis_http_stubs.py | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100755 rest-api-templates/make_stasis_http_stubs.py (limited to 'rest-api-templates/make_stasis_http_stubs.py') diff --git a/rest-api-templates/make_stasis_http_stubs.py b/rest-api-templates/make_stasis_http_stubs.py new file mode 100755 index 000000000..1ec7c5c91 --- /dev/null +++ b/rest-api-templates/make_stasis_http_stubs.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# Asterisk -- An open source telephony toolkit. +# +# Copyright (C) 2013, Digium, Inc. +# +# David M. Lee, II +# +# See http://www.asterisk.org for more information about +# the Asterisk project. Please do not directly contact +# any of the maintainers of this project for assistance; +# the project provides a web site, mailing lists and IRC +# channels for your use. +# +# This program is free software, distributed under the terms of +# the GNU General Public License Version 2. See the LICENSE file +# at the top of the source tree. +# + +try: + import pystache +except ImportError: + print >> sys.stderr, "Pystache required. Please sudo pip install pystache." + +import os.path +import pystache +import sys + +from asterisk_processor import AsteriskProcessor +from optparse import OptionParser +from swagger_model import * +from transform import Transform + +TOPDIR = os.path.dirname(os.path.abspath(__file__)) + + +def rel(file): + """Helper to get a file relative to the script's directory + + @parm file: Relative file path. + """ + return os.path.join(TOPDIR, file) + +API_TRANSFORMS = [ + Transform(rel('res_stasis_http_resource.c.mustache'), + 'res_stasis_http_{{name}}.c'), + Transform(rel('stasis_http_resource.h.mustache'), + 'stasis_http/resource_{{name}}.h'), + Transform(rel('stasis_http_resource.c.mustache'), + 'stasis_http/resource_{{name}}.c', False), +] + +RESOURCES_TRANSFORMS = [ + Transform(rel('stasis_http.make.mustache'), 'stasis_http.make'), +] + + +def main(argv): + parser = OptionParser(usage="Usage %prog [resources.json] [destdir]") + + (options, args) = parser.parse_args(argv) + + if len(args) != 3: + parser.error("Wrong number of arguments") + + source = args[1] + dest_dir = args[2] + renderer = pystache.Renderer(search_dirs=[TOPDIR], missing_tags='strict') + processor = AsteriskProcessor() + + # Build the models + base_dir = os.path.dirname(source) + resources = ResourceListing().load_file(source, processor) + for api in resources.apis: + api.load_api_declaration(base_dir, processor) + + # Render the templates + for api in resources.apis: + for transform in API_TRANSFORMS: + transform.render(renderer, api, dest_dir) + for transform in RESOURCES_TRANSFORMS: + transform.render(renderer, resources, dest_dir) + +if __name__ == "__main__": + sys.exit(main(sys.argv) or 0) -- cgit v1.2.3