Merge pull request #1490 from xhh/java-oauth

Add oauth support for Java client
This commit is contained in:
wing328 2015-11-03 20:34:12 +08:00
commit 5bf4d022cf
10 changed files with 137 additions and 11 deletions

View File

@ -166,6 +166,19 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
/**
* Helper method to set access token for the first OAuth2 authentication.
*/
public void setAccessToken(String accessToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setAccessToken(accessToken);
return;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
*/ */

View File

@ -7,8 +7,20 @@ import java.util.List;
{{>generatedAnnotation}} {{>generatedAnnotation}}
public class OAuth implements Authentication { public class OAuth implements Authentication {
private String accessToken;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
// TODO: support oauth if (accessToken != null) {
headerParams.put("Authorization", "Bearer " + accessToken);
}
} }
} }

View File

@ -173,6 +173,19 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
/**
* Helper method to set access token for the first OAuth2 authentication.
*/
public void setAccessToken(String accessToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setAccessToken(accessToken);
return;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
*/ */

View File

@ -369,6 +369,19 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
/**
* Helper method to set access token for the first OAuth2 authentication.
*/
public void setAccessToken(String accessToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setAccessToken(accessToken);
return;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
*/ */

View File

@ -39,7 +39,7 @@ import io.swagger.client.auth.HttpBasicAuth;
import io.swagger.client.auth.ApiKeyAuth; import io.swagger.client.auth.ApiKeyAuth;
import io.swagger.client.auth.OAuth; import io.swagger.client.auth.OAuth;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-20T10:56:59.550-07:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-02T21:16:46.418+08:00")
public class ApiClient { public class ApiClient {
private Map<String, Client> hostMap = new HashMap<String, Client>(); private Map<String, Client> hostMap = new HashMap<String, Client>();
private Map<String, String> defaultHeaderMap = new HashMap<String, String>(); private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
@ -67,8 +67,8 @@ public class ApiClient {
// Setup authentications (key: authentication name, value: authentication). // Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>(); authentications = new HashMap<String, Authentication>();
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
authentications.put("petstore_auth", new OAuth()); authentications.put("petstore_auth", new OAuth());
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
// Prevent the authentications from being modified. // Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications); authentications = Collections.unmodifiableMap(authentications);
} }
@ -165,6 +165,19 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
/**
* Helper method to set access token for the first OAuth2 authentication.
*/
public void setAccessToken(String accessToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setAccessToken(accessToken);
return;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
*/ */

View File

@ -5,10 +5,22 @@ import io.swagger.client.Pair;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-20T10:56:59.550-07:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-02T21:16:46.418+08:00")
public class OAuth implements Authentication { public class OAuth implements Authentication {
private String accessToken;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
// TODO: support oauth if (accessToken != null) {
headerParams.put("Authorization", "Bearer " + accessToken);
}
} }
} }

View File

@ -43,7 +43,7 @@ import io.swagger.client.auth.HttpBasicAuth;
import io.swagger.client.auth.ApiKeyAuth; import io.swagger.client.auth.ApiKeyAuth;
import io.swagger.client.auth.OAuth; import io.swagger.client.auth.OAuth;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-29T09:31:27.804+08:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-02T22:10:35.641+08:00")
public class ApiClient { public class ApiClient {
private Client client; private Client client;
private Map<String, Client> hostMap = new HashMap<String, Client>(); private Map<String, Client> hostMap = new HashMap<String, Client>();
@ -172,6 +172,19 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
/**
* Helper method to set access token for the first OAuth2 authentication.
*/
public void setAccessToken(String accessToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setAccessToken(accessToken);
return;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
*/ */

View File

@ -5,10 +5,22 @@ import io.swagger.client.Pair;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-20T11:29:47.599-07:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-02T22:10:35.641+08:00")
public class OAuth implements Authentication { public class OAuth implements Authentication {
private String accessToken;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
// TODO: support oauth if (accessToken != null) {
headerParams.put("Authorization", "Bearer " + accessToken);
}
} }
} }

View File

@ -105,8 +105,8 @@ public class ApiClient {
// Setup authentications (key: authentication name, value: authentication). // Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>(); authentications = new HashMap<String, Authentication>();
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
authentications.put("petstore_auth", new OAuth()); authentications.put("petstore_auth", new OAuth());
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
// Prevent the authentications from being modified. // Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications); authentications = Collections.unmodifiableMap(authentications);
} }
@ -368,6 +368,19 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
/**
* Helper method to set access token for the first OAuth2 authentication.
*/
public void setAccessToken(String accessToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof OAuth) {
((OAuth) auth).setAccessToken(accessToken);
return;
}
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
*/ */

View File

@ -5,10 +5,22 @@ import io.swagger.client.Pair;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-10-20T11:42:25.339-07:00") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-02T22:14:00.422+08:00")
public class OAuth implements Authentication { public class OAuth implements Authentication {
private String accessToken;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
@Override @Override
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) { public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
// TODO: support oauth if (accessToken != null) {
headerParams.put("Authorization", "Bearer " + accessToken);
}
} }
} }