Search in sources :

Example 1 with RequestParam

use of entity.RequestParam in project CodeUtils by boredream.

the class ApiDocMain method parseApiDoc.

public static ArrayList<RequestInfo> parseApiDoc(String path) {
    File file = new File(path);
    String response = FileUtils.readToString(file, "UTF-8");
    Document parse = Jsoup.parse(response);
    ArrayList<RequestInfo> requestInfos = new ArrayList<RequestInfo>();
    // 接口类型总称 api-Search
    Elements rootElements = parse.getElementsByTag("section");
    for (Element rootElement : rootElements) {
        // 全部类型接口
        Elements docElements = rootElement.getElementsByTag("article");
        for (Element e : docElements) {
            RequestInfo requestInfo = new RequestInfo();
            // method post/get
            String method = "post";
            // System.out.println("-----> method = " + method);
            requestInfo.setMethod(method);
            String host = "http://short-server.cloudchat.stone-chat.com";
            // url
            String url = e.getElementsByAttributeValue("class", "pln").get(0).text().replace(host, "");
            // System.out.println("-----> url = " + url);
            requestInfo.setUrl(url);
            // System.out.println("---> name = " + attrId);
            String name = url.substring(1).replace("/", "_").toUpperCase();
            requestInfo.setName(name);
            // des
            String des = e.getElementsByAttributeValue("class", "pull-left").get(0).text();
            // System.out.println("-----> des = " + des);
            requestInfo.setDes(des);
            // post params
            Elements paramsElements = e.getElementsByTag("table");
            ArrayList<RequestParam> params = new ArrayList<RequestParam>();
            if (paramsElements.size() > 0) {
                Element ePostParams = paramsElements.get(0);
                for (Element ePostParam : ePostParams.getElementsByTag("tr")) {
                    // param 字段
                    Elements eColumn = ePostParam.getElementsByTag("td");
                    if (eColumn.size() == 0) {
                        continue;
                    }
                    // 第一个字段为参数名
                    String paramName = eColumn.get(0).text();
                    paramName = paramName.replace(" ", "");
                    paramName = paramName.replace("選項", "");
                    paramName = paramName.trim();
                    // 第二个字段为参数类型
                    // 可能类型为 String Number Float
                    String paramType = eColumn.get(1).text();
                    // 第三个字段为参数描述
                    String paramDes = eColumn.get(2).text();
                    // System.out.println("-----> param = " + paramName +
                    // " ... "
                    // + paramType + " ... " + paramDes);
                    RequestParam param = new RequestParam(paramName, paramType, paramDes, null);
                    params.add(param);
                }
            }
            requestInfo.setParams(params);
            requestInfos.add(requestInfo);
        // System.out.println();
        }
    }
    return requestInfos;
}
Also used : RequestParam(entity.RequestParam) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) RequestInfo(entity.RequestInfo) Elements(org.jsoup.select.Elements) File(java.io.File)

Example 2 with RequestParam

use of entity.RequestParam in project CodeUtils by boredream.

the class ApiDocMain method genCode.

