Esempio n. 1
0
    def _build_doc(self) -> None:
        # Generate parts of the doc

        doc_dir = self.project_dir / "docs" / "source"
        #        doc_dir.mkdir()

        endpoint_dir = doc_dir / "endpoints"
        endpoint_template = self.env.get_template(
            "mattermost/doc/endpoint.rst.jinja")

        tags = []
        for tag in self.openapi.endpoint_collections_by_tag:
            tags.append(tag)

            endpoint_sync_path = endpoint_dir / f"sync_{snake_case(tag)}.rst"
            endpoint_sync_path.write_text(
                endpoint_template.render(tag=utils.pascal_case(tag),
                                         path="matterapi.endpoints.sync_api"),
                encoding=self.file_encoding,
            )

            endpoint_async_path = endpoint_dir / f"async_{snake_case(tag)}.rst"
            endpoint_async_path.write_text(
                endpoint_template.render(tag=utils.pascal_case(tag),
                                         path="matterapi.endpoints.async_api"),
                encoding=self.file_encoding,
            )

        endpoint_index_template = self.env.get_template(
            "mattermost/doc/endpoint_index.rst.jinja")
        endpoint_index_path = endpoint_dir / f"async_index.rst"
        endpoint_index_path.write_text(endpoint_index_template.render(
            tags=tags, prefix="async"),
                                       encoding=self.file_encoding)
        endpoint_index_path = endpoint_dir / f"sync_index.rst"
        endpoint_index_path.write_text(endpoint_index_template.render(
            tags=tags, prefix="sync"),
                                       encoding=self.file_encoding)
Esempio n. 2
0
def test_pascalcase(before, after):
    assert utils.pascal_case(before) == after
