1. 版本说明
本示例基于华为云SDK V3.0版本开发
2. 功能介绍
华为云提供了代码托管服务端SDK,您可以直接集成服务端SDK来调用代码托管服务的相关API,从而实现对代码托管服务的快速操作。
该示例展示了如何通过java版SDK查询代码仓库列表。
3. 前置条件
获取AK/SK
开发者在使用前需先获取账号的ak、sk、endpoint、projectId。
您需要拥有华为云账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)。请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的 AK/SK。更多信息请查看访问密钥。
endpoint 华为云各服务应用区域和各服务的终端节点,详情请查看地区和终端节点。
projectId 云服务所在项目 ID ,根据你想操作的项目所属区域选择对应的项目 ID 。
4. 安装SDK
您可以通过Maven方式获取和安装SDK,您只需要在Java项目的pom.xml文件中加入相应的依赖项即可。
codehub的sdk版本需高于3.0.73
具体的SDK版本号请参见 SDK开发中心 。
Copied!
<dependencies>
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk-core</artifactId>
<version>3.0.73</version>
</dependency>
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk-codehub</artifactId>
<version>3.0.73</version>
</dependency>
</dependencies>
5. 示例代码
Copied!
import com.huaweicloud.sdk.codehub.v3.CodeHubClient;
import com.huaweicloud.sdk.codehub.v3.model.GetAllRepositoryByProjectIdRequest;
import com.huaweicloud.sdk.codehub.v3.model.GetAllRepositoryByProjectIdResponse;
import com.huaweicloud.sdk.codehub.v3.region.CodeHubRegion;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ServerResponseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CodeHubGetAllRepositoryDemo {
private static final Logger logger = LoggerFactory.getLogger(CodeHubGetAllRepositoryDemo.class.getName());
public static void main(String[] args) {
String ak = System.getenv("HUAWEICLOUD_SDK_AK");
String sk = System.getenv("HUAWEICLOUD_SDK_SK");
ICredential auth = new BasicCredentials()
.withAk(ak)
.withSk(sk);
CodeHubClient codeHubClient = CodeHubClient.newBuilder()
.withCredential(auth).withRegion(CodeHubRegion.CN_NORTH_4)
.build();
GetAllRepositoryByProjectIdRequest request = new GetAllRepositoryByProjectIdRequest();
request.setProjectUuid("porjectUuid");
request.setPageSize(10);
request.setPageIndex(0);
try {
GetAllRepositoryByProjectIdResponse response = codeHubClient.getAllRepositoryByProjectId(request);
System.out.println(response.toString());
} catch (ClientRequestException e) {
logger.error(String.valueOf(e.getHttpStatusCode()));
logger.error(e.toString());
} catch (ServerResponseException e) {
logger.error(String.valueOf(e.getHttpStatusCode()));
logger.error(e.toString());
}
}
}
6. 修订记录
发布日期 |
文档版本 |
修订说明 |
2021-12-31 |
1.1 |
codehub-sdk版本升级 |
1. 版本说明
本示例基于华为云SDK V3.0版本开发
2. 功能介绍
华为云提供了代码托管服务端SDK,您可以直接集成服务端SDK来调用代码托管服务的相关API,从而实现对代码托管服务的快速操作。 该示例展示了如何通过java版SDK查询代码仓库列表。
3. 前置条件
获取AK/SK 开发者在使用前需先获取账号的ak、sk、endpoint、projectId。 您需要拥有华为云账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)。请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的 AK/SK。更多信息请查看访问密钥。 endpoint 华为云各服务应用区域和各服务的终端节点,详情请查看地区和终端节点。 projectId 云服务所在项目 ID ,根据你想操作的项目所属区域选择对应的项目 ID 。
4. 安装SDK
您可以通过Maven方式获取和安装SDK,您只需要在Java项目的pom.xml文件中加入相应的依赖项即可。 codehub的sdk版本需高于3.0.73 具体的SDK版本号请参见 SDK开发中心 。
<dependencies> <dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-core</artifactId> <version>3.0.73</version> </dependency> <dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-codehub</artifactId> <version>3.0.73</version> </dependency> </dependencies>
5. 示例代码
import com.huaweicloud.sdk.codehub.v3.CodeHubClient; import com.huaweicloud.sdk.codehub.v3.model.GetAllRepositoryByProjectIdRequest; import com.huaweicloud.sdk.codehub.v3.model.GetAllRepositoryByProjectIdResponse; import com.huaweicloud.sdk.codehub.v3.region.CodeHubRegion; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.exception.ClientRequestException; import com.huaweicloud.sdk.core.exception.ServerResponseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; //查询代码仓库列表 public class CodeHubGetAllRepositoryDemo { private static final Logger logger = LoggerFactory.getLogger(CodeHubGetAllRepositoryDemo.class.getName()); public static void main(String[] args) { // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份认证为例,运行示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); //创建codeHubClient实例 CodeHubClient codeHubClient = CodeHubClient.newBuilder() .withCredential(auth).withRegion(CodeHubRegion.CN_NORTH_4) .build(); //创建查询任务列表请求,并指定任务列表偏移及数量 GetAllRepositoryByProjectIdRequest request = new GetAllRepositoryByProjectIdRequest(); request.setProjectUuid("porjectUuid"); request.setPageSize(10); request.setPageIndex(0); try { //执行查询任务列表请求 GetAllRepositoryByProjectIdResponse response = codeHubClient.getAllRepositoryByProjectId(request); System.out.println(response.toString()); } catch (ClientRequestException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.toString()); } catch (ServerResponseException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.toString()); } } }
6. 修订记录