private static void genCode(ArrayList<RequestInfo> infos) {
    StringBuilder sbUrls = new StringBuilder();
    StringBuilder sb = new StringBuilder();
    for (RequestInfo info : infos) {
        String urlName = info.getName();
        String url = info.getUrl();
        sbUrls.append(StringUtils.formatSingleLine(1, "/**"));
        sbUrls.append(StringUtils.formatSingleLine(1, " * " + info.getDes()));
        sbUrls.append(StringUtils.formatSingleLine(1, " */"));
        sbUrls.append(StringUtils.formatSingleLine(1, "public static final String " + urlName + " = \"" + url + "\";"));
        sbUrls.append("\n");
        // 方式注释里参数
        StringBuilder sbAnotation = new StringBuilder();
        // 方法参数里参数
        StringBuilder sbParam = new StringBuilder();
        // 方法内容里参数
        StringBuilder sbBody = new StringBuilder();
        ArrayList<RequestParam> params = info.getParams();
        if (params != null && params.size() > 0) {
            sbAnotation.append(StringUtils.formatSingleLine(1, " *"));
            for (RequestParam param : params) {
                // 方式注释里参数
                // * @param name 姓名
                sbAnotation.append(StringUtils.formatSingleLine(1, " * @param " + param.getName() + " " + param.getDes()));
                // 方法参数里参数 String phone, HttpListener<Object> listener
                sbParam.append(param.getType() + " " + param.getName() + ", ");
                // 方法内容里参数 params.put("phone", phone);
                sbBody.append(StringUtils.formatSingleLine(2, "params.put(\"" + param.getName() + "\", " + param.getName() + ");"));
            }
        }
        sbParam.append("HttpListener<Object> listener");
        sb.append(StringUtils.formatSingleLine(1, "/**"));
        sb.append(StringUtils.formatSingleLine(1, " * " + info.getDes()));
        sb.append(sbAnotation.toString());
        sb.append(StringUtils.formatSingleLine(1, " */"));
        StringBuilder methodNameSb = new StringBuilder();
        String[] nameItems = url.substring(1).split("[/_]");
        for (int i = 0; i < nameItems.length; i++) {
            String nameItem = nameItems[i];
            if (i > 0 && nameItem.length() > 0) {
                nameItem = nameItem.substring(0, 1).toUpperCase() + nameItem.substring(1);
            }
            methodNameSb.append(nameItem);
        }
        sb.append(StringUtils.formatSingleLine(1, "public static void " + methodNameSb.toString() + "(" + sbParam.toString() + ") {"));
        sb.append(StringUtils.formatSingleLine(2, "HashMap<String, Object> params = new HashMap<String, Object>();"));
        sb.append(sbBody.toString());
        sb.append(StringUtils.formatSingleLine(2, "doHttp(URLs.getUrl(URLs." + urlName + ")" + ", \"" + info.getMethod() + "\", params, Object.class, listener);"));
        sb.append(StringUtils.formatSingleLine(1, "}"));
        sb.append("\n");
    }
    System.out.println(sbUrls.toString());
    System.out.println("--------------");
    System.out.println(sb.toString());
}
Also used : RequestParam(entity.RequestParam) RequestInfo(entity.RequestInfo)

Example 3 with RequestParam

use of entity.RequestParam in project CodeUtils by boredream.

the class Main method getApiInfo.

private static RequestInfo getApiInfo(String link) throws Exception {
    RequestInfo requestInfo = new RequestInfo();
    String typeUrl = host + link;
    String response = BmobHttpUtils.getString(typeUrl);
    Document parse = Jsoup.parse(response);
    Elements refnamedivEs = parse.getElementsByClass("refname");
    String refnamediv = refnamedivEs.get(0).text().trim();
    requestInfo.setUrl(refnamediv);
    Elements titleEs = parse.getElementsByClass("dc-title");
    String title = titleEs.get(0).text().trim();
    requestInfo.setName(title);
    Elements desEs = parse.getElementsByAttributeValueContaining("class", "rdfs-comment");
    String description = desEs.get(0).text().trim();
    requestInfo.setDes(description);
    requestInfo.setMethod("post");
    Elements paramElements = parse.getElementsByAttributeValueContaining("class", "parameters");
    // request param
    ArrayList<RequestParam> params = new ArrayList<>();
    if (paramElements.size() > 0) {
        Elements name_type = paramElements.get(0).getElementsByTag("dt");
        Elements des = paramElements.get(0).getElementsByTag("dd");
        for (int i = 0; i < name_type.size(); i++) {
            // cate - int
            Element element = name_type.get(i);
            String name = element.text().split("-")[0].trim();
            String type = element.text().split("-")[1].trim();
            type = getType(name, type);
            String desStr = des.get(i).text().trim();
            params.add(new RequestParam(name, type, desStr, new ArrayList<String>()));
        }
    }
    // return value param
    Elements returnvaluesEs = parse.getElementsByAttributeValueContaining("class", "returnvalues");
    if (returnvaluesEs.size() > 0) {
        Elements exmEs = returnvaluesEs.get(0).getElementsByClass("exm");
        String exm = exmEs.get(0).text().trim();
        List<Json2JavaElement> jsonBeanTree = JsonUtils.getJsonBeanTree(exm);
        ArrayList<NameTypeDes> nameTypeDes = new ArrayList<NameTypeDes>();
        Elements name_type = returnvaluesEs.get(0).getElementsByTag("dt");
        Elements des = returnvaluesEs.get(0).getElementsByTag("dd");
        for (int i = 0; i < name_type.size(); i++) {
            // cate - int
            Element element = name_type.get(i);
            String name = element.text().split("-")[0].trim();
            String type = element.text().split("-")[1].trim();
            if (name.equals("list")) {
                // 集合数据
                Element listParamE = des.get(i);
                Elements list_name_type = listParamE.getElementsByTag("dt");
                Elements list_des = listParamE.getElementsByTag("dd");
                for (int j = 0; j < list_name_type.size(); j++) {
                    // cate - int
                    Element listE = list_name_type.get(j);
                    String listname = listE.text().split("-")[0].trim();
                    String listtype = listE.text().split("-")[1].trim();
                    String desStr = list_des.get(i).text().trim();
                    nameTypeDes.add(new NameTypeDes(listname, listtype, desStr));
                }
                String desStr = des.get(i).text().trim();
                nameTypeDes.add(new NameTypeDes(name, null, desStr));
            } else {
                type = getType(name, type);
                String desStr = des.get(i).text().trim();
                nameTypeDes.add(new NameTypeDes(name, type, desStr));
            }
        }
        // 遍历jsonbean集合,加上注释和类型
        for (Json2JavaElement jje : jsonBeanTree) {
            Iterator<NameTypeDes> iterator = nameTypeDes.iterator();
            while (iterator.hasNext()) {
                NameTypeDes next = iterator.next();
                if (jje.getName().equals(next.getName())) {
                    String type = next.getType();
                    if (type != null && type.equals("int")) {
                        jje.setType(int.class);
                    } else if (type != null && type.equals("long")) {
                        jje.setType(long.class);
                    }
                    jje.setDes(next.getDes());
                    iterator.remove();
                }
            }
        }
        String bean = JsonUtils.createJavaBean(jsonBeanTree);
        requestInfo.setResponseJson(bean);
    }
    requestInfo.setParams(params);
    return requestInfo;
}
Also used : RequestParam(entity.RequestParam) Json2JavaElement(entity.Json2JavaElement) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) RequestInfo(entity.RequestInfo) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Json2JavaElement(entity.Json2JavaElement)

