Refactor auth method naming, add comments

This commit is contained in:
xhh
2015-05-29 11:29:06 +08:00
parent 6eb1a89205
commit e10e1f5249
12 changed files with 24 additions and 20 deletions

View File

@@ -166,7 +166,7 @@ public class ApiInvoker {
}
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException {
processAuthParams(authNames, queryParams, headerParams);
updateParamsForAuth(authNames, queryParams, headerParams);
Client client = getClient(host);
@@ -266,11 +266,12 @@ public class ApiInvoker {
}
}
private void processAuthParams(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
/* Update hearder and query params based on authentication settings. */
private void updateParamsForAuth(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
for (String authName : authNames) {
Authentication auth = Configuration.getAuthentication(authName);
if (auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
auth.processParams(queryParams, headerParams);
auth.applyToParams(queryParams, headerParams);
}
}

View File

@@ -39,7 +39,7 @@ public class ApiKeyAuth implements Authentication {
}
@Override
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
String value;
if (apiKeyPrefix != null) {
value = apiKeyPrefix + " " + apiKey;

View File

@@ -3,5 +3,6 @@ package {{invokerPackage}}.auth;
import java.util.Map;
public interface Authentication {
void processParams(Map<String, String> queryParams, Map<String, String> headerParams);
/** Apply authentication settings to header and query params. */
void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams);
}

View File

@@ -26,7 +26,7 @@ public class HttpBasicAuth implements Authentication {
}
@Override
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
try {
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {

View File

@@ -4,7 +4,7 @@ import java.util.Map;
public class OAuth implements Authentication {
@Override
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
public void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
// TODO: support oauth
}
}