Esempio n. 3
0
    def _build_api(self) -> None:
        # Generate Client

        endpoint_dir = self.package_dir / "endpoints"
        endpoint_dir.mkdir()

        baseclass_path = endpoint_dir / "base.py"
        baseclass_template = self.env.get_template("base.py.jinja")
        baseclass_path.write_text(baseclass_template.render(),
                                  encoding=self.file_encoding)
        # Generate endpoints
        # api_dir = self.package_dir / "api"
        endpoint_sync_dir = endpoint_dir / "sync_api"
        endpoint_sync_dir.mkdir()
        #        endpoint_sync_init = endpoint_sync_dir / "__init__.py"
        #        endpoint_sync_init.write_text('""" Contains classes for accessing the API """', encoding=self.file_encoding)

        endpoint_async_dir = endpoint_dir / "async_api"
        endpoint_async_dir.mkdir()
        #        api_init = endpoint_sync_dir / "__init__.py"
        #        api_init.write_text('""" Contains classes for accessing the API """', encoding=self.file_encoding)

        endpoint_template = self.env.get_template(
            "mattermost/endpoint_class_body.py.jinja")

        oidmapping = dict()
        oidmapping_file = Path("operationid_mapping.json")
        if oidmapping_file.exists():
            with oidmapping_file.open() as mapfile:
                oidmapping = json.loads(mapfile.read())

        tags = list()
        imports = []
        api_classes = []

        tag_map = {
            snake_case(x.name): utils.clean_description(x.description)
            for x in self.openapi.tags
        }
        tag_map[
            "authentication"] = "Endpoint related to authentication operations"
        for tag, collection in self.openapi.endpoint_collections_by_tag.items(
        ):
            tags.append(tag)
            for endpoint in collection.endpoints:
                # Cleanup some things in the description
                # endpoint.description = self._clean_description(endpoint.description)
                oidpath = oidmapping.get(endpoint.path, None)
                oldoid = None
                if oidpath:
                    oldoid = oidpath.get(endpoint.method, None)
                else:
                    oidmapping[endpoint.path] = dict()
                if oldoid:
                    if endpoint.name != oldoid:
                        self.errors.append(
                            OperationIdError(
                                detail=
                                f"Operation ids for path `{endpoint.method.upper()} - {endpoint.path}` do not match. New: `{endpoint.name}`, Old: `{oldoid}`. Using old one. Update the mapping file if necessary",
                                level=ErrorLevel.WARNING,
                            ))
                    endpoint.name = oldoid
                else:
                    oidmapping[endpoint.path][endpoint.method] = endpoint.name
                    self.errors.append(
                        OperationIdError(
                            detail=
                            f"New path `{endpoint.method.upper()} - {endpoint.path}` with operation id `{endpoint.name}`. Update the mapping file and rerun, if this name is not correct",
                            level=ErrorLevel.WARNING,
                        ))
                # Hack to filter out duplicate 'None' responses and make the ordering for generated return types stable
                response_types = set()
                exception_codes = [400, 401, 403, 404, 405, 413, 429, 500, 501]
                for response in endpoint.responses:
                    if response.prop.get_type_string(
                    ) == "None" and response.status_code in exception_codes:
                        continue
                    response_types.add(response.prop.get_type_string())
                if "None" in response_types:
                    response_types.remove("None")
                    response_types = sorted(response_types) + ["None"]
                else:
                    response_types = sorted(response_types)
                endpoint.response_types = response_types

            endpoint_sync_path = endpoint_sync_dir / f"{snake_case(tag)}.py"
            endpoint_sync_path.write_text(
                endpoint_template.render(
                    tag=utils.pascal_case(tag),
                    collection=collection,
                    description=tag_map.setdefault(snake_case(tag)),
                    async_class=False,
                ),
                encoding=self.file_encoding,
            )

            endpoint_async_path = endpoint_async_dir / f"{snake_case(tag)}.py"
            endpoint_async_path.write_text(
                endpoint_template.render(
                    tag=utils.pascal_case(tag),
                    collection=collection,
                    description=tag_map.setdefault(snake_case(tag), ""),
                    async_class=True,
                ),
                encoding=self.file_encoding,
            )

            imports.append(
                f"from .{snake_case(tag)} import {utils.pascal_case(tag)}Api")
            api_classes.append(f"{utils.pascal_case(tag)}Api")

        init_template = self.env.get_template(
            "mattermost/endpoint_init.py.jinja")
        endpoint_init = endpoint_sync_dir / "__init__.py"
        endpoint_init.write_text(init_template.render(imports=imports,
                                                      api_classes=api_classes),
                                 encoding=self.file_encoding)
        endpoint_init = endpoint_async_dir / "__init__.py"
        endpoint_init.write_text(init_template.render(imports=imports,
                                                      api_classes=api_classes),
                                 encoding=self.file_encoding)

        client_dir = self.package_dir / "client"
        client_dir.mkdir()

        #client_path = client_dir / "client.py"
        #client_template = self.env.get_template("mattermost/client_base.py.jinja")
        #http_methods = ["get", "options", "head", "post", "put", "patch", "delete"]
        #client_path.write_text(client_template.render(methods=http_methods), encoding=self.file_encoding)

        client_base_path = client_dir / "base.py"
        client_base_template = self.env.get_template(
            "mattermost/client_base.py.jinja")
        client_base_path.write_text(client_base_template.render(),
                                    encoding=self.file_encoding)

        client_exceptions_path = client_dir / "exceptions.py"
        client_exceptions_template = self.env.get_template(
            "mattermost/exceptions.py.jinja")
        client_exceptions_path.write_text(client_exceptions_template.render(),
                                          encoding=self.file_encoding)

        client_sync_path = client_dir / "sync_client.py"
        client_sync_template = self.env.get_template(
            "mattermost/client_body.py.jinja")
        client_sync_path.write_text(client_sync_template.render(
            tags=tags, async_class=False),
                                    encoding=self.file_encoding)

        client_async_path = client_dir / "async_client.py"
        client_async_template = self.env.get_template(
            "mattermost/client_body.py.jinja")
        client_async_path.write_text(client_async_template.render(
            tags=tags, async_class=True),
                                     encoding=self.file_encoding)

        with oidmapping_file.open("w") as mapfile:
            mapfile.write(json.dumps(oidmapping, indent=4))