Example 4 with RequestParam

use of entity.RequestParam in project CodeUtils by boredream.

the class WikiApiDocMain method parseApiDoc.

public static ArrayList<RequestInfo> parseApiDoc(String path) {
    File file = new File(path);
    String response = FileUtils.readToString(file, "UTF-8");
    Document parse = Jsoup.parse(response);
    ArrayList<RequestInfo> requestInfos = new ArrayList<>();
    Elements names = parse.getElementsByTag("h2");
    ArrayList<String> titleList = new ArrayList<>();
    for (Element nameElement : names) {
        String title = nameElement.text();
        Pattern pattern = Pattern.compile("[\\d]{1,2}");
        Matcher matcher = pattern.matcher(title);
        if (matcher.find(0)) {
            titleList.add(title);
        }
    }
    Elements rootElements = parse.getElementsByTag("table");
    for (Element rootElement : rootElements) {
        if (!rootElement.attr("class").contains("confluenceTable")) {
            continue;
        }
        if (rootElement.child(0).childNodeSize() < 2) {
            continue;
        }
        Elements tds = rootElement.getElementsByTag("td");
        if (tds.size() == 0) {
            continue;
        }
        RequestInfo requestInfo = new RequestInfo();
        // url
        Element urlElement = tds.get(0);
        String url = urlElement.text();
        List<String> paramsFromUrl = new ArrayList<>();
        // get类型url可能包含?
        if (url.contains("?")) {
            int getIndex = url.indexOf("?");
            for (String s : url.substring(getIndex + 1).split("&")) {
                paramsFromUrl.add(s.split("=")[0]);
            }
            url = url.substring(0, getIndex);
        }
        requestInfo.setUrl(url);
        // name
        requestInfo.setName("");
        // method
        String method = tds.get(1).text();
        requestInfo.setMethod(method);
        // params
        ArrayList<RequestParam> params = new ArrayList<>();
        Elements paramsElements = tds.get(2).getElementsByTag("tr");
        for (int i = 1; i < paramsElements.size(); i++) {
            Element paramElement = paramsElements.get(i);
            String name = paramElement.child(0).text();
            String type = "String";
            String desc = paramElement.child(1).text();
            String restType;
            if (url.contains("{" + name + "}")) {
                restType = "Path";
            } else if (method.equalsIgnoreCase("get")) {
                restType = "Query";
            } else {
                restType = "Body";
            }
            RequestParam param = new RequestParam(name, type, desc, null);
            param.setRestType(restType);
            params.add(param);
            // 如果请求参数表中,已经有参数,则先删除。没有的话在后面补上
            if (paramsFromUrl.contains(name)) {
                paramsFromUrl.remove(name);
            }
        }
        for (String param : paramsFromUrl) {
            // 只从url获取到的信息有限,类型写死为String
            params.add(new RequestParam(param, "String", "", null));
        }
        requestInfo.setParams(params);
        boolean has = false;
        for (RequestInfo ri : requestInfos) {
            if (ri.getUrl().equals(requestInfo.getUrl()) && ri.getMethod().equals(requestInfo.getMethod())) {
                has = true;
                break;
            }
        }
        if (!has) {
            requestInfos.add(requestInfo);
        }
    }
    for (int i = 0; i < requestInfos.size(); i++) {
        if (i < titleList.size()) {
            requestInfos.get(i).setName(titleList.get(i));
        }
    }
    return requestInfos;
}
Also used : Pattern(java.util.regex.Pattern) RequestParam(entity.RequestParam) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) RequestInfo(entity.RequestInfo) Elements(org.jsoup.select.Elements) File(java.io.File)

