Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Provide a new open APl to return the organization list(#5349) #102

Merged
merged 8 commits into from
Mar 30, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.openapi.api;

import com.ctrip.framework.apollo.openapi.dto.OpenOrganizationDto;

import java.util.List;

public interface OrganizationOpenApiService {

List<OpenOrganizationDto> getOrganizations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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.client.service.OrganizationOpenApiService;
import com.ctrip.framework.apollo.openapi.dto.*;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
Expand All @@ -45,6 +46,7 @@ public class ApolloOpenApiClient {
private final String portalUrl;
private final String token;
private final AppOpenApiService appService;
private final OrganizationOpenApiService organizationOpenService;
private final ItemOpenApiService itemService;
private final ReleaseOpenApiService releaseService;
private final NamespaceOpenApiService namespaceService;
Expand All @@ -54,7 +56,7 @@ public class ApolloOpenApiClient {
private ApolloOpenApiClient(String portalUrl, String token, RequestConfig requestConfig) {
this.portalUrl = portalUrl;
this.token = token;
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig)
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setDefaultHeaders(Lists.newArrayList(new BasicHeader("Authorization", token))).build();

String baseUrl = this.portalUrl + ApolloOpenApiConstants.OPEN_API_V1_PREFIX;
Expand All @@ -63,6 +65,7 @@ private ApolloOpenApiClient(String portalUrl, String token, RequestConfig reques
namespaceService = new NamespaceOpenApiService(client, baseUrl, GSON);
itemService = new ItemOpenApiService(client, baseUrl, GSON);
releaseService = new ReleaseOpenApiService(client, baseUrl, GSON);
organizationOpenService = new OrganizationOpenApiService(client, baseUrl, GSON);
}

public void createApp(OpenCreateAppDTO req) {
Expand All @@ -83,6 +86,14 @@ public List<OpenAppDTO> getAllApps() {
return appService.getAllApps();
}

/**
* Get all Organizations
*/
public List<OpenOrganizationDto> getOrganization() {
return organizationOpenService.getOrganizations();
}


/**
* Get applications which can be operated by current open api client.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.ctrip.framework.apollo.openapi.client.service;

import com.ctrip.framework.apollo.openapi.client.url.OpenApiPathBuilder;
import com.ctrip.framework.apollo.openapi.dto.OpenOrganizationDto;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import java.lang.reflect.Type;
import java.util.List;

public class OrganizationOpenApiService extends AbstractOpenApiService implements
com.ctrip.framework.apollo.openapi.api.OrganizationOpenApiService{
public OrganizationOpenApiService(CloseableHttpClient client, String baseUrl, Gson gson) {
super(client, baseUrl, gson);
}

private static final Type ORGANIZATIONS_DTO_LIST_TYPE = new TypeToken<List<OpenOrganizationDto>>() {
}.getType();

@Override
public List<OpenOrganizationDto> getOrganizations() {
OpenApiPathBuilder pathBuilder = OpenApiPathBuilder.newBuilder()
.customResource("/organizations");

try (CloseableHttpResponse response = get(pathBuilder)) {
return gson.fromJson(EntityUtils.toString(response.getEntity()), ORGANIZATIONS_DTO_LIST_TYPE);
} catch (Throwable ex) {
throw new RuntimeException("get organizations information failed", ex);
}

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.ctrip.framework.apollo.openapi.dto;

public class OpenOrganizationDto {
private String orgId;
private String orgName;

public String getOrgId() {
return orgId;
}

public void setOrgId(String orgId) {
this.orgId = orgId;
}

public String getOrgName() {
return orgName;
}

public void setOrgName(String orgName) {
this.orgName = orgName;
}

@Override
public String toString() {
return "OpenOrganizationDto{" +
"orgId='" + orgId + '\'' +
", orgName='" + orgName + '\'' +
'}';
}
}