use of com.terran4j.commons.util.value.ValueSource 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.util.value.ValueSource 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