Пример #1
0
    def get_routes(self, router, model, paths):
        verbs = obj()

        parameters = []
        route = router.route
        bits = []
        for is_dynamic, val in route.breadcrumbs:
            if is_dynamic:
                parameters.append(obj((
                    ("name", val),
                    ("description", ""),
                    ("type", "string"),
                    ("required", True),
                    ("in", "path")
                )))
                val = '{%s}' % val
            bits.append(val)
        path = '/%s' % ('/'.join(bits))

        for verb in methods:
            method = getattr(router, verb, None)
            if hasattr(method, '__call__'):
                doc = self.describe(router, verb, method, parameters, model)
                if doc:
                    verbs[verb] = doc

        if verbs:
            paths[path] = verbs

        for child in router.routes:
            self.get_routes(child, model, paths)
Пример #2
0
    def get_routes(self, router, model, paths):
        verbs = obj()

        parameters = []
        route = router.route
        bits = []
        for is_dynamic, val in route.breadcrumbs:
            if is_dynamic:
                parameters.append(
                    obj((("name", val), ("description", ""),
                         ("type", "string"), ("required", True), ("in",
                                                                  "path"))))
                val = '{%s}' % val
            bits.append(val)
        path = '/%s' % ('/'.join(bits))

        for verb in methods:
            method = getattr(router, verb, None)
            if hasattr(method, '__call__'):
                doc = self.describe(router, verb, method, parameters, model)
                if doc:
                    verbs[verb] = doc

        if verbs:
            paths[path] = verbs

        for child in router.routes:
            self.get_routes(child, model, paths)
Пример #3
0
 def doc(self):
     return obj((
         ("swagger", "2.0"),
         ("info", obj(
             version="",
             title="",
             description=""
         )),
         ("produces", [
             "application/json"
         ]),
         ("paths", obj())
     ))
Пример #4
0
    def run(self, options):
        docs = obj()
        default = self.app.config['APP_NAME'].lower()

        for api in self.app.apis:
            if api.netloc:
                continue

            for router in api.router.routes:
                model = router.model
                spec = default
                if model and model.spec:
                    spec = model.spec.lower()

                if spec not in docs:
                    docs[spec] = self.doc()

                spec = docs[spec]
                self.get_routes(router, model, spec["paths"])

        files = []
        for name, doc in docs.items():
            filename = '%s_spec.json' % name
            with open(filename, 'w') as fp:
                json.dump(doc, fp, indent=2)
                self.write('Created %s' % filename)
                files.append(filename)
        return files
Пример #5
0
    def run(self, options):
        docs = obj()
        default = self.app.config['APP_NAME'].lower()

        for api in self.app.apis:
            if api.netloc:
                continue

            for router in api.router.routes:
                model = router.model
                spec = default
                if model and model.spec:
                    spec = model.spec.lower()

                if spec not in docs:
                    docs[spec] = self.doc()

                spec = docs[spec]
                self.get_routes(router, model, spec["paths"])

        files = []
        for name, doc in docs.items():
            filename = '%s_spec.json' % name
            with open(filename, 'w') as fp:
                json.dump(doc, fp, indent=2)
                self.write('Created %s' % filename)
                files.append(filename)
        return files
Пример #6
0
    def describe(self, router, verb, method, parameters, model):
        if not model:
            doc = obj(description=method.__doc__)
            responses = getattr(method, 'responses', None)
            if responses:
                doc['responses'] = copy(responses)
            return doc

        return self.model_doc(verb, method, parameters, model)
Пример #7
0
    def describe(self, router, verb, method, parameters, model):
        if not model:
            doc = obj(description=method.__doc__)
            responses = getattr(method, 'responses', None)
            if responses:
                doc['responses'] = copy(responses)
            return doc

        return self.model_doc(verb, method, parameters, model)
Пример #8
0
 def model_doc(self, verb, method, parameters, model):
     doc = obj(model.json_docs.get(verb) or ())
     if "description" not in doc:
         doc["description"] = trim_docstring(method.__doc__)
     parameters = self.method_parameters(verb, method, parameters, model)
     if parameters:
         doc['parameters'] = parameters
     responses = getattr(method, 'responses', None)
     if responses:
         doc["responses"] = dict(method_responses(responses))
     return doc
Пример #9
0
 def model_doc(self, verb, method, parameters, model):
     doc = obj(model.json_docs.get(verb) or ())
     if "description" not in doc:
         doc["description"] = trim_docstring(method.__doc__)
     parameters = self.method_parameters(verb, method, parameters, model)
     if parameters:
         doc['parameters'] = parameters
     responses = getattr(method, 'responses', None)
     if responses:
         doc["responses"] = dict(method_responses(responses))
     return doc
Пример #10
0
    def method_parameters(self, verb, method, parameters, model):
        form = get_form_class(self.app, getattr(method, 'form', None))
        if not form:
            if verb == 'post':
                form = get_form_class(self.app, model.form)
            elif verb == 'patch':
                form = get_form_class(self.app, model.updateform)

        if form:
            parameters = (parameters or [])[:]
            for name, field in form.base_fields.items():
                parameters.append(
                    obj((("name",
                          name), ("description", field.help_text or field.label
                                  or name), ("type", field_type(field)),
                         ("required", field.required), ("in", "formData"))))
        return parameters
Пример #11
0
    def method_parameters(self, verb, method, parameters, model):
        form = get_form_class(self.app, getattr(method, 'form', None))
        if not form:
            if verb == 'post':
                form = get_form_class(self.app, model.form)
            elif verb == 'patch':
                form = get_form_class(self.app, model.updateform)

        if form:
            parameters = (parameters or [])[:]
            for name, field in form.base_fields.items():
                parameters.append(obj((
                    ("name", name),
                    ("description", field.help_text or field.label or name),
                    ("type", field_type(field)),
                    ("required", field.required),
                    ("in", "formData")
                )))
        return parameters
Пример #12
0
 def doc(self):
     return obj((("swagger", "2.0"), ("info",
                                      obj(version="",
                                          title="",
                                          description="")),
                 ("produces", ["application/json"]), ("paths", obj())))
Пример #13
0
def method_responses(responses):
    for code, info in mapping_iterator(responses):
        if isinstance(info, str):
            info = obj(description=info)
        yield code, info
Пример #14
0
def method_responses(responses):
    for code, info in mapping_iterator(responses):
        if isinstance(info, str):
            info = obj(description=info)
        yield code, info