Example 5 with RequestParam

use of entity.RequestParam in project CodeUtils by boredream.

the class WikiApiDocMain method genRetrofitCode.

private static void genRetrofitCode(ArrayList<RequestInfo> infos) {
    String baseUrl = "http://dev-api.qbaolive.com/v1/";
    for (RequestInfo info : infos) {
        StringBuilder sb = new StringBuilder();
        // 方式注释里参数
        StringBuilder sbAnnotation = new StringBuilder();
        ArrayList<RequestParam> params = info.getParams();
        if (params != null && params.size() > 0) {
            sbAnnotation.append(StringUtils.formatSingleLine(1, " *"));
            ArrayList<RequestParam> bodyParams = new ArrayList<>();
            for (RequestParam param : info.getParams()) {
                if (param.getRestType().equals("Body")) {
                    bodyParams.add(param);
                } else {
                    // 方式注释里参数
                    // * @param name 姓名
                    sbAnnotation.append(StringUtils.formatSingleLine(1, " * @param " + param.getName() + " " + param.getDes()));
                }
            }
            if (bodyParams.size() > 0) {
                StringBuilder sbBodyParams = new StringBuilder();
                for (int i = 0; i < bodyParams.size(); i++) {
                    RequestParam param = bodyParams.get(i);
                    if (i == 0) {
                        sbAnnotation.append(StringUtils.formatSingleLine(1, " * @param body " + param.getName() + " " + param.getDes()));
                    } else {
                        sbAnnotation.append(StringUtils.formatSingleLine(1, " *             " + param.getName() + " " + param.getDes()));
                    }
                    sbBodyParams.append(",").append(param.getName());
                }
            }
        }
        sb.append(StringUtils.formatSingleLine(1, "/**"));
        sb.append(StringUtils.formatSingleLine(1, " * " + info.getName()));
        if (sbAnnotation.length() > 0) {
            sb.append(sbAnnotation.toString());
        }
        sb.append(StringUtils.formatSingleLine(1, " */"));
        String url = info.getUrl();
        String str1 = String.format("@%s(\"%s\")", info.getMethod(), url.replaceFirst(baseUrl, ""));
        sb.append(StringUtils.formatSingleLine(1, str1));
        url = url.replaceAll("/\\{[a-zA-Z0-9]+\\}", "");
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        String str2 = String.format("Observable<HttpResult<String>> %s(", methodName);
        sb.append(StringUtils.formatSingleLine(1, str2));
        if (info.getParams().size() > 0) {
            ArrayList<RequestParam> bodyParams = new ArrayList<>();
            for (RequestParam param : info.getParams()) {
                if (param.getRestType().equals("Body")) {
                    bodyParams.add(param);
                } else {
                    String p = String.format("@%s(\"%s\") String %s,", param.getRestType(), param.getName(), param.getName());
                    sb.append(StringUtils.formatSingleLine(3, p));
                }
            }
            if (bodyParams.size() > 0) {
                sb.append(StringUtils.formatSingleLine(3, "@Body Object body);"));
            } else {
                sb.replace(sb.lastIndexOf(","), sb.length(), ");");
            }
        } else {
            sb.replace(sb.length() - 1, sb.length(), ");");
        }
        sb.append("\n");
        System.out.println(sb.toString());
    }
}
Also used : RequestParam(entity.RequestParam) ArrayList(java.util.ArrayList) RequestInfo(entity.RequestInfo)

Aggregations

RequestParam (entity.RequestParam)9 RequestInfo (entity.RequestInfo)8 ArrayList (java.util.ArrayList)6 File (java.io.File)4 Document (org.jsoup.nodes.Document)4 Element (org.jsoup.nodes.Element)4 Elements (org.jsoup.select.Elements)4 Json2JavaElement (entity.Json2JavaElement)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2