forked from apolloconfig/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,444 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>apollo</artifactId> | ||
<groupId>com.ctrip.framework.apollo</groupId> | ||
<version>1.1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>apollo-openapi</artifactId> | ||
<name>Apollo Open Api</name> | ||
|
||
<properties> | ||
<java.version>1.7</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.ctrip.framework.apollo</groupId> | ||
<artifactId>apollo-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>jcl-over-slf4j</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
209 changes: 209 additions & 0 deletions
209
...-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/ApolloOpenApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package com.ctrip.framework.apollo.openapi.client; | ||
|
||
import com.ctrip.framework.apollo.openapi.client.constant.ApolloOpenApiConstants; | ||
import com.ctrip.framework.apollo.openapi.client.service.AppOpenApiService; | ||
import com.ctrip.framework.apollo.openapi.client.service.ItemOpenApiService; | ||
import com.ctrip.framework.apollo.openapi.client.service.NamespaceOpenApiService; | ||
import com.ctrip.framework.apollo.openapi.client.service.ReleaseOpenApiService; | ||
import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO; | ||
import com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO; | ||
import com.ctrip.framework.apollo.openapi.dto.OpenEnvClusterDTO; | ||
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO; | ||
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO; | ||
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceLockDTO; | ||
import com.ctrip.framework.apollo.openapi.dto.OpenReleaseDTO; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.base.Strings; | ||
import com.google.common.collect.Lists; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import java.util.List; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.message.BasicHeader; | ||
|
||
/** | ||
* This class contains collections of methods to access Apollo Open Api. | ||
* <br /> | ||
* For more information, please refer <a href="https://github.com/ctripcorp/apollo/wiki/">Apollo Wiki</a>. | ||
* | ||
*/ | ||
public class ApolloOpenApiClient { | ||
private final String portalUrl; | ||
private final String token; | ||
private final AppOpenApiService appService; | ||
private final ItemOpenApiService itemService; | ||
private final ReleaseOpenApiService releaseService; | ||
private final NamespaceOpenApiService namespaceService; | ||
|
||
private ApolloOpenApiClient(String portalUrl, String token, RequestConfig requestConfig) { | ||
this.portalUrl = portalUrl; | ||
this.token = token; | ||
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig) | ||
.setDefaultHeaders(Lists.newArrayList(new BasicHeader("Authorization", token))).build(); | ||
Gson gson = new GsonBuilder().setDateFormat(ApolloOpenApiConstants.JSON_DATE_FORMAT).create(); | ||
|
||
String baseUrl = this.portalUrl + ApolloOpenApiConstants.OPEN_API_V1_PREFIX; | ||
appService = new AppOpenApiService(client, baseUrl, gson); | ||
namespaceService = new NamespaceOpenApiService(client, baseUrl, gson); | ||
itemService = new ItemOpenApiService(client, baseUrl, gson); | ||
releaseService = new ReleaseOpenApiService(client, baseUrl, gson); | ||
} | ||
|
||
/** | ||
* Get the environment and cluster information | ||
*/ | ||
public List<OpenEnvClusterDTO> getEnvClusterInfo(String appId) { | ||
return appService.getEnvClusterInfo(appId); | ||
} | ||
|
||
/** | ||
* Get the namespaces | ||
*/ | ||
public List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName) { | ||
return namespaceService.getNamespaces(appId, env, clusterName); | ||
} | ||
|
||
/** | ||
* Get the namespace | ||
*/ | ||
public OpenNamespaceDTO getNamespace(String appId, String env, String clusterName, String namespaceName) { | ||
return namespaceService.getNamespace(appId, env, clusterName, namespaceName); | ||
} | ||
|
||
/** | ||
* Create the app namespace | ||
*/ | ||
public OpenAppNamespaceDTO createAppNamespace(OpenAppNamespaceDTO appNamespaceDTO) { | ||
return namespaceService.createAppNamespace(appNamespaceDTO); | ||
} | ||
|
||
/** | ||
* Get the namespace lock | ||
*/ | ||
public OpenNamespaceLockDTO getNamespaceLock(String appId, String env, String clusterName, String namespaceName) { | ||
return namespaceService.getNamespaceLock(appId, env, clusterName, namespaceName); | ||
} | ||
|
||
/** | ||
* Add config | ||
* @return the created config | ||
*/ | ||
public OpenItemDTO createItem(String appId, String env, String clusterName, String namespaceName, | ||
OpenItemDTO itemDTO) { | ||
return itemService.createItem(appId, env, clusterName, namespaceName, itemDTO); | ||
} | ||
|
||
/** | ||
* Update config | ||
*/ | ||
public void updateItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO) { | ||
itemService.updateItem(appId, env, clusterName, namespaceName, itemDTO); | ||
} | ||
|
||
/** | ||
* Create config if not exists or update config if already exists | ||
*/ | ||
public void createOrUpdateItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO) { | ||
itemService.createOrUpdateItem(appId, env, clusterName, namespaceName, itemDTO); | ||
} | ||
|
||
/** | ||
* Remove config | ||
* | ||
* @param operator the user who removes the item | ||
*/ | ||
public void removeItem(String appId, String env, String clusterName, String namespaceName, String key, | ||
String operator) { | ||
itemService.removeItem(appId, env, clusterName, namespaceName, key, operator); | ||
} | ||
|
||
/** | ||
* publish namespace | ||
* @return the released configurations | ||
*/ | ||
public OpenReleaseDTO publishNamespace(String appId, String env, String clusterName, String namespaceName, | ||
NamespaceReleaseDTO releaseDTO) { | ||
return releaseService.publishNamespace(appId, env, clusterName, namespaceName, releaseDTO); | ||
} | ||
|
||
/** | ||
* @return the latest active release information or <code>null</code> if not found | ||
*/ | ||
public OpenReleaseDTO getLatestActiveRelease(String appId, String env, String clusterName, String namespaceName) { | ||
return releaseService.getLatestActiveRelease(appId, env, clusterName, namespaceName); | ||
} | ||
|
||
|
||
public String getPortalUrl() { | ||
return portalUrl; | ||
} | ||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
public static ApolloOpenApiClientBuilder newBuilder() { | ||
return new ApolloOpenApiClientBuilder(); | ||
} | ||
|
||
public static class ApolloOpenApiClientBuilder { | ||
|
||
private String portalUrl; | ||
private String token; | ||
private int connectTimeout = -1; | ||
private int readTimeout = -1; | ||
|
||
/** | ||
* @param portalUrl The apollo portal url, e.g http://localhost:8070 | ||
*/ | ||
public ApolloOpenApiClientBuilder withPortalUrl(String portalUrl) { | ||
this.portalUrl = portalUrl; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param token The authorization token, e.g. e16e5cd903fd0c97a116c873b448544b9d086de8 | ||
*/ | ||
public ApolloOpenApiClientBuilder withToken(String token) { | ||
this.token = token; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param connectTimeout an int that specifies the connect timeout value in milliseconds | ||
*/ | ||
public ApolloOpenApiClientBuilder withConnectTimeout(int connectTimeout) { | ||
this.connectTimeout = connectTimeout; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param readTimeout an int that specifies the timeout value to be used in milliseconds | ||
*/ | ||
public ApolloOpenApiClientBuilder withReadTimeout(int readTimeout) { | ||
this.readTimeout = readTimeout; | ||
return this; | ||
} | ||
|
||
public ApolloOpenApiClient build() { | ||
Preconditions.checkArgument(!Strings.isNullOrEmpty(portalUrl), "Portal url should not be null or empty!"); | ||
Preconditions.checkArgument(portalUrl.startsWith("http://") || portalUrl.startsWith("https://"), "Portal url should start with http:// or https://" ); | ||
Preconditions.checkArgument(!Strings.isNullOrEmpty(token), "Token should not be null or empty!"); | ||
|
||
if (connectTimeout < 0) { | ||
connectTimeout = ApolloOpenApiConstants.DEFAULT_CONNECT_TIMEOUT; | ||
} | ||
|
||
if (readTimeout < 0) { | ||
readTimeout = ApolloOpenApiConstants.DEFAULT_READ_TIMEOUT; | ||
} | ||
|
||
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout) | ||
.setSocketTimeout(readTimeout).build(); | ||
|
||
return new ApolloOpenApiClient(portalUrl, token, requestConfig); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
.../main/java/com/ctrip/framework/apollo/openapi/client/constant/ApolloOpenApiConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.ctrip.framework.apollo.openapi.client.constant; | ||
|
||
public interface ApolloOpenApiConstants { | ||
int DEFAULT_CONNECT_TIMEOUT = 1000; //1 second | ||
int DEFAULT_READ_TIMEOUT = 5000; //5 seconds | ||
String OPEN_API_V1_PREFIX = "/openapi/v1"; | ||
String JSON_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...main/java/com/ctrip/framework/apollo/openapi/client/exception/ApolloOpenApiException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.ctrip.framework.apollo.openapi.client.exception; | ||
|
||
public class ApolloOpenApiException extends RuntimeException { | ||
|
||
public ApolloOpenApiException(int status, String reason, String message) { | ||
super(String.format("Request to apollo open api failed, status code: %d, reason: %s, message: %s", status, reason, | ||
message)); | ||
} | ||
} |
Oops, something went wrong.