RestfulBox icon indicating copy to clipboard operation
RestfulBox copied to clipboard

Add postman collection export option.

Open guilhermeh-lima opened this issue 10 months ago • 1 comments

Add new export action with postman collection.

src/main/java/io/github/newhoo/restkit/toolwindow/tree/ExportApiAction.java

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = e.getRequiredData(CommonDataKeys.PROJECT);
        List<RestItem> serviceItems = RestDataKey.SELECTED_SERVICE.getData(e.getDataContext());
        if (CollectionUtils.isEmpty(serviceItems)) {
            e.getPresentation().setVisible(false);
            return;
        }

        // check api item is valid. PsiElement may be invalid
        if (serviceItems.stream().anyMatch(restItem -> !restItem.isValid())) {
            NotifierUtils.errorBalloon(RestBundle.message("toolkit.toolwindow.tree.exportapi.error.title"), RestBundle.message("toolkit.toolwindow.tree.exportapi.error.content"), project);
            return;
        }

        ProgressManager.getInstance().run(new Task.Backgroundable(project, "[RestfulBox] Export api") {
            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                List<RestItem> exportItems = DumbService.getInstance(project).runReadActionInSmartMode(() -> {
                    return serviceItems.stream().map(o -> {
                        RestItem restItem = new RestItem();
                        restItem.setUrl(o.getUrl());
                        restItem.setMethod(o.getMethod());
                        restItem.setHeaders(o.getHeaders());
                        restItem.setParams(o.getParams());
                        restItem.setBodyJson(o.getBodyJson());
                        restItem.setDescription(o.getDescription());
                        restItem.setProject(o.getProject());
                        restItem.setModuleName(o.getModuleName());
                        restItem.setPackageName(o.getPackageName());
                        restItem.setFramework(o.getFramework());
                        restItem.setId(o.getId());
                        restItem.setProtocol(o.getProtocol());
                        return restItem;
                    }).collect(Collectors.toList());
                });

                // Convert to Postman collection
                String postmanCollection = convertToPostmanCollection(exportItems);
                IdeaUtils.copyToClipboard(postmanCollection);
                NotifierUtils.infoBalloon(RestBundle.message("toolkit.toolwindow.tree.exportapi.title"), RestBundle.message("toolkit.toolwindow.tree.exportapi.content"), null, project);
            }
        });
    }

     private String convertToPostmanCollection(List<RestItem> endpoints) {
        return JsonUtils.toJson(new PostmanCollection(
            new Info("ms-loan-send-debts", "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"),
            endpoints.stream().map(endpoint -> new Item(
                endpoint.getDescription(),
                new Request(
                    endpoint.getMethod(),
                    endpoint.getHeaders(),
                    new Url(
                        endpoint.getUrl() + (endpoint.getParams() != null && !endpoint.getParams().isEmpty() ? "?" + endpoint.getParams().stream().map(p -> p.getKey() + "=" + p.getValue()).collect(Collectors.joining("&")) : ""),
                        endpoint.getUrl().split("/"),
                        endpoint.getParams()
                    ),
                    endpoint.getBodyJson() != null ? new Body("raw", endpoint.getBodyJson()) : null
                )
            )).collect(Collectors.toList())
        ));
    }

PostmanCollection classes

   class PostmanCollection {
        private Info info;
        private List<Item> item;

        public PostmanCollection(Info info, List<Item> item) {
            this.info = info;
            this.item = item;
        }
    }

    class Info {
        private String name;
        private String schema;

        public Info(String name, String schema) {
            this.name = name;
            this.schema = schema;
        }
    }

    class Item {
        private String name;
        private Request request;

        public Item(String name, Request request) {
            this.name = name;
            this.request = request;
        }
    }

    class Request {
        private String method;
        private List<Header> header;
        private Url url;
        private Body body;

        public Request(String method, List<Header> header, Url url, Body body) {
            this.method = method;
            this.header = header;
            this.url = url;
            this.body = body;
        }
    }

    class Url {
        private String raw;
        private String[] path;
        private List<Param> query;

        public Url(String raw, String[] path, List<Param> query) {
            this.raw = raw;
            this.path = path;
            this.query = query;
        }
    }

    class Body {
        private String mode;
        private String raw;

        public Body(String mode, String raw) {
            this.mode = mode;
            this.raw = raw;
        }
    }

    class Header {
        private String key;
        private String value;
    }

    class Param {
        private String key;
        private String value;
    }

guilhermeh-lima avatar Feb 07 '25 15:02 guilhermeh-lima

You can try the latest version from jetbrains marketplace.

Image

newhoo avatar Feb 08 '25 13:02 newhoo