All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hn.doc.xyj.Xyj Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package com.hn.doc.xyj;

import cn.hutool.core.util.ReUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hn.utils.AssertUtils;
import com.hn.doc.xyj.domain.Doc;
import com.hn.doc.xyj.domain.Project;
import com.hn.doc.xyj.exception.XyjException;

import java.net.HttpCookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 小幺鸡工具类
 *
 * @author fei
 * @since  2019-11-21 11:56
 */
public class Xyj {

    private static final Log log = LogFactory.get();

    /**
     * 小幺鸡根地址
     */
    private String xyjUrl;
    /**
     * 登录cookie
     */
    private String cookie;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 密码
     */
    private String password;

    /**
     * 初始化xyj
     *
     * @param xyjUrl   小幺鸡根地址
     * @param email    邮箱
     * @param password 密码
     */
    protected Xyj(String xyjUrl, String email, String password) {
        this.xyjUrl = xyjUrl;
        this.email = email;
        this.password = password;
        setCookie();
    }

    /**
     * 初始化xyj
     *
     * @param xyjUrl   小幺鸡根地址
     * @param cookie cookie
     */
    protected Xyj(String xyjUrl,String cookie) {
        this.xyjUrl = xyjUrl;
        this.cookie = cookie;
    }

    /**
     * 设置登录cookie
     */
    private void setCookie() {
        Map map = new HashMap<>();
        map.put("email", this.email);
        map.put("password", this.password);
        HttpResponse res = HttpRequest.post(xyjUrl + "/login").form(map).execute();
        List cookies = res.getCookies();
        for (HttpCookie httpCookie : cookies) {
            cookie = httpCookie.toString();
        }
    }

    /**
     * 创建项目
     * @param projectName 项目名
     * @return project 项目ID
     */
    public String createProject(String projectName) {
        String url = xyjUrl + "/project";
        Map project = new Project()
                .setName(projectName)
//                .setDescription("项目测试")
                .setPermission("PUBLIC").build();
        JSONObject req = req(url, Method.POST, project);
        JSONObject data = req.getJSONObject("data");
        String projectId = data.getStr("projectId");
        // 默认文档的id
        String docId = data.getStr("docId");
        // 删除默认文档
        deleteDoc(docId);
        log.info("小幺鸡创建项目成功 projectId:{}",projectId);
        return projectId;
    }

    /**
     * 创建文件夹
     *
     * @param name      文件夹名称
     * @param projectId 项目id
     * @return docId 文件夹id
     */
    public String createFolder(String name, String projectId) {
        String url = xyjUrl + "/doc";
        Map doc = new Doc()
                .setName(name)
                .setProjectId(projectId)
                .setType("sys.folder").build();
        String docId = req(url, Method.POST, doc).getStr("data");
        log.info("小幺鸡创建文件夹成功 docId:{}",docId);
        return docId;
    }

    /**
     * 创建文档
     * @param parentId  文件夹ID
     * @param name 文档名称(接口名称)
     * @param projectId 项目ID
     * @return docId 文档ID
     */
    public String createDoc(String parentId, String name, String projectId) {
        String url = xyjUrl + "/doc";
        Map doc = new Doc()
                .setParentId(parentId)
                .setName(name)
                .setProjectId(projectId)
                .setType("sys.http").build();
        String docId = req(url, Method.POST, doc).getStr("data");
        log.info("小幺鸡创建文档成功 docId:{}",docId);
        return docId;
    }

    /**
     * 创建文档
     * @param name 文档名称(接口名称)
     * @param projectId 项目ID
     * @return docId 文档ID
     */
    public String createDoc(String name, String projectId) {
        return createDoc(null,name,projectId);
    }

    /**
     * 更新文档
     * @param doc {@link Doc}
     */
    public void updateDoc(Doc doc) {
        String url = xyjUrl + "/doc/" + doc.getDocId();
        req(url,Method.POST,doc.build());
    }

    /**
     * 删除文档
     * @param docId 文档id
     */
    public void deleteDoc(String docId) {
        String url = xyjUrl + "/doc/" + docId;
        req(url,Method.DELETE,null);
    }

    /**
     * 设置全局变量
     * @param projectId 项目ID
     * @param projectUrl 项目根地址
     */
    public void createGlobal(String projectId,String projectUrl) {
        // environment: [{"name":"dev","t":1574335476796,"vars":[{"name":"prefix","value":"http://localhost:8080/lawyer"}]}]
        Map map = new HashMap();
        map.put("environment","[{\"name\":\"dev\",\"t\":1574335476796,\"vars\":[{\"name\":\"prefix\",\"value\":\""+projectUrl+"\"}]}]");
        String url = xyjUrl + "/project/global/" + projectId;
        req(url,Method.POST,map);
    }

    /**
     *  请求小幺鸡
     * @param url 地址
     * @param method 请求方法
     * @param formMap 参数
     * @return JSONObject
     */
    private JSONObject req(String url, Method method, Map formMap) {
        //链式构建请求
        HttpRequest request = new HttpRequest(url);
        String result = request.method(method)
                .header(Header.COOKIE, cookie)
                .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8")
                .form(formMap)
                .timeout(20000)//超时,毫秒
                .execute().body();

        AssertUtils.notNull(result, "请先登陆");

        //使用正则获取所有标题 (.*?)
        List titles = ReUtil.findAll("(.*?)", result, 1);
        if (titles != null && !titles.isEmpty()) {
            throw new XyjException(titles.get(0));
        }
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Integer code = jsonObject.getInt("code");
        if (code == 0) {
            return jsonObject;
        }
        throw new XyjException(jsonObject.getStr("errorMsg"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy