forked from loafle/openapi-generator-original
Add oauth support to other Java clients
with libraries jersey2 and okhttp-gson
This commit is contained in:
parent
b27931cd8c
commit
a94f9b69af
@ -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).
|
||||||
*/
|
*/
|
||||||
|
@ -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).
|
||||||
*/
|
*/
|
||||||
|
@ -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).
|
||||||
*/
|
*/
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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).
|
||||||
*/
|
*/
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user