Java SDK 示例

安装 SDK

Java SDK 使用说明:https://help.aliyun.com/document_detail/66496.html

使用 Maven 引入 SDK:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.0</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-codeup</artifactId>
    <version>0.0.8</version>
</dependency>

调用 Codeup API

以 CreateRepository 为例:

可以使用 Codeup Java SDK 或者阿里云 OpenAPI SDK 调用接口,区别在于使用阿里云OpenAPI SDK 需要手动设置接口信息。

使用 Codeup Java SDK

package com.alibaba.openapitest.demo;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.codeup.model.v20200414.CreateRepositoryRequest;
import com.aliyuncs.codeup.model.v20200414.CreateRepositoryResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;

public class CreateRepository {

    private String accessKeyId = "<AccessKey>";
    private String accessSecret = "<AccessSecret>";

    /**
     * 个人访问令牌;使用AK&SK或STS 临时授权方式不传该字段
     */
    private String personalAccessToken = "<PersonalAccessToken>";

    private String regionId = "cn-hangzhou";
    private String endPoint = "codeup.cn-hangzhou.aliyuncs.com";

    /**
     * 企业 ID
     */
    private String organizationId = "<OrganizationId>";

    public void createRepository() {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CreateRepositoryRequest request = new CreateRepositoryRequest();
        request.setEndpoint(endPoint);
        request.setOrganizationId(organizationId);
        request.setAccessToken(personalAccessToken);

        // 请求体参数,参考API文档
        String body = "{\"name\": \"repoName\", \"path\": \"repoPath\", \"visibility_level\": 10, \"namespace_id\": 123}";

        request.setHttpContent(JSON.toJSONString(body).getBytes(), "Utf-8", FormatType.JSON);
        try {
            CreateRepositoryResponse response = client.getAcsResponse(request);
            logInfo(String.valueOf(response.getResult().getId()));
        } catch (ServerException e) {
            logInfo(String.format("Fail. Something with your connection with Aliyun go incorrect. ErrorCode: %s",
                    e.getErrCode()));
        } catch (ClientException e) {
            logInfo(String.format("Fail. Business error. ErrorCode: %s, RequestId: %s",
                    e.getErrCode(), e.getRequestId()));
        }
    }

    private static void logInfo(String message) {
        System.out.println(message);
    }

    public static void main(String[] args) {
        new CreateRepository().createRepository();
    }
}

使用阿里云 OpenAPI SDK

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.0.3</version>
</dependency>
*/
public class CreateRepository {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("codeup.cn-hangzhou.aliyuncs.com");
        request.setVersion("2020-04-14");
        request.setUriPattern("/api/v3/projects");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("OrganizationId", "<OrganizationId>");
        request.putHeadParameter("Content-Type", "application/json");
        String requestBody = "" +
                "{" +
                "    \"name\": \"<RepoName>\"," +
                "    \"path\": \"<RepoPath>\"," +
                "    \"visibility_level\": 10," +
                "    \"namespace_id\": 123" +
                "}";
        request.setHttpContent(requestBody.getBytes(), "utf-8", FormatType.JSON);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}