use of com.terran4j.commons.api2doc.domain.ApiParamObject in project commons by terran4j.
the class ApiCommentUtilsTest method testSee.
@Test
public void testSee() throws Exception {
Api2DocCollector collector = new Api2DocCollector();
ApiFolderObject folder = collector.toApiFolder(new MyController(), "myController");
ApiDocObject doc = folder.getDoc("updateUser");
List<ApiParamObject> params = doc.getParams();
Assert.assertEquals("用户id", params.get(0).getComment().toString());
Assert.assertEquals("123", params.get(0).getSample().toString());
Assert.assertEquals("用户名", params.get(1).getComment().toString());
Assert.assertEquals("terran4j", params.get(1).getSample().toString());
}
use of com.terran4j.commons.api2doc.domain.ApiParamObject in project commons by terran4j.
the class ParseApiCommentOnParam method testParseApiCommentOnParam.
@Test
public void testParseApiCommentOnParam() throws Exception {
log.info("testParseApiCommentOnParam");
Method method = ReflectionUtils.findMethod(getClass(), "setUser", new Class<?>[] { String.class, boolean.class, User.class });
Assert.assertNotNull(method);
Api2DocCollector collector = new Api2DocCollector();
List<ApiParamObject> params = collector.toApiParams(method, null);
Assert.assertEquals(4, params.size());
ApiParamObject type = params.get(0);
Assert.assertEquals("type", type.getId());
Assert.assertEquals("用户类型", type.getComment().getValue());
Assert.assertEquals("root", type.getSample().getValue());
ApiParamObject asRoot = params.get(1);
Assert.assertEquals("asRoot", asRoot.getId());
Assert.assertEquals(ApiParamLocation.RequestParam, asRoot.getLocation());
ApiParamObject id = params.get(2);
Assert.assertEquals("id", id.getId());
Assert.assertEquals("账号id", id.getComment().getValue());
Assert.assertEquals("123", id.getSample().getValue());
ApiParamObject name = params.get(3);
Assert.assertEquals("name", name.getId());
Assert.assertEquals("账号用户名", name.getComment().getValue());
Assert.assertEquals("neo", name.getSample().getValue());
}
use of com.terran4j.commons.api2doc.domain.ApiParamObject in project commons by terran4j.
the class ParseApiCommentOnSeeClassLoop method testParseApiCommentOnSeeClassLoop.
@Test
public void testParseApiCommentOnSeeClassLoop() throws Exception {
log.info("testParseApiCommentOnSeeClass");
Api2DocCollector collector = new Api2DocCollector();
ApiFolderObject folder = collector.toApiFolder(new ParseApiCommentOnSeeClassLoop.MyController(), "myController");
ApiDocObject doc = folder.getDoc("updateUser");
List<ApiParamObject> params = doc.getParams();
Assert.assertEquals("用户id", params.get(0).getComment().toString());
Assert.assertEquals("123", params.get(0).getSample().toString());
Assert.assertEquals("用户名称", params.get(1).getComment().toString());
Assert.assertEquals("neo", params.get(1).getSample().toString());
ApiResultObject user = doc.getResults().get(0);
ApiResultObject userId = user.getChildren().get(0);
Assert.assertEquals("id", userId.getId());
Assert.assertEquals("用户id", userId.getComment().getValue());
Assert.assertEquals("123", userId.getSample().getValue());
ApiResultObject userName = user.getChildren().get(1);
Assert.assertEquals("name", userName.getId());
Assert.assertEquals("用户名称", userName.getComment().getValue());
Assert.assertEquals("neo", userName.getSample().getValue());
}
use of com.terran4j.commons.api2doc.domain.ApiParamObject in project commons by terran4j.
the class Api2DocUtils method toURL.
public static String toURL(ApiDocObject doc, String serverURL) {
if (doc == null) {
throw new NullPointerException("doc is null.");
}
boolean hasGetMethod = false;
RequestMethod[] methods = doc.getMethods();
if (methods != null) {
for (RequestMethod method : methods) {
if (method == RequestMethod.GET) {
hasGetMethod = true;
}
}
} else {
hasGetMethod = true;
}
if (!hasGetMethod) {
// TODO: 暂时不支持非 GET 的请求。
return null;
}
String docURL = serverURL + doc.getPaths()[0];
List<ApiParamObject> params = doc.getParams();
Map<String, String> pathParams = new HashMap<>();
Map<String, String> getParams = new HashMap<>();
if (params != null) {
for (ApiParamObject param : params) {
if (param.getLocation() == ApiParamLocation.PathVariable) {
String value = param.getSample().getValue();
if (StringUtils.isEmpty(value)) {
value = param.getDataType().getDefault();
}
pathParams.put(param.getId(), value);
}
if (param.getLocation() == ApiParamLocation.RequestParam) {
String value = param.getSample().getValue();
if (StringUtils.isEmpty(value)) {
continue;
}
getParams.put(param.getId(), value);
}
}
}
if (pathParams.size() > 0) {
docURL = Strings.format(docURL, new ValueSource<String, String>() {
@Override
public String get(String key) {
String value = pathParams.get(key);
if (value == null) {
return null;
}
return encode(value);
}
}, "{", "}", null);
}
if (getParams.size() > 0) {
List<String> keys = new ArrayList<>();
keys.addAll(getParams.keySet());
keys.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
StringBuffer sb = new StringBuffer();
for (String key : keys) {
String value = getParams.get(key);
if (sb.length() > 0) {
sb.append("&");
}
sb.append(key).append("=").append(encode(value));
}
docURL = docURL + "?" + sb.toString();
}
return docURL;
}
use of com.terran4j.commons.api2doc.domain.ApiParamObject in project commons by terran4j.
the class CurlBuilder method toCurl.
public static String toCurl(ApiDocObject docObject, String serverURL) {
final List<ApiParamObject> allParams = docObject.getParams();
final KeyedList<String, String> headers = new KeyedList<>();
final KeyedList<String, String> params = new KeyedList<>();
final KeyedList<String, String> cookies = new KeyedList<>();
final Map<String, String> pathVars = new HashMap<>();
if (allParams.size() > 0) {
for (ApiParamObject param : allParams) {
String key = param.getId();
String value = param.getSample().getValue();
if (StringUtils.isEmpty(value)) {
Class<?> paramType = param.getSourceType();
if (paramType == String.class) {
value = key;
} else {
value = param.getDataType().getDefault();
}
}
if (param.getLocation() == ApiParamLocation.RequestParam) {
params.add(key, value);
}
if (param.getLocation() == ApiParamLocation.PathVariable) {
pathVars.put(key, value);
}
if (param.getLocation() == ApiParamLocation.RequestHeader) {
headers.add(key, value);
}
if (param.getLocation() == ApiParamLocation.CookieValue) {
cookies.add(key, value);
}
}
}
StringBuilder sb = new StringBuilder("curl ");
sb.append(enter);
// 将 Header 参数拼接起来。
if (headers.size() > 0) {
for (int i = 0; i < headers.size(); i++) {
String key = headers.getKey(i);
String value = headers.get(i);
sb.append(" -H \"").append(key).append(": ").append(value).append("\" ").append(enter);
}
}
if (cookies.size() > 0) {
sb.append(" -b \"");
sb.append(joinText(cookies, ";", "="));
sb.append("\" ").append(enter);
}
// 将 URL 中的 {xx} 变量用参数的示例值代替。
String url = serverURL + docObject.getPaths()[0];
if (pathVars.size() > 0) {
ValueSource<String, String> vars = new ValueSource<String, String>() {
@Override
public String get(String key) {
return encode(pathVars.get(key));
}
};
url = Strings.format(url, vars, "{", "}", null);
}
// 将“参数”拼起来。
if (params.size() > 0) {
RequestMethod[] requestMethods = docObject.getMethods();
if (requestMethods.length == 1 && requestMethods[0] == RequestMethod.POST) {
sb.append(" -d \"").append(joinText(params, "&", "=")).append("\" ").append(enter);
} else {
url += ("?" + joinText(params, "&", "="));
}
}
// 将 URL 拼接起来。
sb.append(" \"").append(url).append("\"");
String curl = sb.toString();
if (log.isInfoEnabled()) {
log.info("doc[{}]'s curl:\n{}", docObject.getId(), curl);
}
return curl;
}
Aggregations